Iterate Multiple Picklist Values (for each list value)



Description

 

The deluge language now supports a second level of iteration to iterate through multiple list values in a row. For example, if there are n rows in a form and for each row there are m values in a multiple list field, you can iterate through the m values in the list within each row.

 

Syntax

for each <fieldname> <variable> in <formname/rowvariable> [criteria]

 

where,

 

Example - Free Flow Scripting

 

Visualize a simple Sports Registration application. It has two forms.

1. The form Sport_Coaching_Details has the following fields.

 

2. The form Sports_Registration has the following fields.

 

Both the forms are related since there is a import data field between them. So it is possible to get the Sports details from the Sport_Coaching_Details form for any given sport in Sports_Registration form.

 

 

For each Sport registered, we have to check if the age specified is within the agelimit prescribed for that sport as specified in form Sport_Coaching_Details. If so, an email is sent with the message that the registration for that sport is accepted. The following code in the on success event block does this.

 

Deluge code snippet

 

    on success
    {
        if (count(Sports_Registration)  >=  15)
        {
            // iterates through each row in the Sports_Regitration form
            for each r in Sports_Registration
            {
             //iterates through multiple values specified for the Sports field in the row r
             for each Sports x in r
                {
                    if ((r.Age  > x.Minimum_Age)  && (r.Age  < x.Maximum_Age))
                    {
                        sendmail
                        (
                            To       :  r.Emailid 
                            Subject    :  "Registration for Sports coaching from " + r.Name 
                            Message  :  "Your registration for" + x.Sport_Name + "is accepted" 
                        )
                    }
                    
                }
            }
        }
    }

where,

 

Deluge code explanation

 

1. Checks whether the total registration is greater than or equal to 15. Here, Sports_Registration is the name of the form.

 

if (count(Sports_Registration)  >=  15)

 

2. Iterates through each row in the Sports_Registration form. Here, r is the name of the row variable that contains the data for a single record corresponding to each iteration.

 

for each r in Sports_Registration

 

3. Iterates through multiple values specified for the Sports field in the row r. Here, Sports is the name of the field in the Sports_Registration form whose values has to be iterated one by one and x is the name of the variable that will hold the data corresponding to each iteration of the Sports field.

 

for each Sports x in r

 

4. Checks whether the Age specified in the Sports_Registration form, is within the Minimum_Age and Maximum_Age limit specified for this sport in the Sports_Coaching_Details form.

 

- r.Age fetches the Age value from the row variable r in Sports_Registration form.

- x.Minimum_age will fetch the Minimum_age for this Sport from the Sports_Coaching_Details form.

 

if ((r.Age  > x.Minimum_Age)  && (r.Age  < x.Maximum_Age))

 

5. The sendmail function is called to send an email to the Emailid specifed in row r.

 

sendmail
                        (
                            To       :  r.Emailid 
                            Subject    :  "Registration for Sports coaching from " + r.Name 
                            Message  :  "Your registration for" + x.Sport_Name + "is accepted" 
                        )

 

Example : Using Script Builder

 

Let us add the on success action script for the Sports Registration Example using Script Builder. The following deluge features are used in this example:

1. Select On Success Form Action

a. Select the form Sports_Registration from the list of forms displayed in the top left corner of the script editor.

b. Select Form Actions -> On Success. Sample form actions will be displayed in the editor area. Delete the sample statements.

2. for each record

a. Select Control Flow -> For each record from the left-side tree and drag and drop it in the editor area. The syntax to iterate each row in a form will be displayed as shown in the screen-shot below.

b. Select Edit option to specify the row variable and criteria, if any.

 

 

f. The 'for each record' Edit dialog will be displayed as shown in the screen-shot given below:

 

The deluge statement to iterate each row r in the form "Sports_Registration" is added to the script editor, as shown below:

 

 

2. for each list value: Now we have to iterate through multiple values specified for the Sports field within each row r. To do this,

a. Drag and drop Control flow -> for each list value to the text editor area. The syntax is displayed as shown in the screen-shot given below. Click on Edit to specify the required details.

 

 

b. Select the row variable r that contains the data for a single record for each iteration in Sport_Registration.

c. Select the form field Sports in the above row variable r, whose values has to be iterated one by one.

d. Declare a user-defined variable name x, that will hold the data corresponding to each iteration of the Sports field in r.
e. Click on Done to add the for each syntax to the script editor.

 

 

Note: When a formvariable/collectionvariable is selected instead of formname, the dialog does not display the criteria text box.

 

The deluge for each list value is added to the script editor, as shown below:

 

 

3. If condition:

a. Drag and drop the If control flow statement, as highlighted in the screen-shot below. Click on Edit to specify the If condition.

 

 

b. The Edit dialog for If is displayed as shown below, wherein you have to create the If condition. In our example, we have to check if the Age specified in Sports_Registration is within the maximum and minimum age limit specified in the Sports_Coaching form.

 

4. Send mail : Now, we have to add the If statements to be executed, when the If condition is satisfied. In our example, we have to send a mail to the email id specified in the Sports_Registration form if the condition is satisfied.

a. Drag and drop the Send mail function to the editor area, as highlighted in the screen-shot below. Refer the Send mail topic, for more information on the syntax and usage.

 

 

b. Click on Edit to specify the To address, Subject and Message for Send mail. The Edit dialog displays the default values. To modify the default values, click on the icon on the right-side of each field. For example, to send an email to the Emailid specifed in row r, select the Emailid field from the collection variable r.

 

Note: The email id of the application owner will be taken as the From Address.

 

 

 

c. The send mail script is added to the script editor as shown below. Click Save Script to add the action script to the script definition.

 

The above action script will send an acceptence email to the registered persons if the age specified in "Sports_Registration" is within the age limit prescribed for that sport as specified in form Sport_Coaching_Details.