Script to automatically add items to Estimate Based on Shipping Method

The client would like to automatically add an item to the quote record when the ship method pre pay and add is selected from the shipping method list. When a shipping method other than Pre pay and add is selected the item should be removed. To satisfy this requirement we have created a client script.

/**
 * @NApiVersion 2.1
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
/*************************************************************************************
 ***********
 * MegaResistors-USA-NS
 *
 * MEGR-138
 *
 *
 *************************************************************************************
 ***********
 *
 *
 * Author: Jobin and Jismi IT Services LLP
 *
 * Date Created : 13-October-2023
 *
 * Description: This script is used for setting the item line in quote record
 *
 * REVISION HISTORY
 *
 * @version 1.0 MEGR-138 :  13-October-2023: Created the initial build by JJ0177
 *
 *
 *************************************************************************************
 **********/

 define(['N/currentRecord'],
 /**
   *@param{currentRecord} currentRecord
   */
 
   function (currentRecord) {
     /**
        * Function to update the line item
        * @param {object} currentRec - current quote record
        * @returns {boolean} true - if the value is not empty
        */
     function addFreightLine(currentRec) {
       try {        
           // to select the new line
           currentRec.selectNewLine({
             sublistId: 'item',
 
           });
           currentRec.setCurrentSublistValue({
             sublistId: 'item',
             fieldId: 'item',
             value: "3587",           
             forceSyncSourcing: true

           });
           currentRec.setCurrentSublistValue({ sublistId: 'item', fieldId: 'quantity', value: 1,forceSyncSourcing: true });
           currentRec.setCurrentSublistValue({sublistId:'item',fieldId:'pricelevels',value:-1})
           currentRec.setCurrentSublistValue({
            sublistId: 'item',
            fieldId: 'rate',
            value: Number(0),
            forceSyncSourcing: true
          });
          currentRec.commitLine({
            sublistId: 'item'          
          });                                 
       }
       catch (e) {
         log.debug("error @ addFreightLine", error);
       }
     }
         /**
        * Function to remove the line item
        * @param {object} currentRec - current quote record
        * @returns {boolean} true - if the value is not empty
        */
         function removeFreightItem(currentRec) {
           try {    
             let lineCount = currentRec.getLineCount({
               sublistId: 'item'
             });  
             console.log("lineCount",lineCount)
             if ( lineCount > 0) {
              
                 let lineNumber = currentRec.findSublistLineWithValue({
                   sublistId: 'item',
                   fieldId: 'item',
                   value: 3587,
               
               });
               currentRec.removeLine({
                 sublistId: 'item',
                 line: lineNumber,                             
             });                     
             
             }  
                     
           }
           catch (e) {
             log.debug("error @ removeFreightItem", e);
             return false;
           }
         }
   
       /**
 
      * Function to be executed when field is slaved.
      *
      * @param {Object} scriptContext
      * @param {Record} scriptContext.currentRecord - Current form record
      * @param {string} scriptContext.sublistId - Sublist name
      * @param {string} scriptContext.fieldId - Field name
      *
      * @since 2015.2
      */
    function postSourcing(scriptContext) {
      try {
        if (scriptContext.fieldId === 'shipmethod') {
          const currentRec = scriptContext.currentRecord;
    
          // Set a timeout of 1000 milliseconds (1 second) before retrieving the 'shipmethod' value
          setTimeout(() => {
            const shipMethodId = currentRec.getValue('shipmethod');
            console.log("shipMethodId@postsourcing", shipMethodId);    
            if (shipMethodId === '3588') {
              console.log("ship method", shipMethodId);
              addFreightLine(currentRec);
            } else {
              removeFreightItem(currentRec);
            }
          }, 1000);
        }
      } catch (e) {
        console.log({
          title: e.name,
          details: e
        });
      }
    }    
     return {
      postSourcing: postSourcing   
     };
   })

Leave a comment

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