Workflow error: org.mozilla.javascript.WrappedException: Wrapped org.mozilla.javascript.JavaScriptException: java.lang.NullPointerException

Tom Siegel
Kilo Guru

Hi all... I am getting the error org.mozilla.javascript.WrappedException: Wrapped org.mozilla.javascript.JavaScriptException: java.lang.NullPointerException (<refname>; line 51) in a "if" workflow script. You will see some gs.addInfoMessage entries, the code runs perfectly fine as an update business rule built on the Change Request table. Other than removing code until I find the culprit, is there any more efficient way to debug? I have seen a similar posting where the error also mentioned a line not contained in the script (i.e. 51). Is this being thrown be down stream ServiceNow code and being passed back up through? Here is the script.

answer=beforeNextCAB();

function beforeNextCAB(){
var startDate = new GlideDateTime(current.start_date);
var nextCAB = new CABLauncher().getNextCABMeeting();

//if next cab start - cab lead time(change property - 90 minutes) > now use this for next CAB end
var nextCABEnd = new CABLauncher().getNextCABMeetingEnd();

//else find next cab after nextCAB and us it's end time for nextCABEnd
var nextCABDate = new GlideDateTime(nextCAB);
var nextCABDateEndAdj = new GlideDateTime(nextCABEnd);
var leadTime = 0;
var grlt = new GlideRecord("u_lead_times");
grlt.addQuery("u_table", "change_request");
grlt.orderBy("u_order");
grlt.query();
while (grlt.next()) {
if (GlideFilter.checkRecord(current, grlt.u_condition, true)) {
leadTime = grlt.u_lead_time;
//gs.addInfoMessage("condition and lead time: " + grlt.u_condition + " : " + leadTime);
break;
}
}
var hours = 60 * 60 * leadTime;
nextCABDateEndAdj.addSeconds(hours);
//gs.addInfoMessage("Next CAB getNextCABMeeting: " + nextCABDate);
//gs.addInfoMessage("Next CAB End getNextCABMeetingEnd: " + nextCABDateEndAdj);
//gs.addInfoMessage("Start Date: " + startDate);
if (startDate.getNumericValue() < nextCABDateEndAdj.getNumericValue()) {
//gs.addInfoMessage("Less than x until next CAB");
return "yes";
} else {
//gs.addInfoMessage("More than x until next CAB");
return "no";
}
}

 

Thanks - Tom

 

11 REPLIES 11

Chris Sanford
Giga Guru

Ok so what I would do is go to scripts - background and run this script:

 

var sysID = "sys_id_of_change_here";
gs.info(beforeNextCAB(sysID));


function beforeNextCAB(sysID){
var current = new GlideRecord('change_request');
current.get(sysID);

var startDate = new GlideDateTime(current.start_date);
var nextCAB = new CABLauncher().getNextCABMeeting();

//if next cab start - cab lead time(change property - 90 minutes) > now use this for next CAB end
var nextCABEnd = new CABLauncher().getNextCABMeetingEnd();

//else find next cab after nextCAB and us it's end time for nextCABEnd
var nextCABDate = new GlideDateTime(nextCAB);
var nextCABDateEndAdj = new GlideDateTime(nextCABEnd);
var leadTime = 0;
var grlt = new GlideRecord("u_lead_times");
grlt.addQuery("u_table", "change_request");
grlt.orderBy("u_order");
grlt.query();
while (grlt.next()) {
if (GlideFilter.checkRecord(current, grlt.u_condition, true)) {
leadTime = grlt.u_lead_time;
//gs.addInfoMessage("condition and lead time: " + grlt.u_condition + " : " + leadTime);
break;
}
}
var hours = 60 * 60 * leadTime;
nextCABDateEndAdj.addSeconds(hours);
//gs.addInfoMessage("Next CAB getNextCABMeeting: " + nextCABDate);
//gs.addInfoMessage("Next CAB End getNextCABMeetingEnd: " + nextCABDateEndAdj);
//gs.addInfoMessage("Start Date: " + startDate);
if (startDate.getNumericValue() < nextCABDateEndAdj.getNumericValue()) {
//gs.addInfoMessage("Less than x until next CAB");
return "yes";
} else {
//gs.addInfoMessage("More than x until next CAB");
return "no";
}
}

 

Then you should let me know what is the output.

 

Also I'm not sure what is CABLauncher is that a script include OOB? I don't have it in my PDI in paris

Tom Siegel
Kilo Guru

Thanks for the reply Chris! CABLauncher is a custom Script include. Application is Global and Accessible From is "This Application Scope Only" 

 

 

Here is what I got back from the background script

CABLauncher.getNextCABMeeting: Next CAB: 2021-03-26 14:30:00
CABLauncher.getNextCABMeetingEnd: Next CAB: 2021-03-26 15:15:00
*** Script: no

Tom Siegel
Kilo Guru

The full error from the work flow log is: Less Than 'X' hours between next CAB and Start Date. Normal Change Risk Med or High(7f15dcf2db03ac10a2d6c25a13961975): org.mozilla.javascript.WrappedException: Wrapped org.mozilla.javascript.JavaScriptException: java.lang.NullPointerException (<refname>; line 51)

Any idea what the sys_id after the workflow step name is?

Thanks,

Tom

Ok well I think that sys ID is from the wf_activity table but not completely sure but if you can't find it there then you can always run this in scripts - background like

      
            function findSysID(sysId) {
                var tbls = ['sys_metadata', 'task', 'cmdb_ci', 'sys_user'];
                var rtrn;
                var i = 0;
                while (tbls[i]) {
                    rtrn = findClass(tbls[i], sysId);
                    i++;

                    if (rtrn) {
                        gs.print("###" + rtrn + "###")
                        return
                    };
                }

                var tblsGr = new GlideRecord("sys_db_object");
                tblsGr.addEncodedQuery("super_class=NULL^sys_update_nameISNOTEMPTY^nameNOT LIKEts_^nameNOT LIKEsysx_^nameNOT LIKE00^nameNOT LIKEv_^nameNOT LIKE$^nameNOT LIKEsys_rollback_^nameNOT LIKEpa_^nameNOT INsys_metadata,task,cmdb_ci,sys_user")
                tblsGr.query();
                while (tblsGr.next()) {
                    rtrn = findClass(tblsGr.getValue('name'), sysId);
                    if (rtrn) {
                        gs.print("###" + rtrn + "###")
                        return
                    };
                }
                function findClass(t, sysId) {
                    var s = new GlideRecord(t);
                    s.addQuery('sys_id', sysId);
                    s.setLimit(1);
                    s.queryNoDomain();
                    s.query();
                    if (s.hasNext()) {
                        s.next();
                        return s.getRecordClassName() + "^" 
                        + s.getClassDisplayValue() + " - " 
                        + s.getDisplayValue() ;
                    }
                    return false;
                }
            }
            findSysID('7f15dcf2db03ac10a2d6c25a13961975')