Access the i-th record of a collection

<< Click to Display Table of Contents >>

Navigation:  Bizagi Studio > Process wizard > Business Rules > Business Rules examples > Collections and entities > Collection examples >

Access the i-th record of a collection

In some situations it is necessary to obtain or manipulate information of a specific record in a collection.

To do this you do not need to iterate over the whole collection and stop when you find the desired record. By knowing the position of the record in the collection you can easily access it through an expression.


The syntax used to access this information is as follow:


To obtain information


Collection.get(index of the record). getXPath("Attribute")

To set


Collection.get(index of the record). setXPath("Attribute",value)



Take into account that indexes of the records within the collection always start from 0.


Suppose a company uses Bizagi to raffle different prizes among its clients. An external service generates a random number which represents the position of the client within the collection of clients. As the number of clients can be very large, the name of the winner is stored in a string type attribute. Once obtained this information, the client is checked as winner in the data base in order to exclude him/her for future raffles.


The following is the data model associated to the process:




To obtain the information of the winner from the clients collection, follow the next steps:


1. Go to the activity where the raffle is performed and create an on enter expression. Then create an expression module.




2. The random number generated by the external service is stored in the WinnerIndex attribute. Type the following expression to obtain the name of the winner from the clients collection. It will be stored in the WinnerName attribute.




//Obtain the index of the winner within the clients collection



//Obtain the clients collection



//Obtain the name of the winner by using the clients index within the clients collection



You can also obtain the id of the record currently being iterated.


//Obtain the name of the winner by using the id



//Obtain the name of the winner by using the surrogate key




The surrogate key is built concatenating "id", with the Entity's name (e.g. idClients)


3. Now to identify the client as a winner use the following sentence. This will set the Winner attribute to true.




//Update the information of the winner by using the clients index within the clients collection



4. Go to the Work Portal to test the expression.





In order to have a better performance, when manipulating data inside a cycle, before entering the cycle extract in a variable the Process entity and then use the variable inside the cycle.




var max = 1500;
var PEntity = Me.getXPath("ProcessEntity");
for (var x = 0; x < max; x++)
      var Record = PEntity.get(0).getXPath("Attribute");