How to use User Event SuiteScript on Sales Order t...
# ask-stanley-ai
s
How to use User Event SuiteScript on Sales Order to manually set each PO Vendor field on a line item, when the Sales Order is being created from a quote.
c
AI generated answer (using internal Slack threads):
To manually set each PO Vendor field on a line item when the Sales Order is being created from a quote, you can use a User Event SuiteScript. The script will be triggered on the 'beforeSubmit' event of the Sales Order record. Here is a sample script that you can modify according to your needs:
Copy code
javascript
/**
 * @NApiVersion 2.0
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
define(['N/record'], function(record) {

    function beforeSubmit(context) {
        //Check if record's form is the Sales Order Form
        var formId = context.newRecord.getValue({ fieldId: 'customform' });
        if (formId === 'Sales Order Form ID') { // replace 'Sales Order Form ID' with your actual form ID
            //Only run if SO is new or being edited
            if (context.type === context.UserEventType.CREATE || context.type === context.UserEventType.EDIT) {
                //Get SO number
                var newRecord = context.newRecord;
                //Get Vendor Name from povendor field on 1st line of sales order
                var firstVendor = newRecord.getSublistValue({
                    sublistId: 'item',
                    fieldId: 'povendor',
                    line: 0
                });
                //Get line count
                var lineCount = newRecord.getLineCount({
                    sublistId: 'item'
                });
                //Set povendor field on line items with the value from line 0
                for (var i = 0; i < lineCount; i++) {
                    newRecord.setSublistValue({
                        sublistId: 'item',
                        fieldId: 'povendor',
                        line: i,
                        value: firstVendor
                    });
                }
            }
        }
    }
    return {
        beforeSubmit: beforeSubmit
    };
});
This script will set the 'povendor' field on each line item of the Sales Order to the value of the 'povendor' field on the first line item. The script will only run when a new Sales Order is being created or an existing one is being edited. ****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ******
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.861083865
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.844726384
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.843505859
Please provide your feedback on this answer.