Applying Logic to a Button (Make Copy) in NetSuite

Overview

In NetSuite, the “Make Copy” button allows users to duplicate existing transaction records—such as Sales Orders, Invoices, or Purchase Orders—while retaining most of the original data. However, there may be cases where you need to apply custom logic during this duplication process (for example, clearing certain fields, recalculating amounts, or setting new default values).

This article explains how to implement logic on the “Make Copy” button using a User Event Script, ensuring that specific fields or conditions are handled correctly when a record is copied.

Use Case

Let’s say you want to:

  • Automatically clear approval status or internal IDs when a record is copied.
  • Recalculate field values based on new data.
  • Prevent certain fields from being duplicated.
  • Apply custom business rules when the user clicks “Make Copy.”

For example, when copying a Sales Order, you might want the new record to:

  • Reset the approvalstatus field to “Pending Approval.”
  • Clear any linked custom fields like “Project Reference.”
  • Set the current date as the trandate.

Approach

NetSuite does not provide a direct event for the “Make Copy” button, but you can detect when a record is being copied using a User Event Script.

When a record is copied, the script context (context.type) equals "copy".

This allows you to write custom logic to modify field values before the copied record is loaded into the form.

Implementation Steps

1. Script Type

Use a User Event Script and deploy it on the record type (e.g., Sales Order, Invoice, Purchase Order).

2. Example Script (SuiteScript 2.x)

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 */
define(['N/record'], function (record) {

    function beforeLoad(context) {
        if (context.type === context.UserEventType.COPY) {
            var currentRecord = context.newRecord;

            // Example 1: Clear approval status
            currentRecord.setValue({
                fieldId: 'approvalstatus',
                value: 1 // Pending Approval
            });

            // Example 2: Clear a custom field (e.g., project reference)
            currentRecord.setValue({
                fieldId: 'custbody_project_reference',
                value: ''
            });

            // Example 3: Set the current date
            currentRecord.setValue({
                fieldId: 'trandate',
                value: new Date()
            });

            // Example 4: Add a flag or note indicating it’s a copied record
            currentRecord.setValue({
                fieldId: 'memo',
                value: 'Copied from another transaction on ' + new Date().toDateString()
            });
        }
    }

    return {
        beforeLoad: beforeLoad
    };
});

Best Practices

  • Always use context.UserEventType.COPY to detect “Make Copy” actions.
  • Avoid heavy logic inside the beforeLoad event to prevent performance issues.
  • Test with different roles and permissions to ensure the logic behaves consistently.
  • Maintain version control for the script (e.g., v1.0, v1.1) in your deployment notes.

Conclusion

By leveraging the User Event Script (beforeLoad) and checking the COPY event type, you can easily control the behavior of NetSuite’s “Make Copy” function.

This helps ensure that duplicated records meet your business requirements without carrying over unwanted data or statuses.

Leave a comment

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