Access the i-th record of a collection

<< Click to Display Table of Contents >>

Navigation:  Bizagi Studio > Process wizard > Business Rules > Business rules examples > 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)

 

 

note_pin

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:

 

 

AccessSpecificRecord1

 

 

 

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.

 

AccessSpecificRecord2

 

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.

 

AccessSpecificRecord3

 

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

WinnerIndex=<Raffle.WinnerIndex>;

 

//Obtain the clients collection

Clients=Me.getXPath("Raffle.Clients");

 

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

<Raffle.WinnerName>=Clients.get(WinnerIndex).getXPath("Name");

 

 

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

 

AccessSpecificRecord4

 

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

Clients.get(WinnerIndex).setXPath("Winner",true);

 

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

 

 

AccessSpecificRecord5