How to Automatically Clear Fields When Copying a Transaction in NetSuite

How to Automatically Clear Fields When Copying a Transaction in NetSuite

When copying transaction records in NetSuite, you might want to clear certain fields to ensure that irrelevant or outdated data doesn't carry over. For example, when duplicating a Sales Order, some custom fields, like shipping or export status, may not apply to the new order.

Here’s how you can achieve that by using a beforeLoad User Event script. This script runs when a Sales Order is copied, ensuring specific fields are cleared.

Steps:

  1. Create a User Event Script for the Sales Order, which will trigger on the copy event.
  2. Clear Sublist and Body Fields: Loop through the item lines and clear custom fields like "Quantity Shipped" and "Export Status".
  3. Reset Field Values: Ensure custom body fields like "3PL Status" and "Fulfillment Error" are also reset.

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 */

define(["N/record"], function (record) {
  function beforeLoad(context) {
    var newRecord = context.newRecord;

    // Get the total number of line items in the 'item' sublist
    var lineCount = newRecord.getLineCount({
      sublistId: "item",
    });

    // Loop through each line item in the sublist and clear relevant fields
    for (var i = 0; i < lineCount; i++) {
      newRecord.setSublistValue({
        sublistId: "item",
        fieldId: "custcol_custom_field_1",
        line: i,
        value: "",
      });

      newRecord.setSublistValue({
        sublistId: "item",
        fieldId: "custcol_custom_checkbox",
        line: i,
        value: false,
      });

      newRecord.setSublistValue({
        sublistId: "item",
        fieldId: "custcol_custom_qty_field",
        line: i,
        value: "",
      });
    }

    // Clear or reset body-level custom fields
    newRecord.setValue({
      fieldId: "custbody_custom_status",
      value: 1,
    });

    newRecord.setValue({
      fieldId: "custbody_custom_checkbox_field",
      value: false,
    });

    newRecord.setValue({
      fieldId: "custbody_custom_error_field",
      value: "",
    });
  }

  return {
    beforeLoad: beforeLoad,
  };
});        


要查看或添加评论,请登录

社区洞察

其他会员也浏览了