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:
/**
* @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,
};
});