Removing File Attachments When Copying a Transaction in NetSuite

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:

  1. Create a User Event Script that triggers on the beforeSubmit event.
  2. Check if the transaction is being copied by detecting the nlapiGetContext().getExecutionContext() value.
  3. 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.CREATE ensures 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 mediaitem sublist and remove each attachment.

Deployment:

  1. Save the script as a User Event Script in NetSuite.
  2. 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.

Leave a comment

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