To loop through all item lines when each item line is added

when each item field is added this line field should be updated and also sum of this field should be updated in body field .

Implement the client script in sublistChanged trigger:

Note: This process will impact the record performance.

/**

     * Function to be executed after sublist is inserted, removed, or edited.

     *

     * @param {Object} scriptContext

     * @param {Record} scriptContext.currentRecord – Current form record

     * @param {string} scriptContext.sublistId – Sublist name

     *

     * @since 2015.2

     */

    function sublistChanged(scriptContext) {

      try {

        // console.log(‘Enters to sublistChanged ‘);

        let currentRecordObj = currentRecord.get();

        let sublistId = ‘item’;

        let totalFieldId = ‘custbody_bit_lines_total_weight’;

        let total = 0;

        let lineCount = currentRecordObj.getLineCount({ sublistId: sublistId });

        for (let i = 0; i < lineCount; i++) {

          let quantity = currentRecordObj.getSublistValue({

            sublistId: sublistId,

            fieldId: ‘quantity’,

            line: i

          });

          let unitWeight = currentRecordObj.getSublistValue({

            sublistId: sublistId,

            fieldId: ‘custcol_bit_unit_weight’,

            line: i

          });

          if (!unitWeight.toString() || !unitWeight)

            total = total;

          else

            total = total + (parseFloat(quantity) * parseFloat(unitWeight));

        }

        console.log(‘Lines Total’, total);

        currentRecordObj.setValue({

          fieldId: totalFieldId,

          value: (total.toFixed(2))

        });

      }

      catch (e) {

        console.log(“error@sublistChanged”, e);

        log.debug(“error@sublistChanged”, e);

      }

    }

 return {

      sublistChanged: sublistChanged

    }

Leave a comment

Your email address will not be published. Required fields are marked *