Implementing Default Field Values in a Make a Copy Context: User Event Script :BeforeLoad

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define(['N/currentRecord', 'N/record','N/runtime', 'N/search', 'N/ui/serverWidget'],
    /**
 * @param{currentRecord} currentRecord
 * @param{record} record
 * @param{runtime} runtime
 * @param{search} search
 * @param{serverWidget} serverWidget
 */
    (currentRecord, record, runtime, search, serverWidget) => {
        /**
         * Defines the function definition that is executed before record is loaded.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @param {Form} scriptContext.form - Current form
         * @param {ServletRequest} scriptContext.request - HTTP request information sent from the browser for a client action only.
         * @since 2015.2
         */
const beforeLoad = (scriptContext) => {
          try{
        if (scriptContext.type === scriptContext.UserEventType.COPY) {
          log.debug("make a  copy");
          // set null if make a copy      
          var currentRecordObj = scriptContext.newRecord;
                  var lineCount = currentRecordObj.getLineCount({
                      sublistId : 'item'
                  })
                  for(var j = 0; j < lineCount; j++){
                      var createPO = currentRecordObj.setSublistValue({
                          sublistId: 'item',
                          fieldId: 'createpo',
                          line : j,
                          value: "",
                      });
                  }  
        }
            else{
              log.debug("not make a copy");
            }
      }
      catch (err) {
        log.debug("error@beforeLoad", err);
      }
        }
return {beforeLoad, beforeSubmit, afterSubmit}
    });

Leave a comment

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