Implement a field in NetSuite for displaying item fulfilment status in Sales Order

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
/*************************************************************************************
 ***********
 * Madi International Co LLC-UAE-SCA
 *
 * MADI-574
 *
 *
 *************************************************************************************
 ***********
 *
 *
 * Author: Jobin and Jismi IT Services LLP
 *
 * Date Created : 12-July-2023
 *
 * Description: This script is used for setting the Infor sync and Maersk status in sales order.
 *
 * REVISION HISTORY
 *
 * @version 1.0 MADI-574 :12-July-2023 : Created the initial build by JJ0177
 *
 *
 *************************************************************************************
 **********/
 define(['N/record', 'N/search'],
 /**
  * @param{record} record
  * @param{search} search
  */
 (record, search,) => {
     /**
      * Function to check whether the field has an empty value or not.
      * @param {parameter} parameter - fieldValue
      * @returns {boolean} true - if the value is not empty
      * @returns {boolean} false - if the value is empty
      */
     function checkForParameter(parameter) {
         try {
             if (parameter != "" && parameter != null && parameter != undefined && parameter != "null" && parameter != "undefined" && parameter != " " && parameter != false && parameter != '' && parameter != ' ') {
                 return true;
             } else {
                 return false;
             }
         }
         catch (e) {
             log.debug({
                 title: "Error @ empty check Function: ", details: e.name + ' : ' + e.message
             })
         }
     }
     /**
     * Saved search function used get the error field value from infor integration info
     * @param soid{number}- internal id of sales order record
     * @returns inforShiperrorObj - 0 or 1 depending on the result
     */
     function searchInforErrorFields(soId) {
         try {

             let inforErrorObj = {};
             let inforSearchObj = search.create({
                 type: "customrecord_jj_infor_integration_info",
                 filters:
                     [
                         ["custrecord_jj_transaction_rec.internalid", "anyof", soId]
                     ],
                 columns:
                     [
                         search.createColumn({
                             name: "formulatext",
                             summary: "GROUP",
                             formula: "CASE WHEN {custrecord_jj_error_if_creation} IS NOT NULL THEN '1' ELSE '0' END",
                             label: "ship error"
                         }),
                         search.createColumn({
                             name: "formulatext",
                             summary: "GROUP",
                             formula: "CASE WHEN {custrecord_jj_error_so_creation} IS NOT NULL THEN '1' ELSE '0' END",
                             label: "Pic error"
                         }),
                     ]
             });
             inforSearchObj.run().each(function (result) {
                 inforErrorObj.inforShipErrorValue = result.getValue(inforSearchObj.columns[0]);
                 inforErrorObj.inforPickErrorValue = result.getValue(inforSearchObj.columns[1]);
                 return true;
             });
             return inforErrorObj;
         } catch (error) {
             log.debug("error @searchInforErrorIfFields", error);
             return {};
         }
     }
     /**
     * Saved search function used get the error field value from maersk integration info
     * @param soid{number}- internal id of sales order record
     * @returns maerskErrorObj - 0 or 1 depending on the result
     */
     function searchMaerskErrorFields(soId) {
         try {
             let maerskErrorObj = {};
             let maerskSearchObj = search.create({
                 type: "customrecord_jj_cr_maersk_integrtn_info",
                 filters:
                     [
                         ["custrecord_jj_transaction_micl300.internalid", "anyof", soId]
                     ],
                 columns:
                     [
                         search.createColumn({
                             name: "formulatext",
                             summary: "GROUP",
                             formula: "CASE WHEN {custrecord_jj_error_if_micl300} IS NOT NULL THEN '1' ELSE '0' END",
                             label: "Ship error"
                         }),
                         search.createColumn({
                             name: "formulatext",
                             summary: "GROUP",
                             formula: "CASE WHEN {custrecord_jj_error_so_micl300} IS NOT NULL THEN '1' ELSE '0' END",
                             label: "Pick error"
                         }),
                     ]
             });
             maerskSearchObj.run().each(function (result) {
                 maerskErrorObj.maerskShipErrorValue = result.getValue(maerskSearchObj.columns[0]);
                 maerskErrorObj.maerskPickErrorValue = result.getValue(maerskSearchObj.columns[1]);               
                 return true;
             });
             return maerskErrorObj;
         } catch (error) {
             log.debug("error @searchMaerskErrorFields", error);
             return {};
         }
     }
     /**
     * function useto submit record fields
     * @param soid{number}- internal id of sales order record
     * @param id{number}- internal id of the list fields
    */
     function submitRecordFields(id, soId) {
         try {
             record.submitFields({
                 type: record.Type.SALES_ORDER,
                 id: soId,
                 values:
                 {
                     custbodyy_jj_if_status_micl574: id
                 },
                 options:
                 {
                     enableSourcing: false,
                     ignoreMandatoryFields: true
                 }
             });
         } catch (error) {
             log.debug("error @submitRecordFields", error);
         }
     }
     /**
      * 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 {
             let soRec = scriptContext.newRecord;
             if (scriptContext.type === scriptContext.UserEventType.COPY) {
                 soRec.setValue({
                     fieldId: 'custbodyy_jj_if_status_micl574',
                     value: '',
                     ignoreFieldChange: true
                 });
             }
             let inforSoSyncField = soRec.getValue({
                 fieldId: 'custbody_jj_infor_sales_order_key'
             });
             let maerskSoSyncField = soRec.getValue({
                 fieldId: 'custbody_jj_synced_to_maersk_micl_300'
             });
             let soStatus = soRec.getValue({ fieldId: 'orderstatus' });
             if (checkForParameter(inforSoSyncField) && !checkForParameter(maerskSoSyncField)) {
                 let inforErrorFields = searchInforErrorFields(soRec.id);
                 if (inforErrorFields.inforShipErrorValue == 1) {
                     submitRecordFields(4, soRec.id);
                 } else if (inforErrorFields.inforPickErrorValue == 1) {
                     submitRecordFields(5, soRec.id);
                 } else if (soStatus == 'F' && inforErrorFields.inforShipErrorValue == 0) {
                     submitRecordFields(2, soRec.id);
                 } else if (soStatus == 'E') {
                     submitRecordFields(3, soRec.id);
                 } else if (soStatus == 'B') {
                     submitRecordFields(1, soRec.id);
                 }
             } else if (checkForParameter(maerskSoSyncField) && !checkForParameter(inforSoSyncField)) {
                 let maerskSyncError = searchMaerskErrorFields(soRec.id);
                 if (maerskSyncError.maerskShipErrorValue == 1) {
                     submitRecordFields(10, soRec.id);
                 } else if (maerskSyncError.maerskPickErrorValue == 1) {
                     submitRecordFields(9, soRec.id);
                 } else if (soStatus == 'F') {
                     submitRecordFields(7, soRec.id);
                 } else if (soStatus == 'E') {
                     submitRecordFields(8, soRec.id);
                 } else if (soStatus == 'B') {
                     submitRecordFields(6, soRec.id);
                 }
             }
             else if (!checkForParameter(maerskSoSyncField) && !checkForParameter(inforSoSyncField) && soStatus == 'B') {

                 let inforErrorsOnPicking = searchInforErrorFields(soRec.id);
                 if (inforErrorsOnPicking.inforPickErrorValue == 1) {
                     submitRecordFields(5, soRec.id);
                 }
                 else {
                     let maerskErrorsOnPicking = searchMaerskErrorFields(soRec.id)
                     if (maerskErrorsOnPicking.maerskPickErrorValue == 1) {
                         submitRecordFields(9, soRec.id);
                     }
                 }
             }
         } catch (e) {
             log.error("error @main function", e);
         }
     }
     return { beforeLoad }
 });

Leave a comment

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