Validate Order on Entry

Customization Details

This customization for this use case includes:

  • A custom field (Cases per Pallet) that is used to store the number of cases of the item that are included on one pallet
  • A custom field (Item Weight) that is used to store the weight of a single item
  • A client script triggered on the validateLine and saveRecord entry points
  • A script parameter (Max Weight ) that is used to store the maximum weight for an order

Steps in this tutorial to complete this customization:

Step 1: Create the Custom Fields

This customization uses two custom fields. The Cases per Pallet field stores the number of cases of a particular item that fit on one pallet. The Item Weight field stores the weight of a single item. Both fields are custom transaction line fields added to the Item sublist on a sales order record.

To create the Cases per Pallet field:

  1. Go to Customization > Lists, Records, Fields > Transaction Line Fields > New.

To create the Item Weight field:

  1. Go to Customization > Lists, Records, Fields > Transaction Line Fields > New.

Step 2: Write the Script

This script validates an order to make sure that the number of items in the order corresponds to the number of cases of that item that can fit on a pallet (that is, only accept orders fulfilled by full cases), and verifies that the total weight of the order does not exceed the maximum allowed weight for a single order.

       /**

 * @NApiVersion 2.1

 * @NScriptType ClientScript

 * @NModuleScope SameAccount

 */

define([‘N/runtime’, ‘N/log’], (, runtime, log) => {

  function validateLine(scriptContext) {

    const recSalesOrder = scriptContext.currentRecord;

    if (scriptContext.sublistId === ‘item’) {

      const casePerPallet = recSalesOrder.getCurrentSublistValue({

        sublistId: ‘item’,

        fieldId: ‘custcol_cases_per_pallet’

      });

      const quantity = recSalesOrder.getCurrentSublistValue({

        sublistId: ‘item’,

        fieldId: ‘quantity’

      });

      if (quantity % casePerPallet !== 0) {

        alert(`Please order in multiples of ${casePerPallet} Case/Pallet for this item.`);

        return false;

      } else {

        return true;

      }

    }

    return true;

  }

  function saveRecord(scriptContext) {

    const scriptObj = runtime.getCurrentScript();

    const weightParam = scriptObj.getParameter({

      name: ‘custscript_max_weight’

    });

    if (NSUtil.isEmpty(weightParam)) {

      log.error({

        title: ‘saveRecord’,

        details: ‘Max weight script parameter not defined’

      });

      return;

    }

    const recSalesOrder = scriptContext.currentRecord;

    const lineCount = recSalesOrder.getLineCount({

      sublistId: ‘item’

    });

    let totalWeight = 0;

    for (let i = 0; i < lineCount; i++) {

      const itemWeight = recSalesOrder.getSublistValue({

        sublistId: ‘item’,

        fieldId: ‘custcol_item_weight’,

        line: i

      });

      const quantity = recSalesOrder.getSublistValue({

        sublistId: ‘item’,

        fieldId: ‘quantity’,

        line: i

      });

      const lineWeight = itemWeight * quantity;

      totalWeight += lineWeight;

    }

    if (totalWeight > weightParam) {

      alert(`The total weight of the order is ${totalWeight}. The max weight is ${weightParam}. Please adjust the order and submit a separate order if necessary.`);

      return false;

    }

    return true;

  }

  const NSUtil = {};

  NSUtil.isEmpty = function(stValue) {

    return(

      stValue === “” || stValue === null || stValue === undefined ||

      (stValue.constructor === Array && stValue.length === 0) ||

      (stValue.constructor === Object &&

      (function(v){

        for (const k in v) return false;

        return true;

      })(stValue))

    );

  }

  return {

    validateLine: validateLine,

    saveRecord: saveRecord

  };

}); 

       

Leave a comment

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