Cannot convert to null error when it's not null

Nisar3
Giga Guru

In my BR, I've the following lines

(function executeRule(current, previous /*null when async*/ ) {



    // report DataCore
    var table = current.getTableName();
    var recSysId = current.sys_id;
    var eventName;
    var finalPayload = {};
    var payload = new global.Foo().bar(table, recSysId);
    var requestPayload = new global.JSON().decode(payload);
    finalPayload.table = table.toString();
    if (current.operation() == 'insert') {
        finalPayload.operation = 'create';
    } else {
        finalPayload.operation = current.operation().toString();
    }
    finalPayload.data = requestPayload;
    var sysHeader = gs.getProperty('gh.datacore.headervalues').split(',');

 

and the log says the following:

com.glide.script.RhinoEcmaError: Cannot convert null to an object.
sys_script.b8a9bfbb1b4de950c5722137b04bcb14.script : Line(16) column(0)
13: if (current.operation() == 'insert') {
14: finalPayload.operation = 'create';
15: } else {
==> 16: finalPayload.operation = current.operation().toString();
17: }
18: finalPayload.data = requestPayload;
19: var sysHeader = gs.getProperty('gh.datacore.headervalues').split(',');

 

Now what has stumped me are the following:

1. finalPayload is already defined as object on line 9

2. current.operation() is out of the box method

 

So what is causing the issue on line 16?

1 ACCEPTED SOLUTION

Nisar3
Giga Guru

It appears that having multiple BRs with the same order will cause the current.operation() to sometimes be null. I changed the order to lower value and it solved the problem. But the cause is still unknown as to why does it behave that way.

View solution in original post

8 REPLIES 8

Ravi Gaurav
Giga Sage
Giga Sage

Hi @Nisar3 

 

To avoid this error, you can first check if current.operation() is empty before calling .toString().


// Check if operation is an empty string or undefined, and handle accordingly
if (operation == '') {
finalPayload.operation = 'Define your own logic';

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

Hi Gaurav, 

My question stands - why would current.operation() be null in any scenario? The BR is set to run for "insert" and "update", so the value should be one of these, right?

Nisar3
Giga Guru

Perhaps @AnkurP can shed some light here.

Nisar3
Giga Guru

It appears that having multiple BRs with the same order will cause the current.operation() to sometimes be null. I changed the order to lower value and it solved the problem. But the cause is still unknown as to why does it behave that way.