How to execute loop # of times based on user input on screen using Lightning flow

on

|

views

and

comments

How to execute loop # of times based on user input on screen using Lightning flow

Use Case:

There are many situations where we need to run the loop in flow builder N number of times based on user input on screen element. For e.g. if user provide the input 3 on screen element then Loop should be executed 3 times. 

As we know, stopping condition for Loop logic in Flow builder is either Collection of Strings or Record ids. We cannot add integer as a stopping condition like Apex:

For(i=1;i<=3;i++){
   //Custom logic Here
}

If user is entering 3 on screen elements, then we need to convert this integer to String (3 => 1,2,3) first. We cannot get easily this String to set collection variable for Loop. To get this String, we have powerful feature provided by Action interaction element in FLOW BUILDER. In Action, we can call the apex action type and use InvocableMethod Annotation Apex method. This method will convert the integer to String.

Logic:

First, we require some resources to build our logic.

Asset_Information_Screen -> This Screen will be used to gather information from the user based on business need.

Total_Assets_having_issue -> Screen Component of type Number or Picklist field (Type number). If you are using then you need to define the choice as well Like 1,2,3,4,5 And So on based on business requirement.

TotalIssueAsset ->Collection Variable of type Text. This will be used to store the output of Invocable method.

TotalIssueAssetLoopVar -> Text variable. This will be used as Loop variable in loop.

Apex_Action_to_convert_IntegerToString ->This Apex action will be used to convert integer to String. Based on this String our loop will be executed number of times.

In Apex Action, we need to define variables “Set input Values” i.e. entered by the user input on screen element and “Store Output Values” that will be returned by Invocable apex method as String.

Loop_for_No_of_Assets_Selected ->Loop logic to create cases based on user input.

Create_Case -> Create Record Data element that will create a case for our requirement.

Thanks_Screen -> Screen component to show the total count of cases created using this flow.

Invocable Class

global with sharing class ConvertIntegerToString {
     
    
     @InvocableMethod
     public static List<Results> GetCount (List<Requests> requests)
     {   
         Requests OBJRequests = requests[0];
         Integer i = OBJRequests.RequestInt;
         list<String> lstint = new list<String>();
         for(integer k=1 ; k<=i ; k++){
             lstint.add(String.valueof(k));
         }
                
         List<Results> resultsList = new List<results>();
         Results curResult = new Results();
         curResult.RequestResult = lstint; 
         resultsList.add(curResult);
         return resultsList;
     }
     
     global class Requests {
         @InvocableVariable
         global Integer RequestInt;
     }
     
     global class Results {
         @InvocableVariable
         global list<String> RequestResult;
     }
 }

Final Flow:

Here is the flow how look at the end. You can include this flow on Account or on custom object as a quick action.

Note: Apex is not supported in FSL mobile flow. You need to find other way to achieve this functionality. If you are using this flow in Quick action, then you need to define recordId variable to initialize the flow.

If you have any suggestions then you are most welcome 🙂

For any query please reach out to us @ sfdcpanther@gmail.com OR sfdcpanther@outlook.com OR you can dm US at @cloudyamit or @sfdc_panther

References:

Share this

Leave a review

Excellent

SUBSCRIBE-US

Book a 1:1 Call

Must-read

How to start your AI Journey?

Table of Contents Introduction Are you tired of the same old world? Do you dream of painting landscapes with your code, composing symphonies with data, or...

The Secret Weapon: Prompt Engineering: 🪄

Table of Contents Introduction Crafting the perfect prompt is like whispering secret instructions to your AI muse. But there's no one-size-fits-all approach! Exploring different types of...

How to Singup for your own Production org?

Let's see how we can have our salesforce enterprise Salesforce Org for a 30-day Trial and use it as a Production environment. With the...

Recent articles

More like this

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

5/5

Stuck in coding limbo?

Our courses unlock your tech potential