Add records to a table within a table

<< Click to Display Table of Contents >>

Navigation:  Low-code Process Automation > Studio Cloud - Authoring environment > Bizagi Studio > Process wizard > Business Rules > Business Rules examples > Collections and entities > Collection examples >

Add records to a table within a table

Imagine a Purchase Request process where by default one product is added to the request when a case is started and some suppliers are added to the product.

Thus, a record of the table Products is created and then a record to the table Suppliers within Products is added.

 

There is a one-to-many relationship between the Process Entity PurchaseRequest and the collection entity ProductsRequested.

There is a one-to-many relationship between the Entity ProductsRequested and the collection entity PossibleSuppliers.

The the one is called PurchaseRequest.

The collection entity is called ProductsRequested.

The second collection is called PossibleSuppliers

 

addrelation9

 

An expression is created On Enter of the Purchase Request process to add products and suppliers.

 

The following is the list of the variables created for the expression.

 

addrelation10

 

The first thing to do is to add the Products, that is, the external table:

 

addrelation11

 

var parameters = new FilterParameters();

parameters.AddParameter("@Code", "1");

ProdType = CHelper.getEntityAttrib("Producttype","idProducttype","Code = @Code", parameters);

Product = Me.newCollectionItem("PurchaseRequest.ProductsRequested")

Product.setXPath("Producttype",ProdType);

Product.setXPath("Quantity",10);

 

Once created, go use an Iterate Over XPath module to go through each Product and add the records of the second level table within.

 

 

addrelation12

 

Finally we add the Supplier records to each record of the Products collection.

The Me.newCollectionItem changes in the first variable to the Iterate variable.

 

addrelation13

 

Supplier = Me.getXPath("entity-list('Supplier', '')");

for(var i=0;i<Supplier.size();i++)

{

 NewSupplier= NewProducts.newCollectionItem("Possiblesuppliers");

 NewSupplier.setXPath("Offeredunitprice",10);

 NewSupplier.setXPath("Supplier",Supplier[i].getXPath("Id"));

}

 

Notice that you can also obtain the id with the name of the primary key (surrogate key)

for(var i=0;i<Supplier.size();i++)

{

 NewSupplier= NewProducts.newCollectionItem("Possiblesuppliers");

 NewSupplier.setXPath("Offeredunitprice",10);

 NewSupplier.setXPath("Supplier",Supplier[i].getXPath("idSupplier"));

}

 

Option 2

Another way to add records to a table within a table is by using an newCollectionItem over a variable that contains another newCollectionItem. This way we do not need to use an iterate over XPath.

 

An expression is created On Enter of the Purchase Request process to add products and suppliers.

The following is the list of the variables created for the expression.

 

addrelation14

 

The first thing to do is to add the Product, that is, the record of the external table. Use the Me.newCollectionItem method.

 

addrelation15

 

Now from the Supplier parameter entity obtain the list of suppliers to be added to the second level (internal) table.

 

addrelation16

 

Go through the list of suppliers and create a record in the Possible suppliers table for each supplier.

Note Me.newCollectionItem changes in the first variable to the related to the external table. (Product.newCollectionItem)

 

addrelation17

 

//Get the product from the parameter entity

var parameters = new FilterParameters();

parameters.AddParameter("@Code", "1");

ProdType = CHelper.getEntityAttrib("Producttype","idProducttype","Code = @Code", parameters);

//Add the product to the first level table

Product = Me.newCollectionItem("PurchaseRequest.ProductsRequested");

Product.setXPath("Producttype",ProdType);

Product.setXPath("Quantity",10);

 

//Get the suppliers from the parameter entity

Supplier = Me.getXPath("entity-list('Supplier', '')");

 

//For each supplier

for(var i=0;i<Supplier.size();i++)

{

//Add the supplier to the second level table

NewSupplier= Product.newCollectionItem("Possiblesuppliers");

NewSupplier.setXPath("Supplier",Supplier[i].getXPath("Id");

}

 

In here, you can also access the id using the primary key.

NewSupplier= Product.newCollectionItem("Possiblesuppliers");

NewSupplier.setXPath("Supplier",Supplier[i].getXPath("idSupplier");

}

 

note_pin

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.

 

Example:

 

var max = 1500;
var PEntity = Me.getXPath("ProcessEntity");
for (var x = 0; x < max; x++)
{
      var Newrecord = PEntity.newCollectionItem("RelationshipToTheCollection");
      Newrecord.setXPath("Column","Value");
}


Last Updated 1/6/2022 4:15:27 PM