Sum of items

Actually I use Bonita Studio Community 7.5.4

Context:

I have created a form where I have a collection with a field called subtotal, therefore for each new collection another subtotal field is added so that they can be from 1 to 10,100 or 1000.

Problem:

How can I add up all the subtotals that are generated?

I think the best way to do this is in a script because I need the total value to meet a condition on a gateway or if it is possible to do it from the form, could you give me ideas on how to do it!

Thanks in advanced!

 

Hi Claudia,

Assuming you have in your form a variable invoice with following structure

{
“customer”:“Customer 1”,
“invoiceLines”:[
{
“product”:“product 1”,
“quantity”:3
“unitCost”:22
},
{
“product”:“product 2”,
“quantity”:5
“unitCost”:7
}
]
}

You can have an JS expression that does the math for subtotal and total of the invoice

if($data.invoice && $data.invoice.invoiceLines && $data.invoice.invoiceLines.length>0){ var total =0; $data.invoice.invoiceLines.forEach(function(item){ var subtotal = item.quantity * item.unitCost; item.subtotal = subtotal; total += subtotal; }); $data.invoice.total = total; }

Hi Claudia,

Could you share the form? it will be easier

Any case, it is a matter of set the right variable names on the JS expression I proposed. Does not matter if it is the form created automatically or where the form is attached (task or process instantiation)

 

Cheers 

Hi Pablo thanks for answering!

So the form is created automatically with the contract for this reason my unique variables is formInput and forOutput.

also it is the firts form to start a process the variable JS doesn't have problem with this? and how add the value in the formInput? 

The structure of de formInput is somethig like that

JSON

{attach: null,

BDM variable:{
some variable of the contract : "",
Table:[{ subTotal}]
more variables: "",
total: ""
}

}