Item Line Combining

User event script for item line combining in purchase order. Only consider inventory,non-inventory and service items

    function removeItemLines(rec, linesToRemove) {

      try {

        // Remove the unupdated quantity item lines

        for (let i = linesToRemove.length – 1; i >= 0; i–) {

          rec.removeLine({

            sublistId: ‘item’,

            line: linesToRemove[i]

          });

        }

      } catch (e) {

        log.error(“error@removeItemLines”, e);

      }

    }

    function updateQuantity(rec, itemRateQuantityMap) {

      try {

        // Update the quantities in the sublist based on the itemRateQuantityMap

        for (let key in itemRateQuantityMap) {

          let totalQuantity = itemRateQuantityMap[key].totalQuantity;

          let totalAmount = itemRateQuantityMap[key].totalAmountQuantity;

          let cbm = itemRateQuantityMap[key].totalCbmQuantity;

          let weight = itemRateQuantityMap[key].totalWeightQuantity;

          let fpQty = itemRateQuantityMap[key].totaltotalFPQuantity;

          let qtySent = itemRateQuantityMap[key].totalQuantitySent;

          let firstLine = itemRateQuantityMap[key].firstLine;

          // Update the quantity for the first line of the item and rate combination

          rec.setSublistValue({

            sublistId: ‘item’,

            fieldId: ‘quantity’,

            line: firstLine,

            value: totalQuantity

          });

          rec.setSublistValue({

            sublistId: ‘item’,

            fieldId: ‘amount’,

            line: firstLine,

            value: totalAmount

          });

          rec.setSublistValue({

            sublistId: ‘item’,

            fieldId: ‘custcol_cd9_jj_new_cbm’,

            line: firstLine,

            value: cbm

          });

          rec.setSublistValue({

            sublistId: ‘item’,

            fieldId: ‘custcol_cd9_jj_new_weight’,

            line: firstLine,

            value: weight

          });

          rec.setSublistValue({

            sublistId: ‘item’,

            fieldId: ‘custcol_jj_total_fp_qty_cdus_3346’,

            line: firstLine,

            value: fpQty

          });

          rec.setSublistValue({

            sublistId: ‘item’,

            fieldId: ‘custcol_jj_qty_sent’,

            line: firstLine,

            value: qtySent

          });

        }

      } catch (e) {

        log.error(“error@updateQuantity”, e);

      }

    }

    /**

    * Defines the function definition that is executed before record is submitted.

    * @param {Object} scriptContext

    * @param {Record} scriptContext.newRecord – New record

    * @param {Record} scriptContext.oldRecord – Old record

    * @param {string} scriptContext.type – Trigger type; use values from the context.UserEventType enum

    * @since 2015.2

    */

    const beforeSubmit = (scriptContext) => {

      try {

        if (scriptContext.type == scriptContext.UserEventType.CREATE || scriptContext.type == scriptContext.UserEventType.EDIT) {

          let rec = scriptContext.newRecord;

          let linecount = rec.getLineCount({ sublistId: ‘item’ });

          let itemRateQuantityMap = {};

          let linesToRemove = [];

          // Loop through the sublist to populate the itemRateQuantityMap and handle duplicates

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

            let isClosed = rec.getSublistValue({

              sublistId: ‘item’,

              fieldId: ‘isclosed’,

              line: i

            });

            // Skip closed lines and group lines

            if (isClosed == true || rec.getSublistValue({ sublistId: ‘item’, fieldId: ‘quantity’, line: i }) == 0 || rec.getSublistValue({ sublistId: ‘item’, fieldId: ‘itemtype’, line: i }) == “Group” || rec.getSublistValue({ sublistId: ‘item’, fieldId: ‘itemtype’, line: i }) == “EndGroup” || rec.getSublistValue({ sublistId: ‘item’, fieldId: ‘ingroup’, line: i }) == “T”) {

              continue;

            }

            let item = rec.getSublistValue({ sublistId: ‘item’, fieldId: ‘item’, line: i });

            let rate = rec.getSublistValue({ sublistId: ‘item’, fieldId: ‘rate’, line: i });

            let classValue = rec.getSublistValue({

              sublistId: ‘item’,

              fieldId: ‘class’,

              line: i

            });

            let expectedReci = rec.getSublistValue({

              sublistId: ‘item’,

              fieldId: ‘expectedreceiptdate’,

              line: i

            });

            let description = rec.getSublistValue({

              sublistId: ‘item’,

              fieldId: ‘description’,

              line: i

            });

            let cbm = rec.getSublistValue({

              sublistId: ‘item’,

              fieldId: ‘custcol_corpdesign_cbm’,

              line: i

            });

            let weight = rec.getSublistValue({

              sublistId: ‘item’,

              fieldId: ‘custcol_jj_cd9_item_weight’,

              line: i

            });

            let cartons = rec.getSublistValue({

              sublistId: ‘item’,

              fieldId: ‘custcol_jj_cartons’,

              line: i

            });

            let quantity = rec.getSublistValue({ sublistId: ‘item’, fieldId: ‘quantity’, line: i });

            let amount = rec.getSublistValue({

              fieldId: “amount”,

              sublistId: ‘item’,

              line: i

            });

            let newCbm = rec.getSublistValue({

              fieldId: “custcol_cd9_jj_new_cbm”,

              sublistId: ‘item’,

              line: i

            });

            let newWeight = rec.getSublistValue({

              fieldId: “custcol_cd9_jj_new_weight”,

              sublistId: ‘item’,

              line: i

            });

            let totalFPQty = rec.getSublistValue({

              fieldId: “custcol_jj_total_fp_qty_cdus_3346”,

              sublistId: ‘item’,

              line: i

            });

            let qtySent = rec.getSublistValue({

              fieldId: “custcol_jj_qty_sent”,

              sublistId: ‘item’,

              line: i

            });

            

            if (rate == “”) {

              rate = 0;

            }

            

            let key = `${item}-${rate}-${classValue}-${expectedReci}-${description}-${cbm}-${weight}-${cartons}`;

            // Update or add to itemRateQuantityMap

            if (key in itemRateQuantityMap) {

              itemRateQuantityMap[key].totalQuantity += quantity;

              itemRateQuantityMap[key].totalAmountQuantity += amount;

              itemRateQuantityMap[key].totalCbmQuantity += newCbm;

              itemRateQuantityMap[key].totalWeightQuantity += newWeight;

              itemRateQuantityMap[key].totaltotalFPQuantity += totalFPQty;

              itemRateQuantityMap[key].totalQuantitySent += qtySent;

              itemRateQuantityMap[key].combined = true;

              linesToRemove.push(i);

            } else {

              itemRateQuantityMap[key] = { totalQuantity: quantity, totalAmountQuantity: amount, totalCbmQuantity: newCbm, totalWeightQuantity: newWeight, totaltotalFPQuantity: totalFPQty, totalQuantitySent: qtySent, combined: false, firstLine: i };

            }

          }

          updateQuantity(rec, itemRateQuantityMap);

          removeItemLines(rec, linesToRemove);

        }

      } catch (e) {

        log.error(“error@beforSubmit”, e);

      }

    }

Leave a comment

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