In this Article we will show three (3) examples of using a Salesforce Flow "Get" and "Loop" action to find records which meet a certain criterion, loop through those Records, get a collection of data and, Sum the data collected, or get a single data constant, or update the records which meet the criterion with the same action.
The examples we will cover in the Article are:
- Get the Project Activities where the Name contains the text value "Installation." Use an Assignment element to Sum the "Count" field. Then, update the related Project record, to stamp the Sum of all "Installation" Project Activities onto the Project. Use case: The customer wants to divide the Sold Price (or Project Price for those using Supportworks or Products & Materials packages) by the number of Installation Activities, allowing them to see an “Installation Revenue” for each Project Activity on the Staff Calendar.
- Use a Get action to find a specific Project Activity record and update a different Project Activity record related to the same Project. Example: We want the Completion Date of the “Installation” Activity to populate the Due Date of the “Final Inspection” Project Activity Task with the Completed Date of the Installation plus 7 Days. Note: No Loop action is necessary for this exercise because we are only querying one related Final Inspection Project Activity record.
- Products and Materials package does not support Custom Fields. The native Trigger will not populate data from the templated Material record to subsequent Material records created via Sale Item creation. This Flow allows you to use a Get action to find the templated Product and Material (based on unique names) and get the desired data from your Custom Field, then update any subsequently created Material records with your custom data.
- Update the Custom Calendar Text field on the Project Activity object using a Flow, and use Line Breaks to separate the Text values displayed on the Calendar tile onto different lines, rather than a text string.
Example 1: Get the Sum of Project Activities where Name contains "Installation"
Upon Edit of any Project Activities with the name containing the word "Installation," Get the Project Activities where the Name contains the text value "Installation" AND the Project ID equals the Project>Record ID of the Project Activity currently being Edited.
Then, use an Assignment element to Sum the "Count" field.
Last, update the related Project record, stamping the Sum of all "Installation" Project Activities onto the Project.
Use case: The customer wants to divide the Sold Price (or Project Price for those using Supportworks or Products & Materials packages) by the number of "Installation" Activities, allowing them to see an “Installation Revenue” for each Project Activity, and display that data on the Staff Calendar using Custom Calendar Text.
Start Action:
Get Records action (be sure to choose All records)
Choose the Project Activities object
Set Conditions
Name 'contains' "Installation" AND
Choose the Project Activities' Project Record ID field 'equals' The Project Activity (Record) > Project > Record ID
This assures that the Project Activities being queried in the Get action have the same Project as the Project Activity being edited/saved in the action triggering the Flow.
Loop Records Action
Next click the Toolbox icon on the upper lift side of the Flow Builder tool
Click the New Resource button
Choose Variable
Choose Data Type of Number
Set Decimal Places to 0
and select Available for input and
Available for output
Next, Add an Assignment element to the Flow
Choose the Variable you've just created
and choose an Operator of Add, select the Current Item from the Loop
This Assignment assures that the values collected in the Loop element are added together and the Sum of all the values will be found after the final loop and the updated value is in the Variable element.
Next, you will want to add a Decision node to the Flow
This assures that you are only updating the record if the value currently on the record is different from the Sum of the Looped records in the Variable element. In this example: If the Sum of Installation Activities is 'not equal to' the Sum of the Count field from all the Project Activities queried in our Get action.
Final action! Update the Project record with the Sum of all Project Activities where the Name contains "Installation"
Add an Update Records action on the positive outcome side of the Flow (see screen shot at the beginning of this Example).
Choose to Specify conditions to identify records, and set fields individually
Choose the Project object
Set the Conditions
Choose the Project's Record ID field 'equals' The Project Activity (Record) > Project > Record ID
This assures that the Project being updated with a new value is the same Project related to the Project Activity being edited/saved in the action triggering the Flow.
Scroll down in this window to set the new Field value.
Choose the Installation Activities field and select the Value from the Variable
Choose Save As to save this Flow.
And click Activate to activate and test the Flow.
Example 2: Get a specific Project Activity and update the Date Value
Use a Get action to find a specific Project Activity record and update the Project Activity record related to the same Project. Example: We want the Completion Date of the “Installation” Activity to populate the Due Date of the “Final Inspection” Project Activity Task, using the Completed Date of the "Installation" activity plus 7 Days. Note: No Loop action is necessary for this exercise because we are only querying one related Final Inspection Project Activity record.
Start Node:
- Project Activity object
- when a record is created or updated
- Name contains Installation
- Start Date is not Blank
Create a Decision Node
Only fire when the Start date of the Installation is Changed:
Next click the Toolbox icon on the upper lift side of the Flow Builder tool
Click the New Resource button
Create a new Formula date element. Choose the record's Start date plus 30 days.
Save the formula.
On the positive outcome path of the Flow, choose an Update Records element.
Choose Project Activity object with the following conditions:
Project ID of the Installation activity is equal to the Project ID of the related Project Activity and the Name contains "Final Inspection"
When this is True, update the Final Inspection's Due Date with the Formula element you've created.
Save the Flow and Activate
Example 3: Populate data from a Custom Field on the Material records created via a managed Trigger
improveit 360's Products and Materials Managed package does not support Custom Fields on the Materials object. The native Trigger will not populate data from the templated Material record to any subsequent Material records created via Sale Item creation. This Flow allows you to use a Get action to find the templated Product and Material (based on unique names) and get the desired data from your Custom Field, then update any subsequently created Material records with your custom data. In the following example, we will create a Comments field on the Material. When the native Trigger creates subsequent Material records, the Flow will locate the original Material and copy the Comments field text value to the newly created Material record.
Start node:
- Material object
- A record is created or updated
- Comments field is Blank
Use a Get action to find the related Material with the same Name, same related Product, and is not related to any Sale Items or Projects, and its Comments field is not Blank.
Next, we will Loop the Materials from the Get action:
Next click the Toolbox icon on the upper lift side of the Flow Builder tool
Click the New Resource button
Choose Variable
This variable element will be used to store the Comments value.
Select a Data Type of Text and Available for input and Available for output.
Next add an Assignment element and assign the Comments value from the Material record in the Loop to the Text Variable you've just created.
After Last, add an element to Update Triggering Record
Use the material record that triggered the flow
Update the Comments field on the record, with the Variable element you've created:
Save the Flow and Activate
Example 4: Use a Flow to update Custom Calendar Text and use a <BR> or <P> to use line breaks on Calendar Tiles
Configure Start Node
- Project Activity
- Created or updated
- No other Conditions
Open the Toolbox
Click New Resource and select Constant
Data Type: Text
Set the value as "<br>" (the HTML for a line break) NOTE: You could also use "<p>" to insert a paragraph break.
Next, click New Resource again
this time select Formula
Use Text formatting to set the Value for Custom Calendar Text fields
NOTE: Use the Constant element you created for the Line Break to separate text into multiple lines on the Calendar tile.
Next, select an element to Update Triggering record
Set the Calendar Text Field using the Value from the Formula element you created in the last step
Save the Flow and Activate