Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Scheduled job not picking up relevant records based on one small change to the filter.

snow_beginner
Giga Guru

Hi, I was recently assigned to change the fitler criteria of a scheduled job. The filter was basically to look into table 1 and pick all the records which start with lbg- and put them into another table if they had been updated in the last 2 days, the requirement was to change that to pick records which contains lbg. So literally just an operator change. When I first did the change in dev it worked, but when the change moved to test it stopped working, now its not working in dev either.

 

Here is the code below as it was when I didn't change it:

 

//Review all records updated from last week from Network Nodes table
var grXBBCNN = new GlideRecord('x_btesi_bt_cmdb_network_nodes');

grXBBCNN.addEncodedQuery("u_ntn_managed_flag=Y^sys_updated_onRELATIVEGT@dayofweek@ago@2");
//grXBBCNN.addEncodedQuery("ntn_man_host_nameSTARTSWITHLBG-");

//grXBBCNN.setLimit(22);
grXBBCNN.query();
while (grXBBCNN.next()) {
	
    //Initialize BT Network Node table
    var grUCCBNN = new GlideRecord('u_cmdb_ci_bt_network_node');

    //Get BT Product Type and Model Number from Port Node Product table
    var grXBBCPNPT = new GlideRecord('x_btesi_bt_cmdb_port_node_products');
    grXBBCPNPT.get(grXBBCNN.getValue('ntn_ndp_id'));
    var btProductType = grXBBCPNPT.getElement('ndp_ndt_id.ndt_name').getValue() || '';
	var modelNumber = grXBBCPNPT.getValue('ndp_name') || '';

... rest of script which picks the fields and does things
	
	

 

I literally only changed the line 

grXBBCNN.addEncodedQuery("ntn_man_host_nameSTARTSWITHLBG-"); to
grXBBCNN.addEncodedQuery("ntn_man_host_nameCONTAINSLBG"); and didn't touch anything else in the rest of the code. Like I said it worked once in dev, but didn't work in test and now when I look at it in dev, its again not working there either. 
 
Here is what I have tried so far:
 
I built the actual filter in the table and saw that when you copy query, you get LIKE not contains so I changed it to grXBBCNN.addEncodedQuery("ntn_man_host_nameLIKELBG"); - it didn't work.
 
I also tried to have just one encoded query like 
('u_ntn_managed_flag=Y^ntn_man_host_nameLIKELBG^ORntn_man_host_nameLIKElbg^sys_updated_onRELATIVEGT@dayofweek@ago@2'); - doesn't work
 
I have tried to split each query like:
 
var twoDaysAgo = new GlideDateTime();
twoDaysAgo.addDaysUTC(-2);

grXBBCNN.Query('u_ntn_managed_flag', 'Y');
grXBBCNN.Query('sys_updated_on','>=', twoDaysAgo); // tried to do this <= as well and didnt work
grXBBCNN.addQuery('ntn_man_host_name','CONTAINS','LBG');
 
What's more interesting is, if I take the script from another instance where it hasn't been changed and paste it into background script, I don't get an error. But if I paste the changed script (first change of changing startswith to contains) I get an error
 
Script execution error: Script Identifier: null.null.script, Error Description: java.lang.NullPointerException: Cannot invoke "Object.toString()" because "value" is null, Script ES Level: 0
Couldn't decipher the stack trace resulting from the following JavaScriptException:
java.lang.NullPointerException: Cannot invoke "Object.toString()" because "value" is null: org.mozilla.javascript.JavaScriptException: java.lang.NullPointerException: Cannot invoke "Object.toString()" because "value" is null: org.mozilla.javascript.Context.makeJavaScriptException(Context.java:2631) org.mozilla.javascript.Context.throwAsScriptRuntimeEx(Context.java:2615) org.mozilla.javascript.MemberBox.invoke(MemberBox.java:243)

 

Can someone please tell me what's going on and why its not working at all anymore.

12 REPLIES 12

@snow_beginner Try splitting or combining because LIKE is very commonly used and we all have been using it over the years successfully.


Raghav
MVP 2023
LinkedIn

@snow_beginner Did this fix your issue?


Raghav
MVP 2023
LinkedIn

Hi @RaghavSh 

no it has not solved the issue, when I run it in background scripts, I get records, but they are undefined, even though I did name them. Here is what I ran in the backgroun scripts:

 

(function execute() {
var grXBBCNN = new GlideRecord('x_btesi_bt_cmdb_network_nodes');
gs.info("==== Records with CONTAINS LBG ====");
    
    grXBBCNN.addQuery('u_ntn_managed_flag', 'Y');
    grXBBCNN.addNotNullQuery('u_ntn_man_host_name');
    grXBBCNN.addQuery('u_ntn_man_host_name', 'LIKE', '%LBG%');
	grXBBCNN.addQuery('sys_updated_onRELATIVEGT@dayofweek@ago@2');

grXBBCNN.query();
 while (grXBBCNN.next()) {
        gs.info("Matched (CONTAINS): " + grXBBCNN.u_ntn_man_host_name);
    }
})();

 

and here is what I got in return. It finds the few records which match the query, but they are undefined.

*** Script: ==== Records with CONTAINS LBG ====
*** Script: Matched (CONTAINS): undefined
*** Script: Matched (CONTAINS): undefined
*** Script: Matched (CONTAINS): undefined
*** Script: Matched (CONTAINS): undefined
*** Script: Matched (CONTAINS): undefined
*** Script: Matched (CONTAINS): undefined