Just completed my first functioning user event scr...
# suitescript
h
Just completed my first functioning user event script and was wondering if anyone had recommendations on this script? Goal: Duplicate sublist field value to a field value on item record save.
Copy code
/**
 *@NApiVersion 2.1
 *@NScriptType UserEventScript
 */
 
// Load two standard modules.
define ( ['N/record', 'N/ui/serverWidget'] ,
    // Add the callback function.
    function(record, serverWidget) {
 
        // In the beforeSubmit function, add new price to Schema custom field on inv & non-inv item records.
        function myBeforeSubmit(context) {
            //Simplify Code - remove context.
            var newRecord = context.newRecord;
            var oldRecord = context.oldRecord;

            //get the online price that is about to be submitted
            var newItemOnlinePrice = newRecord.getSublistValue({
                sublistId: "price",
                fieldId: "price_1_",
                line: 6
            });

            //get the online price that was on the record prior
            var oldItemOnlinePrice = oldRecord.getSublistValue({
                sublistId: "price",
                fieldId: "price_1_",
                line: 6
            });    

            //If there is no change to the online price the script should be done
            if (oldItemOnlinePrice === newItemOnlinePrice)
                return;

            //Update the custom field on the item record to match the online price. Make sure it is a number record and has 2 decimal places.
            newRecord.setValue({
                fieldId: 'custitem_onlineprice_for_schema',
                value: `${Number(newItemOnlinePrice).toFixed(2)}`
            });
        }
        //Return the before submit statement
        return {
            beforeSubmit: myBeforeSubmit
        };
    });
r
I would consider arrow functions since you're using 2.1. Also
toFixed(2)
does inconsistent rounding. https://gist.github.com/djD-REK/068cba3d430cf7abfddfd32a5d7903c3
๐Ÿ‘Ž 1
๐Ÿ‘ 1
a
~Two recommendations~: What he said ^^ ...
r
@alien4u deserves credit for teaching me the second point.
๐Ÿ™Œ 1
๐Ÿ™Œ๐Ÿป 1
๐Ÿ˜… 2
a
You can add: โ€ข Never use
var
again in 2.1.
โœ… 2
b
you are supposed to use the Pricing Matrix
h
Would this be necessary if we don't have quantity pricing and only accept a single currency?
b
Pricing Sublist Feature Dependencies lists the three features that enable the pricing matrix
s
i donโ€™t see why youโ€™d want matrix (qty based pricing) given the code. iโ€™ve seen cases where itโ€™s simply desirable to pull the amount from a field rather than the pricing list and this code would allow that to happen. @Hunter Jacobs just be careful that nothing updates the price without triggering the user event (ex CSV with scripting turned off).
h
Sweet, that makes sense. Thank you all for the insight (I guess I need to break some bad habits before they form)!
b
you choose the pricing matrix because its more likely to work in the future
netsuite internally represents the matrix as a sublist, which is why you have the unusual field and why it works with the sublist related apis
that can change, and its more likely that code that uses the matrix related apis will continue to work
๐Ÿ’ฏ 1
h
@battk You are the best, encountered my first bug today ๐Ÿ˜…. We added an additional price level which caused the price field to update with the wrong line pricing!
Put in a temporary fix but need to figure out how to use the matrix
s
oh i see what he meant now. you want to find out which line contains the pricing you want which you can do with find line item value then you have the line number and can get the right value
b
not really matrix related, but that the price level hes interested in isnt always gonna be line 7
h
Yup, it updated today from 6 to 7 so now I need it based on price level name
s
would be better if you use the internal id
in case it gets renamed
h
message has been deleted
Would that have an ID?
is it %?
5
b
the price level field is a select field
with each option have an internal id value
and display text
the internal id of the price level is
5
, the text is
Online Price
h
Again, I can't thank you guys enough and just incase you wanted to see the updated code:
Copy code
/**
 *@NApiVersion 2.1
 *@NScriptType UserEventScript
 */
 
// Load two standard modules.
define ( ['N/record', 'N/ui/serverWidget'] ,
    // Add the callback function.
    (record, serverWidget) => {
 
        // In the beforeSubmit function, add new price to Schema custom field on inv & non-inv item records.
        myBeforeSubmit = (context) => {
            //Simplify Code - remove context.
            const newRecord = context.newRecord;
            const oldRecord = context.oldRecord;

            //Pull the line value of the price level with the Id of 5 (Our Online Price Level).
            const pricingSublistLineLevel = newRecord.findSublistLineWithValue({
                sublistId: "price",
                fieldId: 'pricelevel', //The Id of the sublist values
                value: '5'
            })

            //get the online price that is about to be submitted
            const newItemOnlinePrice = newRecord.getSublistValue({
                sublistId: "price",
                fieldId: "price_1_",
                line: `${Number(pricingSublistLineLevel)}`
            });

            //get the online price that was on the record prior
            const oldItemOnlinePrice = oldRecord.getSublistValue({
                sublistId: "price",
                fieldId: "price_1_",
                line: `${Number(pricingSublistLineLevel)}`
            });

            //If there is no change to the online price the script should be done
            if (oldItemOnlinePrice === newItemOnlinePrice)
                return;

            //Update the custom field on the item record to match the online price. Make sure it is a number record and has 2 decimal places.
            newRecord.setValue({
                fieldId: 'custitem_onlineprice_for_schema',
                value: `${Number(newItemOnlinePrice).toFixed(2)}`
            });
        }
        //Return the before submit statement
        return {
            beforeSubmit: myBeforeSubmit
        };
    });
b
Copy code
`${Number(pricingSublistLineLevel)}
is an unusual choice in general
and extra unnecessary with pricingSublistLineLevel, its already a number
h
other than the Number() redundancy how would I make that more "normal"
b
the backtick (`) makes a template string
which is used to put code in the middle of a string
you only have code in there, so the template string is unnecessary
you can just go straight to code
h
Like this
line: pricingSublistLineLevel
b
yes
๐Ÿ‘ 1