Ask Your Question
0

How can we take a subset of data from the Sales Order and save it in a Custom Body Field on Sales Order SuiteScript 2.0 to derive its worth?

asked 2021-06-13 11:00:00 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-05-16 03:00:00 +0000

huitzilopochtli gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-06-13 11:00:00 +0000

Seen: 8 times

Last updated: May 16 '22