copyRecord()

The copyRecord() function (server-side) creates a new record in the database by duplicating an existing one. It can also update fields and sublist fields as needed. You can use this function to set up new records that are similar to existing ones but require a few changes.

Note:

If you copy a record that requires unique values, make sure you update those fields in your function. For example, when coping an item, make sure you provide a new Item Name/Number (field ID: itemid).

Syntax

Use this syntax for the copyRecord() function:

            copyRecord({
    type: 'recordTypeId',
    id: recordId,
    fields: {
        fieldId1: 'value1',
        fieldId2: 'value2',
        // Additional fields
    },
    lists: {
        'sublistId1': [
            {
                update: [lineNumber] | 'all',
                values: { 
                    sublistFieldId1: 'value1', // Fields to be updated
                    sublistFieldId2: 'value2',
                    // Additional sublist fields
                }
            },
            {
                clear: [lineNumber] | 'all',
            },
            {
                sublistFieldId1: 'value1', // Specifies a new sublist line to be added
                sublistFieldId2: 'value2',
                // Additional sublist fields
            },
            // Additional sublist operations
        ],
        'sublistId2': [
            // Sublist fields
        ],
        // Additional sublists
    },
    dynamic: true | false
}).done(callback); 

          

Return Value

The copyRecord() function returns a promise. This promise resolves to an object with the internal ID of the copied record as a number. The object returned has the following structure:

            {
    "id": number
} 

          

Parameters

Note:

Required properties:

  • type

  • id

The copyRecord() function accepts a single object as a parameter. The object includes the following properties:

  • type (string) - Specifies the ID of the record type you want to copy, for example, salesorder or inventoryitem.

  • id (number) - Specifies the internal ID of the record to be copied.

  • fields (object) - Defines key-value pairs representing the field IDs to be updated in the new record.

  • lists (object) - Defines key-value pairs where each key represents a sublist ID, and each value is an array of operations to perform on that sublist.

  • dynamic - Determines whether to process the record in standard or dynamic mode. This property defaults to false. For more information about standard and dynamic modes, see SuiteScript 2.x Standard and Dynamic Modes.

    Note:

    This function works in standard mode by default. Use dynamic mode only if you experience issues when copying records in standard mode.

Sublist Operations

When copying a record, these sublist operations are available:

  • update - Specifies the line numbers to update in the new record. The values key provides the new field values. Use all to update all sublist lines.

  • clear - Specifies the line numbers to remove from the sublist for the new record. Use all to clear all sublist lines.

You can also add new sublist items by providing field values directly and combine multiple operations for the same sublist.

Examples

The following examples show how to use the copyRecord() function.

Copying a Record and Updating Field Values

This example copies an inventory item record and updates field values.

              copyRecord({
    type: 'inventoryitem',
    id: 395,
    fields: {
        itemid: 'DESK-LAMP-AB01',
        displayname: 'Desk Lamp',
        subsidiary: ["1"],
        includechildren: true,
        costcategory: 1,
        taxschedule: 1
    }
}).done(function(output) {
    console.log(output);
}); 

            

To set or update a matrix option field, make sure you also include the same field without the matrixoption prefix ID and sets its value to an empty string. See the syntax below:

              fields: {
    custitem_fieldID: "",
    matrixoptioncustitem_field_ID: "15"
} 

            

Copying a Record, Changing, and Adding Sublist Lines

In this example, the copyRecord() function updates the Amount field for lines 1 and 3 in the Items sublist. A new line item is also added by specifying item, quantity, and amount values.

              copyRecord({
    type: 'salesorder',
    id: 395,
    lists: {
        'item': [
            {
                update: [1, 3],
                values: {
                    amount: 22
                }
            },
            {
                item: 67,
                quantity: 4,
                amount: 1
            }
        ]
    }
}).done(function(output) {
    console.log(output);
}); 

            

Copying a Record and Changing Fields for All Sublist Lines

This example copies a sales order record and changes the value of the Amount field for all line items.

              copyRecord({
    type: 'salesorder',
    id: 395,
    lists: {
        'item': [
            {
                update: 'all',
                values: {
                    amount: 8
                }
            }
        ]
    }
}).done(function(output) {
    console.log(output);
}); 

            

Copying a Record and Deleting Specific Sublist Lines

The example removes lines 1 and 3 from the item sublist in a copied sales order record.

              copyRecord({
    type: 'salesorder',
    id: 396,
    lists: {
        'item': [
            {
                clear: [1, 3]
            }
        ]
    }
}).done(function(output) {
    console.log(output);
}); 

            

Copying a Record and Deleting All Sublist Lines

This example copies a sales order record and deletes all sublist line items.

              copyRecord({
    type: 'salesorder',
    id: 396,
    lists: {
        'item': [
            {
                clear: 'all'
            }
        ]
    }
}).done(function(output) {
    console.log(output);
}); 

            

Copying a Record and Combining Multiple Sublist Operations

The example copies a sales order record and combines multiple sublist operations in one call.

              copyRecord({
    type: 'salesorder',
    id: 205,
    lists: {
        'item': [
            {
                item: 12,
                quantity: 3,
                amount: 1
            },
            {
                update: 'all',
                values: {
                    amount: 2,
                    quantity: 7
                }
            },
            {
                item: 12,
                quantity: 2,
                amount: 1
            },
            {
                clear: [3, 1]
            }
        ]
    }
}).done(function(output) {
    console.log(output);
}); 

            

Related Topics

General Notices