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.

Requesting help in scheduled job script

Supriya25
Tera Guru

Hi Team,

 

I have ran Scheduled Job script in Background script for testing, but not sure why it is NOT working properly .

var arr=[];
var gr = new GlideRecord('Scoped application Table name');

gr.addEncodedQuery('State is not Resolved,Closed, Cancelled');

gr.query();

while(gr.next()){

gs.info('Gate 1 '+gr.sys_id);

arr.push(gr.sys_id);

}

gs.info("Array Length "+arr.length); // getting result 30, which is correct

for(var i=0;i<arr.length;i++){

var ttgr = new GlideRecord('sys_history_line');

ttgr('oldISNOTEMPTY^set.id='+arr[i]);

ttgr.query();

if(ttgr.next())

{

gs.info('Gate 2 '+ttgr.set+"  "+arr[i]); // here I'm getting only 5-sysID details  , which is wrong

}
}

why system giving 5-sysId details only out of 30 -sysID's  from Second loop, I'm not understanding why this wrong result coming.

 

 

when I search sysID directly in sys_history_line table there data is available, but not resulting from Script.

 

12 REPLIES 12

Community Alums
Not applicable

Hi @Supriya25 ,

Try again by updating the below code-

for(var i=0; i < arr.length; i++) {
var ttgr = new GlideRecord('sys_history_line');
ttgr.addQuery('set.id', arr[i]); 
ttgr.query();
while(ttgr.next()) {
gs.info('Gate 2 ' + ttgr.set + " " + ttgr.sys_id);
}
}

Deepak Shaerma
Kilo Sage
Kilo Sage

Hi @Supriya25 

 Inside the loop, your condition to print log messages (if(gr.next())) is mistakenly using gr instead of ttgr. This is likely a copy-paste error. You should be checking if ttgr has any records after the query.

 

var arr = [];
var gr = new GlideRecord('your_scoped_application_table_name');

gr.addEncodedQuery('state!=Resolved^state!=Closed^state!=Cancelled');
gr.query();

while (gr.next()) {
    gs.info('Gate 1 ' + gr.sys_id);
    arr.push(gr.sys_id.toString());
}

gs.info("Array Length " + arr.length);

// Correctly iterating through the Array of sys_ids
for (var i = 0; i < arr.length; i++) {
    var ttgr = new GlideRecord('sys_history_line');
    ttgr.addQuery('old', 'ISNOTEMPTY');
    ttgr.addQuery('set.id', arr[i]); 
    ttgr.query();

    while (ttgr.next()) { // Correctly iterating through the matching history lines
        gs.info('Gate 2 ' + ttgr.set + "  " + ttgr.sys_id); // Now correctly using ttgr instead of gr
    }
}

 

Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma 


sorry it was wrong typo when I was raising request here , but in Original code I'm using correct object.
but no luck 

 

var arr=[];
var gr = new GlideRecord('Scoped application Table name');
gr.addEncodedQuery('State is not Resolved,Closed, Cancelled');
gr.query();
while(gr.next()){
gs.info('Gate 1 '+gr.sys_id);
arr.push(gr.sys_id);
}
gs.info("Array Length "+arr.length); // getting result 30, which is correct

for(var i=0;i<arr.length;i++){
var ttgr = new GlideRecord('sys_history_line');
ttgr.addEncodedQuery('oldISNOTEMPTY^set.id='+arr[i]);
ttgr.query();
if(ttgr.next())
{
gs.info('Gate 2 '+ttgr.set+"  "+arr[i]); 
}
}

 

 

Try :

 

var arr = [];
var gr = new GlideRecord('Scoped application Table name');
gr.addEncodedQuery('StateNOT INResolved,Closed,Cancelled'); 
gr.query();

while (gr.next()) {
    gs.info('Gate 1 ' + gr.sys_id);
    arr.push(gr.sys_id);
}

gs.info("Array Length " + arr.length);

for (var i = 0; i < arr.length; i++) {
    var ttgr = new GlideRecord('sys_history_line');
    ttgr.addQuery('set.id', arr[i]); 
    ttgr.addNotNullQuery('old'); 
    ttgr.query();

    while (ttgr.next()) { 
        gs.info('Gate 2 ' + ttgr.getDisplayValue('set') + "  " + arr[i]);
    }
}

 

 

I hope this helps..


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect