In NetSuite, when copying a transaction such as an invoice, sales order, or purchase order, the attached files may also get copied. If you want to prevent this from happening, you can use a User Event Script (UE Script) to remove the file attachments from the copied transaction.
Steps to Implement:
- Create a User Event Script that triggers on the
beforeSubmitevent. - Check if the transaction is being copied by detecting the
nlapiGetContext().getExecutionContext()value. - Remove attachments from the newly created transaction.
Sample User Event Script:
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
*/
defined(['N/record', 'N/log'], function (record, log) {
function beforeSubmit(context) {
if (context.type !== context.UserEventType.CREATE) return;
var newRecord = context.newRecord;
var copiedFrom = newRecord.getValue('createdfrom');
if (copiedFrom) {
try {
// Remove existing file attachments
var attachments = newRecord.getLineCount({ sublistId: 'mediaitem' });
for (var i = attachments - 1; i >= 0; i--) {
newRecord.removeLine({
sublistId: 'mediaitem',
line: i
});
}
log.debug('Success', 'File attachments removed successfully.');
} catch (e) {
log.error('Error Removing Attachments', e);
}
}
}
return {
beforeSubmit: beforeSubmit
};
});
Explanation:
context.type !== context.UserEventType.CREATEensures that the script runs only when a new record is created.newRecord.getValue('createdfrom')checks if the transaction was copied from an existing one.- Loop through the
mediaitemsublist and remove each attachment.
Deployment:
- Save the script as a User Event Script in NetSuite.
- Deploy it to the relevant transaction record types (e.g., Invoice, Sales Order, etc.).
This script ensures that file attachments are not carried over when a transaction is copied, maintaining a clean data structure in NetSuite.