ANS-0602 · SUITESCRIPT DEVELOPMENT
How to Detect a ‘Make Copy’ Event in NetSuite SuiteScript Beyond beforeLoad
Leverage a custom hidden field and SuiteScript 2.x to identify transactions created via the 'Make Copy' function in NetSuite's beforeSubmit or afterSubmit events.
Short answer
To detect a 'Make Copy' event in NetSuite's beforeSubmit or afterSubmit scripts, add a hidden custom checkbox field to the transaction. In the beforeLoad script, if the event type is 'copy', set this custom field to true on the record. Subsequently, beforeSubmit or afterSubmit scripts can check the value of this custom field to determine if the transaction originated from a copy operation.
Scenario
NetSuite's SuiteScript beforeLoad event provides a 'copy' event type, allowing scripts to identify when a record is being created from an existing one using the 'Make Copy' function. However, this specific event type is not directly available in subsequent server-side events like beforeSubmit or afterSubmit. Developers often need to determine if a transaction originated from a copy operation during these later stages to implement specific business logic.
Solution
To identify if a transaction was created using the 'Make Copy' function in NetSuite's beforeSubmit or afterSubmit events, implement the following steps:
Create a Custom Checkbox Field
First, create a custom checkbox field on the relevant transaction record type (e.g., custpage_is_make_copy). This field should be configured as a custom *record* field in NetSuite's UI.
Implement a User Event Script
Develop a User Event script that includes both beforeLoad and beforeSubmit (or afterSubmit) functions. The beforeLoad function will detect if the current event type is 'copy' and, if so, mark the custom checkbox field on the record. The beforeSubmit function will then read this field's value.
javascript/** * @NApiVersion 2.x * @NModuleScope SameAccount * @NScriptType UserEventScript */define(['N/log', 'N/ui/serverWidget', 'N/record'], function(log, serverWidget, record) { function beforeLoad(scriptContext) { var type = scriptContext.type; var form = scriptContext.form; var newRecord = scriptContext.newRecord; if (type === scriptContext.UserEventType.COPY) { log.debug('Copy Event - Setting custom information field'); // Set the value of the custom record field newRecord.setValue({ fieldId: 'custpage_is_make_copy', // Assuming this is a custom record field created in NetSuite value: true, ignoreFieldChange: true }); // Optionally, hide the field on the form if it's visible var copyFieldOnForm = form.getField({ id: 'custpage_is_make_copy' }); if (copyFieldOnForm) { copyFieldOnForm.updateDisplayType({ displayType: serverWidget.FieldDisplayType.HIDDEN }); } } if (type === scriptContext.UserEventType.CREATE) { log.debug('Create Event'); } } function beforeSubmit(scriptContext) { var type = scriptContext.type; var newRecord = scriptContext.newRecord; var isMakeCopy = newRecord.getValue({ fieldId: 'custpage_is_make_copy' }); if (isMakeCopy) { // Check if the checkbox is true log.debug('Before Submit of a Copy', type); } else { log.debug('Before Submit of a Create', type); } } return { beforeLoad: beforeLoad, beforeSubmit: beforeSubmit };});This script first sets the custpage_is_make_copy field to true during a 'copy' event in beforeLoad. Subsequently, the beforeSubmit function retrieves this field's value from the newRecord object to determine if the transaction was copied.
Expert NetSuite Support
Need help with this NetSuite issue?
SuiteScript Development consulting and configuration support
