I have a script which has been working for several...
# suitescript
b
I have a script which has been working for several years that at some point stopped working but I am not sure why. It fires when a sales order is edited or created and sets an appropriate order status and pending reason based on the customers credit limit. Recently it doesn't seem to fire at all.
Copy code
/**
 *@NApiVersion 2.x
 *@NScriptType UserEventScript
 */

define([
    'N/search',
    'N/record',
    'N/runtime'
], function ValidateCreditLimit(
    Search,
    Record,
    Runtime
) {
    'use strict';

    return {
        afterSubmit: function afterSubmit(context) {
            if (Runtime.executionContext === Runtime.ContextType.WEBSTORE) {
                try {
                    switch (context.type) {
                    case 'edit':
                    case 'create':
                        var salesOrder = context.newRecord;
                        var customerId = salesOrder.getValue('entity');
                        var customer = Record.load({
                            type: Record.Type.CUSTOMER,
                            id: customerId
                        });

                        salesOrder = Record.load({
                            type: Record.Type.SALES_ORDER,
                            id: salesOrder.id
                        });

                        var unbilledOrders = customer.getValue('unbilledorders');
                        var balance = customer.getValue('balance');
                        var creditLimit = customer.getValue('creditlimit');
                        var terms = salesOrder.getValue('terms');

                        if (terms && (balance + unbilledOrders > creditLimit)) {
                            salesOrder.setValue('custbody_artinapendingapprovalreason', 10);
                            salesOrder.setValue('orderstatus', 'A');
                            salesOrder.save({
                                ignoreMandatoryFields: true
                            });
                        }

                        break;
                    default:
                        log.debug({
                            title: 'Not allowed UserEventType'
                        });
                        break;
                    }
                } catch (e) {
                    log.error({
                        title: 'Error in SCA | Validate Credit Limit',
                        details: e.message
                    });
                }
            }
        }
    };
});
e
A script not firing at all is usually a problem with the Script Deployment or because an error is being thrown earlier in the chain
s
Perhaps your context filter (WEBSTORE) is something to consider? Confirm that is the actual context for the use case where you think this isn't 'firing' - since if it's not that context then the script does nothing?
d
@bbeach the deployment is enabled? the script is active? also, check the executionContext
b
I redeployed the script, and it did fire on an order now, with an error.
d
which error ?
b
@DMG Error: Error in SCA | Validate Credit Limit details: This order has been partially or fully processed and may not be reset to 'Pending Approval'.
d
which SCA version are you running ?
c
That error comes from this script, and it indicates that NetSuite is preventing it from setting the context to "Pending Approval" since some quantity of one or more line items has been fulfilled or billed
b
@DMG in this example it came from SCA Vinson. But we also have other sites on 2022.1 @Clay Roper this was an order from our SCA site. It does not appear to be partially fulfilled. A related Work Order record was created however.
c
@bbeach Ah gotcha, that makes sense too
@bbeach It sounds like your default Sales Order status is Pending Fulfillment and the customer added an Assembly Item that has Special Work Order Item checked. Is that the case?
b
@Clay Roper yes that is the case. What is odd is this was working before.
Would changing this to a beforeSubmit script instead of afterSubmit address the issue?
c
@bbeach For create, probably