Auto-generate a string which counts number of containers?

Beginner to Bonita and junior in terms of js skills, so please over-explain (thanks in advance). 

I want to generate two strings (ID codes), 


First I want to have a string output which details a project code (given by form input) + the number of populated containers in the form. "PROJCODE-#CONTAINERS". 

I have the following as a js variable:

 

var pC = String($data.formInput.productionOrderInput.projectCode)
var qty = String($data.formInput.productionOrderInput.panelInformation.length) 
// where panelInformation is the list which stores container inputs

return masterMO == pC + qty;
// how to output this to the form? 

 

I then place the name of the js var (also masterMO) in the 'value' setting for the form input where I want it to be displayed, but I get no output. Where am I missing something? 


The second string output details the same project code and another ID code given by form input within the container. This needs to be generated and output within every container. Here, again, I could really do with advice/guidance as to how to output the codes onto the form so that they display automatically. 

 

var pC = String($data.formInput.projectCode)
var pT = String($item.panelNameP)
var pS = String($item.panelSizeP)
var setQ = String($item.qtyP)

return subMO == pC + pT + pS + setQ;

For answers, if you could explain why you have offered the suggestions to help me understand how it all works that would be super helpful! Thank you

Hi,

// This will return a boolean value. return masterMO == pC + qty; // This will return a String. // String interpolation in javascript is a powerfull tool to build strings return `${$data.formInput.productionOrderInput.projectCode}- ${$data.formInput.productionOrderInput.panelInformation.length}`;

If the above expression is a variable named masterMO then you can print its value in your form using a Text widget and use an AngularJs interpolation {{masterMO}} in the Text property.
This variable can also be used as a value in an Input widget.

For the second part, you can use a Text widget and set the following expression in the Text property

{{formInput.projectCode}}- {{$item.panelNameP}}-{{$item.panelSizeP}}-{{$item.qty}}

If this string must be stored in variable, it is a little more complicated as there is not container scope variable, so javascript expression doesn’t handle the $item and $index variables.
The workaround is to use the list pass to the container (panelInformation in your case if understood well), and, assuming that panelInformation is an array of objects, store the result there.
To do this, you can create a Javascript variable that returns a function like storeContainerId

return function(item, index){ const containerId = `${$data.formInput.projectCode}- ${item.panelNameP}-${item.panelSizeP}-${item.qty}` $data.formInput..productionOrderInput.panelInformation[index].containerId = containerId; return containerId ; };

And in the Text widget, instead of the angularJs interpolation, use:

{{storeContainerId($item, $index}}

HTH
Romain

Thank you so much for the help and the details, this is so informative.