Michael Sheely
Michael Sheely

Reputation: 1031

Netsuite Scheduled Script produces unexpected error I can't duplicate

I have a Scheduled SuiteScript 2.0 that uses a saved search to get a list of unpaid or partially paid invoices. The script iterates through each invoice and looks at the transaction lines. First I check to see if the transaction line is associated with a subscription since some transaction lines could be for something else. If I find a subscription ID, the idea is to load it and do additional things to that subscription because of its unpaid status. The problem is when the scheduled script runs, it's failing on the line that is trying to load the subscription record. Here is a snippet of the code, and then the error I receive:

/**
* @NApiVersion 2.x
* @NScriptType ScheduledScript
* @NAmdConfig /SuiteScripts/nsRequireConfig.json
*/
define(["require", "exports", "N/search", "N/record", "revlocal/utils/datefunctions", "N/log"], function (require, exports, search, record, dateUtils, log) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.execute = function (context) {
    if (context.type !== context.InvocationType.SCHEDULED) {
        return;
    }
    var s = search.load({ id: "customsearch_openinvoices" });
    var results = s.run().getRange(0, 1000);
    for (var _i = 0, results_1 = results; _i < results_1.length; _i++) {
        var r = results_1[_i];
        var invoiceId = parseInt(r.getValue({ name: "internalid", summary: search.Summary.GROUP }), 10);
        var dueDate = new Date(r.getValue({ name: "duedate", summary: search.Summary.GROUP }));
        var invoice = record.load({ type: record.Type.INVOICE, id: invoiceId });
        var count = invoice.getLineCount({ sublistId: "item" });
        for (var i = 0; i < count; i++) {
            try {
                var linePaid = invoice.getSublistValue({ sublistId: "item", line: i, fieldId: "custcol_rl_paiddate" });
                if (!linePaid) {
                    var subId = parseInt(invoice.getSublistValue({
                        sublistId: "item", line: i, fieldId: "subscription"
                    }), 10);
                    var sub = record.load({ type: record.Type.SUBSCRIPTION, id: subId });
        //  more code here that doesn't have anything to do with the problem
                }
            }
            catch (error) {
                log.error("Suspension on invoice failed: " + invoiceId, error);
                continue;
            }
        }
    }
};    
});

And here's the error:

{"type":"error.SuiteScriptError","name":"UNEXPECTED_ERROR","message":"An unexpected SuiteScript error has occurred","stack":["loadRecord_impl(N/recordImpl)","(/SuiteScripts/revlocal/scheduled/setBillingSuspension.js:27)"],"cause":{"type":"internal error","code":"UNEXPECTED_ERROR","details":"An unexpected SuiteScript error has occurred","userEvent":null,"stackTrace":["loadRecord_impl(N/recordImpl)","(/SuiteScripts/revlocal/scheduled/setBillingSuspension.js:27)"],"notifyOff":false},"id":"jwjlma0g1g7jovptug37j","notifyOff":false,"userFacing":false}

I have debugged the script and have loaded the subscription records that are failing with no problem so I can't duplicate the error except when the scheduled script runs. I've already spoke with Netsuite Support, and they didn't know what was wrong, which didn't surprise me. I'm hoping someone has had a similar experience and can help me out.

Upvotes: 0

Views: 3792

Answers (1)

Michael Sheely
Michael Sheely

Reputation: 1031

So it turns out that it was an Event Trigger script that was interfering with the scheduled script. Depending upon what's being done to records in a beforeLoad trigger could cause an error in the scheduled script. I removed the trigger, and have had success now. The work for the beforeLoad was work that could be done elsewhere, so the trigger was moved to afterSubmit.

Upvotes: 0

Related Questions