1

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;
                }
            }
        }
    }
})
R. C.
  • 13
  • 4

1 Answers1

1

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?

  • Thanks for taking a look. I've run into issues with UE scripts not running when expected in the past, so it's good to know it may just be something on my end. I've added my script to my question. – R. C. Jan 28 '21 at 17:42
  • In the deployment of the User Event script, is the Web Services context excluded? – Suite Resources Jan 28 '21 at 19:30
  • Deployment looks correct. I noticed that the script wasn't running on Invoices imported from NSPOS. I'm not able to test them right now, but I was able to test a different integration for Sales Orders and my UE script ran fine against those. – R. C. Jan 28 '21 at 22:55