In this blog, we will try to understand how to create Record Collection variables, create a loop, set values via assignments, add a loop record to a collection, and handle DML (database actions) outside of the loop in a flow.
As a multi-tenant environment, Salesforce has various governor constraints that apply to both code and automation. The DML actions (get, edit, create, or delete) should always be carried out outside of your loop to prevent exceeding the governor restrictions. Use the loop's assignment element to accomplish this. At the end of the loop, perform the action for all the records at once.
As we know, Flow is the only automation tool that can perform actions on a group of records. Collections are a list of records or values on which you want to work. A loop element is usually required to perform operations on each item in the collection.
Collection Variable:-
A collection variable is a list of records on which you want to perform an action. A loop stores the values of the current record in the collection in a loop variable. When the loop has finished processing one loop record, Salesforce inserts the next record into the loop variable. Add the loop record variable as a record to a new collection variable to track changes made along the loop path.
Use Case:
Bertah is the system administrator at Trident Fasteners. Rahul Yadav is the Director of Operations. He would like to automatically update Opportunities whose Stage Name is 'Qualification' and Closed Date is 'Today' and set the Stage Name to 'Closed Lost'.
Solution:
Getting the Opportunities records that have the status is 'Qualification' and Closed Date is 'Today' and taking action on those records.
The end result is as follows:
Steps:
From Setup, in the Quick Find box, enter Flows, select Flows, and then click New Flow. Select the flow type as "Autolaunched Flow", then click Next.
Click the "New Resource" button in the Flow builder. Select Resource Type as 'Variable'. Give the api Name as 'oppRecordCollection'. Select data type as 'Record'. Object as 'Opportunity' and Enable the checkbox Allow multiple values (collection).Click Done.
Next, we'll use the Get Records flow element to query the Opportunity object for all records whose stage name is 'Qualification' and Closed Date is 'Today'.
Object: Opportunity
Filtering the Opportunity Records by Stage name Equals Qualification and Closed Date is Today.
How many records to store: All records
For the “How to Store the Data” option, I chose to use “Automatically store all fields.”
Filtering the Opportunity Records by Stage name Equals Qualification and Closed Date is Today.
How many records to store: All records
For the “How to Store the Data” option, I chose to use “Automatically store all fields.”
Next Step is to determine whether records were found in the previous step or Not. So for this use the Decision element.
{!getOppRecords} isNull {!$GlobalConstant.False}
If the Decision elements founds the Opportunity Records means satisfied the Outcomes as Yes. We now need a Loop flow element which will take each record in the getOppRecords record collection variable to iterate through. Else do Nothing.
We need one more Assignment Element to add loop record to our Record Collection variable i.e. oppRecordCollection.
{!oppRecordCollection} add {!oppRecord}
Now we want to update all the records in the collection {!oppRecordCollection} with an Update Records flow element
Now Save the flow and Activate it.
This flow can be configured as a scheduled flow, invoked by a process, or triggered by a button. This blog post will not go into detail about the initiation. Remember to test your configuration changes.
- Make a few Opportunities with the stage name Qualification and a deadline of Today.
- Start the flow.
- Check to see if the Opportunities have been updated (StageName as Closed Lost).
No comments:
Post a Comment