Script to set item description in make a copy

Script to set item description in make a copy of a sales order for custom descriptions even if the customer is changed. We can’t implement this using user event script.

define([‘N/currentRecord’],

  /**

   * @param{currentRecord} currentRecord

   */

  function (currentRecord) {

    ‘use strict’;

    let originalDescription = {};

    let mode;

    /**

    * Function to handle storing original descriptions.

    * 

    * @param {Object} rec – The current record

    */

    function handleOriginalDescriptions(rec) {

      try {

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

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

          let itemId = rec.getSublistValue({

            sublistId: ‘item’,

            fieldId: ‘item’,

            line: index

          });

          let description = rec.getSublistValue({

            sublistId: ‘item’,

            fieldId: ‘description’,

            line: index

          });

          // Store the original description using item ID as the key

          originalDescription[itemId] = description;

        }

      } catch (e) {

        console.error(“Error @ handleOriginalDescriptions:”, e);

        log.error(“Error @ handleOriginalDescriptions”, e);

      }

    }

    /**

     * Function to handle restoring descriptions when entity field changes in ‘copy’ mode.

     * 

     * @param {Object} rec – The current record

     */

    function handleEntityFieldChange(rec) {

      try {

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

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

          let itemId = rec.getSublistValue({

            sublistId: ‘item’,

            fieldId: ‘item’,

            line: ind

          });

          if (originalDescription[itemId]) {

            if (originalDescription[itemId]) {

              rec.selectLine({ sublistId: ‘item’, line: ind });

              rec.setCurrentSublistValue({

                sublistId: ‘item’,

                fieldId: ‘description’,

                value: originalDescription[itemId],

                forceSyncSourcing: true

              });

              rec.commitLine({ sublistId: ‘item’ });

            }

          }

        }

      } catch (e) {

        console.error(“Error @ handleEntityFieldChange:”, e);

        log.error(“Error @ handleEntityFieldChange:”, e);

      }

    }

    /**

     * Function to be executed after page is initialized.

     *

     * @param {Object} scriptContext

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

     * @param {string} scriptContext.mode – The mode in which the record is being accessed (create, copy, or edit)

     *

     * @since 2015.2

     */

    function pageInit(context) {

      try {

        let recObj = context.currentRecord;

        mode = context.mode;

        if (mode === ‘copy’) {

          handleOriginalDescriptions(recObj);

        }

      }

      catch (e) {

        console.error(“Error @ pageInit:”, e);

        log.error(“Error @ pageInit:”, e);

      }

    }

    /**

     * Function to be executed when field is changed.

     *

     * @param {Object} scriptContext

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

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

     * @param {string} scriptContext.fieldId – Field name

     * @param {number} scriptContext.lineNum – Line number. Will be undefined if not a sublist or matrix field

     * @param {number} scriptContext.columnNum – Line number. Will be undefined if not a matrix field

     *

     * @since 2015.2

     */

    function fieldChanged(context) {

      try {

        let currec = context.currentRecord;

        let fieldId = context.fieldId;

        // If the customer field changes

        if (fieldId === ‘entity’ && mode === ‘copy’) {

          handleEntityFieldChange(currec);

        }

      }

      catch (e) {

        console.error(“Error @ fieldChanged:”, e);

        log.error(“Error @ fieldChanged:”, e);

      }

    }

     

    return {

      pageInit: pageInit,

      fieldChanged: fieldChanged

    };

  });

Leave a comment

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