Iterate a set of code n times | Creator Simplified

Iterate a set of code n times | Creator Simplified

Hey Creators—we're back with another edition of Creator Simplified!


Objective

Generate subform rows with serial numbers based on user input in a field.

Use case

Assume an event is being organized in an organization, and HR needs the list of participants from each team. When the team leader inputs the number of participants on the event registration form, the specified number of subform rows are generated along with serial numbers.





How to achieve this

1. Create a form named Event Details. Add relevant fields and a number field named No.of Participants




2. Add a subform (either a blank form or an existing form) named Participant Details, with necessary fields to collect participant information.



3. Configure a form workflow for the Event Details form on user input of the No.of Participants field. 




4. Copy and paste the code given below. The code has been broken down for easy understanding. 
 

  1. //Run the action when no.of participants are mentioned

  2.    if(input.No_Of_Participants != null)
  3. {

  4.  //Clear existing data  
  5.             input.Participant_Details.clear();

To iterate n number of records, we need to create a list with dynamic values. Since we cannot directly create a dynamic list, here's a workaround:

First, take a string value; let's say "t". Next, use the leftpad() function to return the text "t" left-padded with white spaces that together equates to the count of participants entered.

Next, replace each whitespace with "t,". The comma separates each replaced t and helps convert it into a list. Finally, use the toList() function to convert all these values to a list. 


  1. //Create a dynamic list with number of participants mentioned 

  2.          no_of_participants = "t".leftpad(input.No_Of_Participants).replaceAll(" ","t,").toList();

Create a collection variable to store subform data, then iterate through the list of participants using a for loop to create, serialize, and insert subform rows into the collection variable.


  1.  col = Collection();
  2.  sl = 1;

  3.  for each  participant_rec in no_of_participants 

  4.  {
  5.   rows = Event_Details.Participant_Details();
  6.   rows.Sl_No=sl;
  7.   col.insert(rows);
  8.   sl = sl + 1; 
  9.  }

  10.  input.Participant_Details.insert(col);
  11. }


And that's all for this post! We hope this workaround helps you quickly resolve your real-time requirements.

Feel free to share your questions, thoughts, or suggestions in the comment box.

Keep an eye on this space for more useful posts in the future!