Ask Your Question

Revision history [back]

To take a subset of data from the Sales Order and save it in a Custom Body Field on Sales Order SuiteScript 2.0, you can use the following steps:

  1. Create a custom field on the Sales Order record to store the subset of data.

  2. Create a SuiteScript 2.0 script that will retrieve the desired data from the Sales Order record and store it in the custom field.

  3. In the script, use the N/record module to load the Sales Order record.

  4. Use the N/search module to perform a search for the desired data.

  5. Use the getValue() method to retrieve the value of the desired field on the Sales Order record.

  6. Use the setValue() method to set the value of the custom field with the retrieved data.

  7. Save the Sales Order record.

Here's an example of what the script could look like:

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
define(['N/search', 'N/record'],

function(search, record) {
    function afterSubmit(context) {
        if (context.type === context.UserEventType.CREATE ||
            context.type === context.UserEventType.EDIT) {
            var salesOrderId = context.newRecord.id;

            // Load the Sales Order record
            var salesOrder = record.load({ type: record.Type.SALES_ORDER, id: salesOrderId });

            // Perform a search for the desired data
            var searchResults = search.create({
                type: search.Type.SALES_ORDER,
                filters: [ ['internalid', 'anyof', salesOrderId] ],
                columns: [ 'total' ]
            }).run().getRange({ start: 0, end: 1 });

            if (searchResults.length > 0) {
                var total = searchResults[0].getValue('total');

                // Set the value of the custom field with the retrieved data
                salesOrder.setValue({ fieldId: 'custbody_sales_order_total', value: total });

                // Save the Sales Order record
                salesOrder.save();
            }
        }
    }

    return {
        afterSubmit: afterSubmit
    };
});

In the example above, the script sets the value of a custom field called custbody_sales_order_total with the total amount of the Sales Order. The script performs a search for the Sales Order using its internal ID, gets the total amount, and sets the value of the custom field on the Sales Order record. The script then saves the Sales Order record. You can modify the script to retrieve and store different subsets of data as needed.