Cannot convert null to an object (scripts background)

robhaas
Tera Contributor

Very simply put, I am running a query in scripts background and am getting the error 'cannot convert null to an object'. I'm not sure which piece is pulling back null. Can anyone advise what I can change in the script?

The logs say the error is in line 5. - 'org.mozilla.javascript.EcmaError: Cannot convert null to an object.'

var gr = new GlideRecord('x_access_request');

gr.query();

while (gr.next()){

if (gr.getValue('receivers').contains(gr.getValue('opened_by'))) {

gs.print('Number : ' + gr.number);

gs.print('Requestor : ' + gr.opened_by);

gs.print('Receivers : ' + gr.getValue('receivers'));

}

}

TIA!

6 REPLIES 6

subramanian
ServiceNow Employee
ServiceNow Employee

var gr = new GlideRecord('x_access_request');

gr.query();

 

while (gr.next()){

var receivers = gr.getValue("receivers");

if (receivers && receivers.contains(gr.getValue('opened_by'))) {

gs.print('Number : ' + gr.number);

gs.print('Requestor : ' + gr.opened_by);

gs.print('Receivers : ' + gr.getValue('receivers'));

}

}

 

the above changes should make it work. 

1. gr.getValue("") was not expected to return null - but that seems to be happening here AND

2. null.contains() is causing the code to fail

 

Better to do a null check on the object before using it to invoke a property on it - a general safe coding practice.

saikatmitra
Tera Contributor

Hi All,

I am also getting a similar error while enhancing a catalog item and getting the following error. Can anyone of you please advise me on this. There is a variable called "affected_ci_12" in my catalog item and it is mandatory and it is being filled but unfortunately each time the form is submitted, I am getting the below error and this particular variable question is appearing empty in both RITM as well as in task level. Please help.

 

Exception (TypeError: Cannot convert null to an object. (sys_script_include.4c6855377522d0004c6382de47d76747.script; line 2)) occured while evaluating'Condition: gup('sysparm_changeset',gs.getSession().getStack().top())!=''' in business rule 'Propose relationship query' on u_propose_relationships:Created ; skipping business rule