Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
865 views
in Technique[技术] by (71.8m points)

netsuite - What is the recommended method for running SuiteScripts on imported Web Services transactions?

I currently run a User Event script with a Before Submit function that populates custom column fields for every transaction. Because it's a User Event script, it doesn't fire on transactions imported through Web Services, like POS Invoices.

Edit: Per the comment below, this is a misunderstanding on my part. User Event scripts do run on Web Services.

What's the best kind of script to use if I want to modify custom fields on every imported transaction? Would a Workflow Action script triggered on After Record Submit work? Could that be problematic if I'm importing several transactions at once?

If there isn't a good way to do this when the transaction is imported, I'll probably just run a Scheduled or Map/Reduce script to update these records after the fact.

Below is the UE script. It looks at each line in a transaction, starting at the bottom, to add discount information to the line above it, if applicable.

/**
 *@NApiVersion 2.0
 *@NScriptType UserEventScript
 */

define([], function() {
    return {
        beforeSubmit : function(context) {
            
            // get record and line count
            var currentRecord = context.newRecord;
            var count = currentRecord.getLineCount({
                sublistId:'item'
            }); 

            // init discount amount
            var discountAmount = 0;

            // for each line, starting from the bottom
            for(var i = (count-1); i >= 0; i--) {

                // fetch data
                var type = currentRecord.getSublistValue({
                    sublistId: 'item',
                    fieldId: 'itemtype',
                    line: i
                });
                var amount = currentRecord.getSublistValue({
                    sublistId: 'item',
                    fieldId: 'amount',
                    line: i
                });
                var quantity = currentRecord.getSublistValue({
                    sublistId: 'item',
                    fieldId: 'quantity',
                    line: i
                });


                if (type == 'Discount') {

                    // add to current discount amount if discount
                    discountAmount += amount;

                } else {

                    // parse data
                    amount = parseFloat(amount);
                    quantity = parseInt(quantity);
                    discountAmount = parseFloat(discountAmount);

                    // set variables
                    calculatedAmount = (amount + discountAmount).toFixed(2);
                    calculatedRate = ((amount + discountAmount) / quantity).toFixed(2);
                    calculatedDiscount = (discountAmount).toFixed(2);
                    
                    // update sublist
                    currentRecord.setSublistValue({
                        sublistId: 'item',
                        fieldId: 'custcol_calculated_amount',
                        line: i,
                        value: calculatedAmount
                    });
                    currentRecord.setSublistValue({
                        sublistId: 'item',
                        fieldId: 'custcol_calculated_rate',
                        line: i,
                        value: calculatedRate
                    });
                    currentRecord.setSublistValue({
                        sublistId: 'item',
                        fieldId: 'custcol_calculated_discount',
                        line: i,
                        value: calculatedDiscount
                    });
                   
                    log.debug('discountAmount', discountAmount);
                    // reset current discount amount
                    discountAmount = 0;
                }
            }
        }
    }
})
question from:https://stackoverflow.com/questions/65924634/what-is-the-recommended-method-for-running-suitescripts-on-imported-web-services

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Because it's a User Event script, it doesn't fire on transactions imported through Web Services

This is not how it works. Your assumption above is probably made based on confusing script type name which might be accidentally identified as it only triggers when the record is created from User Interface by the User. But, in reality, the documentation outlines, the User Event script triggers when:

...The client request can come from the user interface, SOAP web services, server–side SuiteScript calls, CSV imports, or XML. ....

enter image description here

So, probably, there is something in your script that just filters out the requests that go from WebServices. Can you share the code please to have more details on this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...