adn
adn

Reputation: 99

TypeError: Cannot call method "getField" of null User Event

I am receiving an error on a script I just released to production (did not receive error in sandbox testing)

Error: TypeError: Cannot call method "getField" of null User Event

-This is strange because from what I can tell it is not causing issues with my solution (it works completely the way it's supposed to.)

Here is the code:

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 */
define(['N/record', 'N/ui/serverWidget', 'N/url', 'N/runtime', 'N/search'],

function(record, serverWidget, url, runtime, search) {
    function beforeLoad(context){
        var form = context.form;
        var sublist = form.getSublist({
            id : 'item'
        });

        var recObj = context.newRecord
        var recId = recObj.id;
        var currUser = runtime.getCurrentUser();
        var numLines = recObj.getLineCount({sublistId: 'item'});
        var sessionScope = runtime.getCurrentSession().get({  ////remove after debug
            name: 'scope'
        });
        log.debug('Tansaction: ' + recObj.getValue({fieldId: 'tranid'}))
        log.debug('runtime.executionContext: ' + runtime.executionContext)///remove after debug
        var isConnectedField = sublist.getField({id: 'custcol_isconnected'});//this is line 25
        var connectedPoField = sublist.getField({id: 'custcol_connected_po'});

Here is the log:

DEBUG | runtime.executionContext: USERINTERFACE

SYSTEM | org.mozilla.javascript.EcmaError: TypeError: Cannot call method "getField" of null (/SuiteScripts/rei_userEvent_so.js#25) JS_EXCEPTION

Any Insight into why this may be happening would be appreciated.

Upvotes: 0

Views: 706

Answers (1)

bknights
bknights

Reputation: 15367

We'd need to know a little more about what transaction types you've deployed this against but you mention multiple types. Simple answer is that not all transactions have an items sublist.

e.g. neither vendorbill nor deposit have an item sublist

You could short circuit this like:

var sublist = form.getSublist({
  id : 'item'
});

if(!sublist) return; // not one of the transactions of interest

There may also be types of events where the sublist isn't available. You're code doesn't show filtering for type. You could try something like this just before you reference the form. I've had some strange UserEvent issues when I don't do filter for type:

if (context.type != ctx.UserEventType.VIEW && context.type != ctx.UserEventType.EDIT && context.type != ctx.UserEventType.CREATE) return;

Upvotes: 2

Related Questions