The For element offers the possibility to include a cycle to be executed a specified number of times. The initial value of a variable will increase by a set amount on each iteration. The cycle will executed as long as a Loop Condition is met.
The For window has four requirements:
•The Loop Initialization indicates the loop control variable which is used to regulate the number of times the loop is executed.
•The initial value of the loop control variable is set equal to a constant value in the following field. The initial value can be set to any number which fulfills your purpose; the default is zero (0).
•The Loop Increase / Decrease indicates the amount of which the variable is going to increase/decrease in each of the iterations. The variable may be increased or decreased by any amount that fits your purpose. The default is an increment of one (1).
•The Loop Condition is evaluated at the end of every loop. The loop continues to iterate while the condition is true. If the condition is NOT met (is false), the loop finishes and the workflow continues to the next element on the Activity diagram.
What you need to do in Bizagi
1. Create a scripting expression.
2. Declare a variable to store all the elements of the collection.
3. Create a For element to include the logic of the cycle until a condition is true.
4. Within the For loop establish its characteristics to perform the calculations required.
5. Save the rule.
Suppose you work in a bank where you perform an initial appraisal of the applicant to see if he/she is eligible for the financial products requested.
If the applicant passes this first assessment, the bank will request several documents from the customer to support the data presented. There is a parameter entity, Applicant Document, that contains a list of all documents that the bank may require based to the products requested. Therefore, it is necessary to examine the list and extract only the documents that apply. These documents will be related to a Table in the Case.
The following is the Data Model of the example, where parameter entities are in green and master entities in blue:
1. Create a rule On Enter of the Activity where the document list will be displayed.
2. Declare the variables that we will use.
•FilterDocsXApplicant (object): The filter to find the documents in the parameter table that apply to the specific Case.
•DocsXApplicant (object): The loop iteration variable storing the specific documents selected from the parameter table. This variable will be filtered by FilterDocsXApplicant.
•Count (int): The looping variable. It starts at zero and increments by one for each loop.
•IdDocument (int): A variable storing the Document unique identifier that is points to the current record in the DocsXApplicant collection.
•ApplicantDocument (object): A result variable that will store the selected documents that will be added one by one to the collection of the Case.
3. Include an expression element to obtain the records of the parameter entity Document per Applicant, that apply to the Case.
//Store the filter in a variable, according to the applicant's role and the income source
FilterDocsXApplicant = "idApplicantRole = " + <idApplicant.idApplicantRole.id>
+ " and IncomeSource = " + <idApplicant.IncomeSource.id>;
//Store the records of the Parameter entity that comply with the filter in a variable
DocsXApplicant = Me.getXPath("entity-list('DocumentXApplicant', '" + FilterDocsXApplicant + "')");
4. Include the For element.
Select the variable declared and initialize it to zero, so the starting point of storage will be the first record. This will make sure that the loop starts at the first record of the DocsxApplicant variable.
The variable will be increased by one, to add each record of DocsXApplicant to the collection ApplicantDocument.
Create the condition: this loop will be executed as long as the DocsXApplicant variable still contains records to add to the collection.
Count < DocsXApplicant.size()
5. Include an assignment element to add the records to the collection of Documents.
//Store the Document identifier that points to the current record in DocsXApplicant collection (loop iteration variable).
IdDocument = DocsXApplicant[Count].getXPath("idDocument");
//Adds an element (a new record) to the Applicant Document entity (one-to-many relationship).
ApplicantDocument = Me.newCollectionItem("idApplicant.Documents");
//Sets the Document identifier of the newly created record.
//Set the Required attribute (True or False) of the newly created record.
Note that the variable ApplicantDocument is used to copy values from the Document per Applicant parameter entity to the Applicant Document entity.
The expression adds each record individually, iterating the parameter entity.
Variable.setXPath("attribute within the collection", value to set);