Iterate over XPath element
Iterate over XPath element
Overview
Iterate over XPath allows you to carry out iterations (or cycles) over a collection XPath, that is, a one-to-many relationship.
You can access each record of the collection individually to carry out operations such as: modify values, retrieve values, or perform calculations and validations with values.
The collection you iterate can be filtered to only retrieve specific records.
What you need to do in Bizagi
- Create a Scripting expression.
- Declare a variable to store the elements of the collection.
- Include an Iterate over XPath element defining the collection XPath to be iterated, including filters if required.
- Within the iteration cycle include an expression element to perform the calculations required.
- Save the rule.
Example
In the following scenario, you work in the loan department of a bank. Two applicants respectively request analysis of their borrowing capability to ascertain they qualify for a loan. The bank performs a financial analysis and assesses the applicant's repayment capacity.
The Process entity for this process is Request. Each request can have multiple Applicants; thus, there is a one-to-many relationship between Request and Applicants. Additionally, each applicant will have a Customer Snapshot, to keep a record of the applicant's financial capability at the time of the request.
A rule must evaluate each applicant based on preliminary information, namely the base income and variable income of the application. The rule will calculate the total income from these figures.
-
Create a rule On Exit of the Activity where the data is entered.
-
Declare the following variables:
- Applicant (object): To store the collection records.
- MonthlyBaseIncome (int): To hold the base income value for future calculation.
- MonthlyVarIncome (int): To hold the variable income value for future calculation.
- TotalIncome (int): To store the total of both incomes.
-
Include the Iterate over XPath element.
- Select the Applicant variable declared earlier to store the collection records.
- Select the XPath collection. In this case, we select
Request.Applicants
.
- Include an expression element to calculate the total income.
Enter the following code in the Edit Expression window:
MonthlyBaseIncome = Applicant.getXPath("CustomerSnapshot.MonthlyBaseIncome");
MonthlyVarIncome = Applicant.getXPath("CustomerSnapshot.MonthlyVariableIncome");
TotalIncome = 0;
if (!BAIsBlank(MonthlyBaseIncome))
TotalIncome = TotalIncome + MonthlyBaseIncome;
if (!BAIsBlank(MonthlyVarIncome)) TotalIncome = TotalIncome + MonthlyVarIncome;
Applicant.setXPath("CustomerSnapshot.MonthlyTotalIncome", TotalIncome);
Notes:
The object variable, Applicant, is used to get and set values to each record individually.
Variable.getXPath("attribute within the collection");
Variable.setXPath("attribute within the collection", value to set);
If the XPath collection has been filtered, the iteration would only affect the records that comply with the filter.