<< Click to Display Table of Contents >> Iterate over fact element |
Overview
Iterate over Fact allows you to carry out iterations (or cycles) over a one-to-many relationship ONLY when the relationship cannot be accessed directly through the data model using XPath.
You can access each record of the collection individually to carry out operations such as modifying its values, getting its values or performing calculations and validations with them.
The collection you iterate can be filtered so you access specific records to work with them.
What you need to do in Bizagi
1. Create a scripting expression
2. Declare the following variables:
- A variable to store all the elements of the collection
- A variable to store each record individually as the iteration takes place
- A counting variable to control the cycle
3. Using an Expression element get the collection desired and store it in the first variable
4. Include an Iterate over Fact element defining: the One entity, the Fact (which is the relationship's name between the One entity and the Many entity) and the Key (which is the identifier that will get the record of the One entity to identify the records of the Many entity to be iterated)
5. Within the iteration cycle include an expression element to perform the calculations required.
6 Save the rule.
Example
Imagine a school that is organizing all activities at the start of the new school year.
Bizagi allows the administrative staff to include as parameter entities information about the new year such as supply lists, the grades covered by the school, the teachers, their respective subjects and the grades they will be teaching, etc.
The following is the data model of our Process. Note that teachers are created in the WFUser entity. We assume that they teach one subject but they can teach many grades.
The main objective of the Back to School Process is to retrieve all the data of a specific grade and hand this information to parents and students.
The Process has two activities: one to enter the information for the grade and another to display and print it.
We will use the Iterate over Fact element to iterate over the teachers and select those allocated to the given grade. Remember this information is stored in the System Entity WFUser. Consequently, there is no specific XPath at the beginning of a case to know which teachers are assigned. The information has to be searched and then saved in the data model of the case.
1. Create a rule On Enter of the Activity where the information will be displayed.
2. Declare the variables to use:
•Grade: the grade to retrieve information from
•AddTeacher: variable to add every teacher to the Case XPath
•Teachers: variable to store the information of all teachers in the WFUser
•Counter: variable to control the cycle
•TeachersLength: the size of the Teachers collection
•IdUser: variable to store the key to access the WFUser
•GradesToIterate: variable that will access each record individually
3. Using an Expression element to retrieve the collection and store it in the Teachers collection variable.
Save the Teacher collection's length and grade in the respective variables.
//Remove all the items stored in the collection to start from a blank one
Me.deleteAllCollectionItems("Backtoschool.Teachers");
//Store all WFUser teachers found in the Teachers variable
var parameters = new FilterParameters();
parameters.AddParameter("@IsTeacher", true);
Teachers = Me.getXPath("entity-list('WFUser', 'IsTeacher=@IsTeacher')",parameters);
//Store the size of the Teachers collection in the TeachersLength variable
TeachersLength = Teachers.size();
//Store the grade for which to retrieve information
Grade=<Backtoschool.Grade.Id>;
4. We will include a For element to cycle the WFUser entity and obtain each idUser (our key) to be used in the Iterate over Fact element.
5. Within the For element we obtain the idUser
6. Include an Iterate over Fact element defining:
- The variable for iteration: GradesToIterate
- The One entity: WFUser
- The Fact : Grades
- The Key: idUser (which is the identifier that will get the record of the One entity to identify the records of the Many entity to be iterated)
5. Within the iteration cycle include an expression element to perform the calculations required.
//Compare the grade each teacher is assigned to with the grade of the Case
var idGrade = GradesToIterate.SurrogateKeyValue;
if(idGrade==Grade)
{
//adds an element (a new record) to the Backtoschool- Teacher collection. (one-to-many relationship)
AddTeacher = Me.newCollectionItem("Backtoschool.Teachers");
//sets the value of the ID in WFUser to the new record of Teacher just created.
AddTeacher.setXPath("Teacher",idUser);
}
We compare the grade each teacher is assigned to with the grade of the Case. If it is the same one, then the teacher will be teaching the selected grade and thus, it is added to the Teacher's collection XPath for the Case.
6. Save the rule.
Last Updated 1/6/2022 11:36:28 AM