Restrict Sales Order if Customer is Unapproved

Client script can be used to restrict the sales order if the customer is unapproved. To check whether the customer is approved load the corresponding customer record and check whether the approved field value is true or not. If true save the sales order else restrict the sales order record and show an alert message.

For example:

/**

 * @NApiVersion 2.x

 * @NScriptType ClientScript

 */

define([‘N/record’, ‘N/search’], function(record, search) {

  function saveRecord(context) {

    var currentRecord = context.currentRecord;

    var customerId = currentRecord.getValue(‘entity’); // Get the Customer ID from the Sales Order

    if (!customerId) {

      return true; // Allow save if no customer is selected

    }

    // Load the Customer record to check the “Approved” checkbox

    var customerRecord = record.load({

      type: record.Type.CUSTOMER,

      id: customerId

    });

    var isApproved = customerRecord.getValue(‘custentity_approved’); // Replace with your custom checkbox field ID

    if (!isApproved) {

      // Update the Customer record (example: setting a custom field to indicate they tried to place an order)

      customerRecord.setValue({

        fieldId: ‘custentity_tried_to_place_order’,

        value: true

      });

      // Save the updated Customer record

      customerRecord.save();

      alert(‘The customer is not approved. You cannot save this Sales Order.’);

      return false; // Restrict saving the Sales Order

    }

    return true; // Allow save if the customer is approved

  }

  return {

    saveRecord: saveRecord

  };

});

Leave a comment

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