What is a Loop Block?
A Loop Block is a Group Element in the Integration Procedure that iterates over the items in a data array, enabling the Actions within it to repeat for each item.
For conditional execution of a Loop Block, we can add conditions in the ‘Execution Conditional Formula’ field of :
The Loop Block group element – to conditionally execute the whole Loop Block (and/or),
The actions inside the Loop Block - to just execute the actions inside the Loop Block conditionally.
Let us understand how to use a Loop Block Group element in the Integration Procedure.
Example :
“How to concatenate the ‘Contact Names’ of all the Contacts linked with an Account using a Loop Block and send that response.”
Here, the Data Raptor Extract Action ‘DRExtractContacts’ retrieves the Contacts linked with an Account (‘Test Account ABC’) and maps the field values to a ‘Contacts’ array.
Now, two Set Values action (‘SVContactName’ and ‘SVContactCount’) elements are added to the IP. They are used to initialize the ‘contact names’ and the ‘count of contacts’ elements respectively, which will be used inside the Loop Block.
SVContactName:
In the Element Value Map, a new element is added with the below details.
Element Name: ‘ContactName’Value: blank
In the ‘Response Transformations’ section, the below details are entered.
Response JSON Path: ‘ContactName’
Response JSON Node: ‘ContactsResult’
We can use any custom element name of our choice.
SVContactCount:
Similar to the above SV action, in the Element Value Map, a new element is added with the below details.
Element Name: ‘ContactCount’Value: ‘0’
In the ‘Response Transformations’ section, the below details are entered.
Response JSON Path: ‘ContactCount’
Response JSON Node: ‘CountResult’
Now, a Loop Block (‘LoopBlockContacts’) element is added.
In the ‘Loop List’ field, the array to be iterated has to be added.
In our example, the ‘DRExtractContacts:Contacts’ node holds the Contacts array.
Based on our needs, we can add any actions inside the Loop Block. This will ensure that the actions will be executed for each element in the array. For best practice, we should not add any actions that will fetch data or trigger DML updates inside the Loop Block. For example, avoid using the Data Raptor Extract action, the Data Raptor Turbo Extract action, and the Data Raptor Load action inside the Loop Block.
This is to ensure that governor limits are not exceeded.
Inside the Loop Block, four Set Value actions are added, as below.
SVConcatContacts
SVCount
SVPrimaryContactName
SVPrimaryContactEmail
SVConcatContacts:
This action has a formula that concatenates the contact names from the Contacts array, separated by a comma (‘,’).
In the Element Value Map, a new element is added with the below details.
Element Name: ‘ContactNames’Value: CONCAT(%ContactsResult%, ', ', %DRExtractContacts:Contacts:Name%)
Either of the below formulas can be used for concatenation.
1. using CONCAT function
CONCAT(%ContactsResult%, ', ', %DRExtractContacts:Contacts:Name%)
2. using Add ‘+’ operator
%ContactsResult% + ", " + %DRExtractContacts:Contacts:Name%
Here,
‘%ContactsResult%’ is the SV action element initialized to blank value.
‘%DRExtractContacts:Contacts:Name%’ holds the name value of each contact element in the ‘Contacts’ array.
In the ‘Response Transformations’ section, the below details are entered.
Response JSON Path: ‘ContactNames’
Response JSON Node: ‘ContactsResult’
NOTE: The ‘Response JSON Node’ value should match the SV action element value (‘ContactsResult’) used in the function.
So now, for each iteration of the Loop block, the corresponding contact name gets appended and finally holds the concatenated string of all the contact names.
SVCount:
This action calculates the count of contacts.
Here, ‘%CountResult%’ is the SV action element initialized to value ‘0’.For each iteration of the Loop Block, the value will be incremented by ‘1’ and finally hold the count of contacts.
SVPrimaryContactName:
This action gets executed conditionally and holds the name of the ‘Primary Contact’ linked with the account.
Here, ‘%DRExtractContacts:Contacts:IsPrimaryContact%’ in the ‘Execution Conditional Formula’ allows the SV action to get executed only for the contact record that has the ‘IsPrimaryContact’ value set to ‘true’ and stores the corresponding Contact’s name in the ‘PrimaryContactName’ element.
So, now when the Loop Block executes, this SV action will not be executed for the non-primary contacts in the ‘Contacts’ array.
SVPrimaryContactEmail:
This action gets executed conditionally and holds the email address of the ‘Primary Contact’ linked with the account.
Now another Set Value (‘SVTrimComma’) action is added outside of the Loop Block.
SVTrimComma:
This action has a formula that removes the extra comma and space from the concatenated ‘Contact Names’ value.
Finally, a Response Action (‘RAContactResponse’) is added, and the values are mapped in the ‘Additional Output’ section as shown below.
Output:
Now, when the IP is executed, the following response is generated.
Here,
‘ContactsLinkedWithAccount’ - holds the concatenated ‘contact names’ string separated by a comma.
‘CountOfContacts' - contains the number of contacts linked to the account.
‘PrimaryContactName’ - holds the name of the Account’s primary contact.
‘PrimaryContactEmail’ - holds the email address of the Account’s primary contact.
References
https://help.salesforce.com/s/articleView?id=sf.os_process_arrays_using_loop_blocks_49200.htm&type=5