How to define formulas for computed fields

This article explains how administrators can define formulas to be used in their custom fields

Under administrator settings, an administrator can define a formula (as a JavaScript code) that can be used for computed custom fields.

Please note that formulas are only accessible to advanced users, as an incorrect formula definition could potentially cause issues with their model. For any question, reach us at support@pilario.com.

While creating a formula, and administrator can define the following attributes:

Under the general section, a user can define the following attributes

  • Name: the name of the formula. When applicable, it is a good practice to prepend the model where it will be used.
  • Notes: used to add a description of what the formula does. Will be displayed in a tooltip to help administrators identify the correct formula when using it in the computed fields. This is a non mandatory attribute.
  • Value: a JavaScript code containing the model formula. Should always include a return statement to be usable.

Example

Let's see how we can define a formula to be used in our custom fields. In this example we will show you how an administrator can define a formula which adds all the input weights. This formula will be used in a packaging model.

In the left menu go to Admin > Formulas

Click on the upper right button Add formula, and add the following information

  • Name: Packaging_Comp_Weight_Sum
  • Notes: Sum the weight of elements of a packaging component
  • Value:
const { asset, subAsset, subAssets, input, getLookupTableValue, getFieldValue } = context

const params = ['pack_rawmat_l2comp_alu_weight','pack_rawmat_l2comp_steel_weight','pack_rawmat_l2comp_stainlesssteel_weight','pack_rawmat_l2comp_pet_weight','pack_rawmat_l2comp_pp_weight','pack_rawmat_l2comp_hdpe_weight','pack_rawmat_l2comp_ldpe_weight','pack_rawmat_l2comp_ps_weight','pack_rawmat_l2comp_pc_weight','pack_rawmat_l2comp_pvc_weight','pack_rawmat_l2comp_pse_weight','pack_rawmat_l2comp_glass_weight','pack_rawmat_l2comp_cardboard_weight','pack_rawmat_l2comp_paper_weight','pack_rawmat_l2comp_wood_weight','pack_rawmat_l2comp_plasticizer_weight','pack_rawmat_l2comp_cork_weight','pack_rawmat_l2comp_adhesive_weight']

let result = 0

params.forEach(param => {

result = result + Number(getFieldValue(subAsset,param) || 0)

})

return result.toFixed(3)

That's it! We can now use this formula to extend the functionality of our model via the custom fields.