Peter Bender asked “What’s the best way with declarative automation to update the records on the other side of a junction object? For instance, if a change is made to an Opportunity, update a field on all Contacts related to that record via an OpportunityContactRole. “
The new Collection Actions capability in Spring ’20 provides an opportunity to attack this declaratively. We started out building collection actions to do simple collection manipulation, but working with junction objects requires collection actions that can work with relationships. To that end, we’ve added two new actions:
GetChildCollection takes a record and returns a specified collection of child records.
GetLookupCollection takes a collection of records and the name of an object related via a Lookup, and returns a collection of objects.
Let’s look at this from the perspective of the example requested above.
Suppose that, whenever an Opportunity changes to a Status of Closed Won, we want any Contacts that are associated with that Opportunity via an Opportunity Contact Role to have their field of “Needs Thanks!” set to True.
Here’s what the Flow looks like:
Starting with the affected opportunity we first call the Get Child Collection action. We pass in the Opportunity and the name of the related object type (OpportunityContactRole) that we want.
Here’s how the action is configured:
We actually have to specify the child collection twice: once at the top to ‘tune’ the collection action to output Opportunity Contact roles, and again in the childRelationshipName (at some point we’ll be able to simplify that). We need to specify the fields we want to retrieve but in this case we only need the Id field.
We next want to get all of the Contacts linked to the Opportunity Contact Roles. This is done via a Lookup relationship Opportunity Contact Role to Contact. The Get Lookup Collection action allows us to do this with one element:
Here we set the inputCollection to be the automatically generated output from the previous Get Child Records. We pass in the object that we’re looking for. Note that this version currently only works with objects that have a single relationship between them. In other words if, there were two different fields on OpportunityContactRole that both looked up to Contact, this action would grab the first one it found, which isn’t reliable (we’ll fix this).
The returned set of Contacts is then passed to Map Collection, which changes the field.
Install Collection Actions here.