Update Child Records When There is a Change in Parent Record
Record-Triggered Flow in Salesforce is one of the many types of Flows that you can use to automate your business processes. You can trigger the Flow when:
- A record is created
- A record is updated
- A record is created or updated
- A record is deleted
And run the Flow:
- Before the record is saved
- After the record is saved
With record-triggered flows you can potentially avoid writing triggers in some specific scenarios. Now you have the option of meeting complex business requirements without writing a single line of code.
Flow is the future of automation in Salesforce and is becoming increasingly powerful with every release. If you are still using Workflow Rules and/or Process Builders to build new automations in Salesforce, I urge you to switch to flows and avoid incurring technical debt.
Spring ‘21 Release Update – Refer to the Prior Values of the Record That Triggered Your Flow
Now when an object’s record is updated, you can access that record’s earlier values in Salesforce Flow. The ‘$Record__Prior’ global variable contains the record’s values immediately before the Flow was run. You can use these values to check for changes in fields and calculate differences in your flow. This acts as an ‘ISCHANGED’ condition in Process Builder which we have made maximum use of in the past. So now you can take benefit of it in Flow and build logic around it.
For this guide, we will be covering how to use the aforementioned ‘$Record__Prior’ global variable in Salesforce Flow. After going through this guide, within the next 20 minutes, you will learn how to:
- Create a Record-Triggered Flow
- Save & Activate the Screen Flow
- Test the Screen Flow
In this guide, we will be creating a Flow to update all the Contacts’ addresses when the related Account address changes. Earlier it could be done using Process Builder, but now you can incorporate it in your Flows using ‘$Record__Prior’ global variable.
NOTE: Certain sections of the guide will appear as locked in the free preview. You can download the unlocked version of the guide in PDF format by subscribing to our “All Access” Pass through the link below.
Thanks a lot
Introducing Mastering Salesforce Flows Training Course
Liked this post? We are now offering a complete self-paced, video-based training course on Mastering Salesforce Flows. The course comes with downloadable step-by-step PDF guides, unlimited access, free upgrades, community discussion area, free preview and more. For more details, please click here.
Need Help with Flows?
Need advice on Flows? I also offer advisory & consulting services on Salesforce Flows. So, whether you want to create new Flows or migrate existing automations (Workflow Rules, Process Builder, Triggers) to Flows, I can help you analyze, design, plan & review. To initiate a discussion, please submit the contact form here stating your requirement or schedule a discussion at this URL.
References & Useful URLs
- Flow Types – Help Article (10 mins) –
https://help.salesforce.com/articleView?id=sf.flow_concepts_type.htm&type=5 - Refer to the Prior Values of the Record That Triggered Your Flow – Spring ’21 Release Notes Article (5 mins) – https://help.salesforce.com/articleView?id=release-notes.rn_forcecom_flow_fbuilder_prior_values_flow.htm&type=5&release=230
- With so many different automation features in Salesforce like validation rules, workflow rules, process builder, flows, apex triggers, assignment rules, escalation rules, auto-response rules etc., it is important for you to understand the order in which these rules will be executed. To download a infographic on the order in which these rules will be executed, please refer to my another blog post (10 mins) – Order of Execution (Rules, Triggers etc.) in Salesforce & Debug Log
Is it possible to add addError to an object in flow? I am trying to stop an approval process when an article has been rejected and there is no comment from the approver person.
Hi Flora, no I don’t think there is any option to add addError in Flow. And anyway, Salesforce’s standard Approval Process is not in Flow, it is a separate automation feature.
yes I am using that one already but as I said how else I can force the approver to add a comment when rejecting an article. I thought about flows before going into Apex.
Hi Flora, see if this thread helps – https://developer.salesforce.com/forums/?id=906F0000000BQtaIAG
How to add apex trigger in flows?
Hi Diksha, Apex Trigger and Flows are two separate things. You can’t “add” Apex Trigger in Flows as such. But what you can do is replace your Apex Trigger with Record-Triggered Flows (depending on the complexity of what your apex trigger is doing).
If you have both, Apex Trigger & Record-Triggered Flow on an object, they both will fire as per the Order of Execution Diagram – https://www.asagarwal.com/salesforce-order-of-execution-overview/
Great article, but I’m having trouble creating a flow.
Looking to have a note pop up when a specific picklist value is selected. I keep getting an error. Any suggestions ?
Thank you.
Hi Mary, this does not sound like a standard functionality of screen flow. How have you configured it?
Hi Ashish,
I have a requirement as below
Trigger the flow when opp product group record created
Objects :
Opportunity
CW Products
CW Product Groups
Opportunity Product Association
Opp Product Group Assoc
Relationship
Opp Product Group Assoc (MDR to CW Product Groups)
Opp Product Group Assoc (MDR to Opportunity)
Opportunity Product Association (MDR to CW Products) junction obj
Opportunity Product Association (MDR to Opportunity) junction obj
When I add/create New Opp Product Group Assoc record – flow need to trigger and get all the products from the selected CW Product Group and create Opportunity Product Association record for each product from the group and associate with Opportunity.
How to achieve this?
Many Thanks…
Hi Ashish,
looks like slide 19 of the PDF for creating record trigger flow is Old. Please check.
In current versions, we have
fast field updates ( before save) & Actions & Related records( After save)
Yes Subhasis, we have noticed that too. We are planning to upgrade the guides after Summer ’22 Release.
Hi Ashish is possible to implement a validation rule using record triggered flow where we would a use boolean variables in the assginment, set those boolean variables based of some conditions and then use those variables in the apex trigger and leverage sobject.adderror.
Hi Mangesh, sounds like it should be possible technically. But then please do a PoC to confirm. But then my question will be why to use Flows at all? Since you have to use trigger to leverage sobject.adderror, just do the whole thing in Apex itself. What do you think?
Ashish,
Is it possible in a Record Triggered Flow to determine if the record needs to be created or updated? They say to have only one automtion per object so I am confused as to how to do that on a record triggered flow. You must select the trigger and run flow options in the start node.
Hi Linda, I am not sure what do you mean by “Is it possible in a Record Triggered Flow to determine if the record needs to be created or updated?”. Record triggered flow fires when a record is created or updated. Can you please elaborate your question.
Hi Ashish,
Can we use the same flow for the Update and Create scenario by adding the check-in in the flow?
I have a few actions for On Create scenarios and a few for update Scenarios.I want to use the same flow for Update and On Create both. How can we add the check in the decision Box?
Hi there! It is possible to check the create and update scenario in Flow. Please refer to URL this blog post and see if it helps. https://jenwlee.com/2021/05/26/use-isnew-ischanged-and-priorvalue-in-flow-formulas/
Very Nicely explained !! Thank you.
Thank You Sachin!
Hi,
I have a peculiar problem where I need your advice and whether FLOW can accomplish this.
I have created a custom object – ACP and it has a master-detail relationship to ACCOUNT.
Now our OPPORTUNITY standard object has one mandatory ACCOUNT and we have also created 5 more custom look-up field to ACCOUNTs called Specifier, Main Contractor, Contractor, Installer and Distributor. These 4 fields are non-mandatory.
I would like to achieve:
1. If a record is created in ACP identifying an ACCOUNT and DATE, I would like to bring in all the opportunities (where close date = ACP DATE), in the related list or in the detail page, where this account is identified irrespective of whether it is identified on Account, Specifier, Contractor, etc.
2. Also I would like to roll up the Opportunity Value and show in ACP (without duplicates)…so for e.g. Opportunity = A has “Widget Inc” identified as the standard ACCOUNT and also as Contractor…then the Opportunity value should only be considered one time.
3. And also if in the future, the users create a new Opportunity identifying the ACCOUNT in #1 and Closedate as ACP Date, then this opportunity should also show in the ACP related list or detail section + the value should be added to the roll-up
Is this something achievable with FLOW or would need Coding?
Hi Chandru,
Here is how it should work.
Since you need to do steps 1 & 2 on creation of an ACP record as well as Opportunity record, create step 1 & 2 in an auto-launched flow (This won’t be a record triggered flow). In this auto launched flow you will need to
1. Include the ‘Get Records’ elements from Opportunity object where Close Date = ACP Date AND (Account = ACP.Account OR Contractor = ACP.Account OR Installer = ACP.Account … and so on). You can then insert these records in the child object of ACP object (you will need to create a child object, if you haven’t already)
2. Calculate the Opportunity rollup in ACP in the same Flow. You will have to run a loop in the Flow for all the records that was retrieved in Step 1 and add the values to a variable in the loop. Then just update the field on the ACP. This will not count the values twice as you will just get one record for an account whether it is Contractor or Installer or something else.
Now to call this auto-launched flow, you will have to use the process builder as currently you cannot call another flow from a record triggered flow.
As I am writing this reply and thinking in my head, I think it should be possible to achieve this with the help of a flow and process builder.
Hope this helps.
Hi,
Thank you for this helpful article.
Question: Can we use Trigger and record triggered flows in same object?
We are in the process of moving away from custom coding in Case Trigger by utilizing new flow functionality including record triggered flows. We were getting Too many SOQL queries: 101 error when trigger is turned on and works fine when trigger is turned off. Asking this question as we haven’t finished cleaning and recreating everything that trigger does in flow and to move what we have created to PROD and do rest of the work in phases.
Hi Abhi, yes, you can use the trigger and record triggered flows in same object. You just need to be mindful of the order of execution to ensure that the system behaves the way you expect it to. You may want to download the infographic from my blog post https://www.asagarwal.com/order-of-execution-rules-triggers-etc-in-salesforce-debug-log/ for this.
You are seeing the “Too many SOQL queries” because you are running both triggers and flows. See if there is a way to optimize the flow and/or trigger. If you are using SOQL statement inside a loop, you will need to move it outside the loop to avoid getting the error.
Realy good. Thanks Ashish
My pleasure Negah 🙂
Thanks for the excellent Content Ashish. Just wrapping my head around the exciting Triggered Flows. I got this error. I understand what it is saying. Funny thing though, I can’t find the Custom Validation it is referring to
Review the errors on this page.
We can’t save this record because the “Update Contact Address on Account Address Change” process failed. Give your Salesforce admin these details. This error occurred when the flow tried to update records: FIELD_CUSTOM_VALIDATION_EXCEPTION: Contact must be in Account ZIP Code. You can look up ExceptionCode values in the SOAP API Developer Guide.—This error occurred when the flow tried to update records:
No Worries Ashish. I found the validation rule on Contact. Deactivated it for testing purposes and everything works like a charm.
Again thanks for the excellent content
Good content well explained great effort. more power to you
Thanks Abhi!