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

Valmik Patil1
Kilo Sage

Hello @Nisar3 ,

 

Could you please log what current.operation returns in your case.

I would suggest to add logs for Payload and requestPayload variable as well.

So that we can get what values they have.

 

Thanks,

Valmik Patil

 

Thanks,

Valmik Patil

The error is from Prod, so difficult to add statements there directly. I haven't had luck reproducing it in dev instances.

 

But still, I don't see a need to print those as current.operation is ServiceNow method and not custom, so it's expected to have a value always and secondly the line error says line 16 so Payload and requestPayload variables are out of scope, right?

dgarad
Giga Sage

Hi @Nisar3 

 apply below code.

if(current.operation != null)
        finalPayload.operation = current.operation().toString();
If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

Interesting. Do you see any case where the out of the box method will be null?