In this post, we will implement Record Triggered Flow with Example. Record Triggered Flow is the new type of Flow introduced by Salesforce.
Salesforce Record Triggered Flow allows us to get rid of some of the Apex Trigger methods. This will not replace the Apex Triggers completely as some complex logic needs to be implemented in Apex Triggers. But with Record Triggered Flow, it becomes easier to handle many scenarios rather than writing Apex Code for everything.
In this implementation, we will update MailingCountry of all the Contacts after the Account’s BillingCountry is changed. This can be achieved using Apex Trigger and Process Builder. But we will implement using Flow to check out the Record Triggered Flow feature.
Follow below step to create a Record Triggered Flow and get to the Flow Builder page.
- Type Flow in the Quick Find box and click on Flows under Workflow and Approvals.
- Click the New Flow button.
- Click on the Record-Triggered Flow.
- Choose either Freeform or Auto-Layout. I would recommend Auto-Layout as it automatically arranges the Flow elements.
After you follow all the above steps, Flow Builder will open with the Start and End element. Click on the Edit of Start element to specify the events for our flow. In our case, select the below values:
Trigger the Flow When: A record is updated.
Run the Flow: After the record is saved.
Then, we need to get the Contacts of the Account which triggered this flow. Use the Get Records element to fetch the Contacts associated with the Account based on the AccountId on Contact.
After that, we need to check if the Contacts that are returned by Get Records is Null. Add the Decision element to check if the returned value is Null. If it is Null, end the flow using End element.
If the returned value is not Null, use the Loop element to loop through the returned records. Add the Assignment element to assign the BillingCountry of Account to MailingCountry of Contact. Make sure to assign it to the current loop record.
There is one catch here about the Get Records. We cannot directly update the records that are fetched from Get Records. We are running the loop on records fetched from Get Records. But even though we update the MailingCountry of Contact inside the loop, we cannot directly update the list returned by Get Records. It will not be reflected in the database.
In order to update the Contacts, create another Resource of type Variable and Data Type of type Record, mark Allow multiple values (collection) to true, to make it a list variable.
Inside the loop, after assigning MailingCountry to Contact, add the current Contact from Loop to the resource variable that we created above.
Finally, to complete our Record Triggerd Flow, use the Update Records element to update the variable that we create above
Save and Activate the Flow.
Record Triggered Flow with Example
This is how our flow will look:
- Flow Basics: How to use Flow Builder in Salesforce?
- Salesforce Real Time Project Scenario: To-Do List in Lightning
To test this Record Triggered Flow, go to any Account record and update the Billing Country of Account, the Mailing Country of all the Contacts associated with this Account will be updated to match the Account’s Billing Country.
Keep in Mind
If an object has multiple active record triggered flows that are configured to run before the record is saved, there is no gurantee in the order of execution of these flows.
That is all from this post. If you don’t want to miss new implementations, please Subscribe here.
See you in the next implementation!