How to Repeat Screen in Lightning Flow Designer

Have you ever came across a scenario where you want to repeat a screen in Lightning Flows?
You might be wondering we have Loop element for that, right? NO! The loop element won't work in this scenario because the Loop is used to iterate over items in a collection variable and not the screens. 

So how will you achieve this functionality?
Well, we have a solution for this. The Decision and Assignment Elements in Lightning Flows if combined together, they can repeat or call the same screen again and again, just like in recursion.

Scenario: A user wants to create an Account and 'n' number of Contacts related to that Account. All this should be in one flow like a Quick Account/Contact Creation.

Solution:
We can achieve this by creating a for loop kind structure in Lightning Flows.
for (initializationStatement; testExpression; updateStatement)
{
    // Create Contact record
}
Was that confusing??   Yeah!!
Well, don't worry, if you are not from a technical background or cannot relate the for loop with the flows, because we are going to work on this tiny project step by step.

1. First of all, ask the User, how many contacts does he wants to create? (This many times you will show the same screen)
Get that input in a 'Number' component(repeat_contact_screen) of the Lightning Flow and set the default value equal to 0.
(You can also give validations like negative numbers not allowed)

User 'Number' component to get 'n' inputs
User 'Number' component to get 'n' inputs


2. We will create an Integer variable(compareVariable) so that we can compare it with the original number(repeat_contact_screen)
Set the 'Default value' of this new number to 0 and also change the 'Decimal Places' to 0. 

Create a new variable to compare
Create a new variable to compare

3. In the next step, we want to check whether the value of repeat_contact_screen is greater than compareVariable. If the value is greater, which means that User wants to create 'n' number of Contacts, then we will run the Logic: Decision Element.
This logic will run until the value of 'repeat_contact_screen' is greater than 'compareVariable'


Logic to input Test variables
Logic to input Test variables

4. In the final step, we have to increase the count of compareVariable, or else it will get stuck in the loop forever.
So, create an Assignment and add '1' to compareVariable.

Assignment to Increase the Count
Assignment to Increase the Count

5. In between the Decision and Assignment, you can play with your Business Logic. You can even get creative and Bulkify the Flow with the Loop Element and after the Decision is complete you can create all the records at a time.

Lightning Flow Structure
Lightning Flow Structure


Comments

Popular posts from this blog

Salesforce Integration with Foreign Exchange Rates API for Currency Conversion

Administrator Certification Maintenance (Spring '21) Questions

Salesforce App Builder - Question Set 5