The CreatorCon Call for Content is officially open! Get started here.

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

Ankur Bawiskar
Tera Patron
Tera Patron

@snow_beginner 

CONTAINS won't work in encodedQuery

you need to use LIKE

var grXBBCNN = new GlideRecord('x_btesi_bt_cmdb_network_nodes');
grXBBCNN.addEncodedQuery("u_ntn_managed_flag=Y^ntn_man_host_nameLIKElbg^sys_updated_onRELATIVEGT@dayofweek@ago@2");
grXBBCNN.query();
while (grXBBCNN.next()) {
    // Defensive checks before accessing fields
    var ndpId = grXBBCNN.getValue('ntn_ndp_id');
    if (ndpId) {
        var grXBBCPNPT = new GlideRecord('x_btesi_bt_cmdb_port_node_products');
        if (grXBBCPNPT.get(ndpId)) {
            // safe to getElement or getValue
            // ...
        }
    }
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar 

I mentioned that I already tried 1 encoded query with all the filters and just copy pasted it and it does use LIKE, but that is not working.

@snow_beginner If you are using encodedQuery it should be “LIKE” instead of “CONTAINS”

 

It will fetch every string containing your string in that column.


Please mark the answer correct/helpful accordingly.


Raghav
MVP 2023
LinkedIn

Hi @RaghavSh 

var grXBBCNN = new GlideRecord('x_btesi_bt_cmdb_network_nodes');
grXBBCNN.addEncodedQuery("(u_ntn_managed_flag=Y^ntn_man_host_nameLIKElbg^sys_updated_onRELATIVEGT@dayofweek@ago@2");

// SFSTRY0038587: only bring in records starting LBG- (any case)
//grXBBCNN.addEncodedQuery("ntn_man_host_nameLIKEBG");
// end SFSTRY0038587

//grXBBCNN.setLimit(22);
grXBBCNN.query();
while (grXBBCNN.next()) {

This is what I have in there. I tried with just 1 encoded query like Ankur mentioned and it uses LIKE. 

 

It's commented out now, but I also tried it like below

 

var grXBBCNN = new GlideRecord('x_btesi_bt_cmdb_network_nodes');
grXBBCNN.addEncodedQuery("(u_ntn_managed_flag=Y^sys_updated_onRELATIVEGT@dayofweek@ago@2");

// SFSTRY0038587: only bring in records starting LBG- (any case)
grXBBCNN.addEncodedQuery("ntn_man_host_nameLIKEBG");
// end SFSTRY0038587

 

Both are not picking up the relevant record that I made and updated today (an hour ago) or any of the other ones made/updated in the last few days.