Restricting the sales order creation based on the checkbox field in the customer record

Description

Upon saving the sales order record, the script will verify whether the customer’s checkbox named “Disallow Create Order” is checked. If the checkbox is found to be checked, it will restrict the update or creation of the sales order each time on its save.

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 * @NModuleScope public
 */
define([
  'N/search',
  'N/record',
  'N/runtime'
], function (search, record, runtime) {
  
  function beforeSubmit(context) {
    const scriptObj = runtime.getCurrentScript();
    const entityId = context.newRecord.getValue('entity');
    const rejectionMessage = scriptObj.getParameter('custscript_csso_ue_rejectmessage');
    const disAllow = search.lookupFields({
      type: search.Type.CUSTOMER,
      id: entityId,
      columns: ['custentity_itg_disallow_create_order']
    }).custentity_itg_disallow_create_order;
    if(disAllow) {
      throw rejectionMessage;
    }
  }

  return {
    beforeSubmit
  };
});

Leave a comment

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