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

Hi @Sohail Khilji ,

I tried below script without conditions, you please closely monitor Background result 

Let's take Incident table we have 900+ history record are here

Supriya25_0-1714566209708.png

 



which I'm running below script and  Output is below :

 Background script :

var arr=[];
var gr = new GlideRecord('incident');
gr.addEncodedQuery('state=1');
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.addEncodedQuery('set.id='+arr[i]);
ttgr.query();
if(ttgr.next())
{
gs.info('Gate 2 '+ttgr.set+"  "+arr[i]); 
}
}

 

*** Script: Gate 1 471bfbc7a9fe198101e77a3e10e5d47f
*** Script: Gate 1 0c5f3cece1b12010f877971dea0b1449
*** Script: Gate 1 552c48888c033300964f4932b03eb092
*** Script: Gate 1 57af7aec73d423002728660c4cf6a71c
*** Script: Gate 1 78271e1347c12200e0ef563dbb9a7109
*** Script: Gate 1 85071a1347c12200e0ef563dbb9a71c1
*** Script: Gate 1 965c9e5347c12200e0ef563dbb9a7156
*** Script: Gate 1 a623cdb073a023002728660c4cf6a768
*** Script: Gate 1 a83820b58f723300e7e16c7827bdeed2
*** Script: Gate 1 ed92e8d173d023002728660c4cf6a7bc
*** Script: Gate 1 f12ca184735123002728660c4cf6a7ef
*** Script: Gate 1 ff4c21c4735123002728660c4cf6a758
*** Script: Gate 1 a2496c05731110107418660c4cf6a711
*** Script: Gate 1 a9e30c7dc61122760116894de7bcc7bd
*** Script: Gate 1 d71da88ac0a801670061eabfe4b28f77
*** Script: Array Length 15
*** Script: Gate 2 726ab74b97f9021014e4f93de053afa2  d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 726ab74b97f9021014e4f93de053afa2  d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 726ab74b97f9021014e4f93de053afa2  d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 726ab74b97f9021014e4f93de053afa2  d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 726ab74b97f9021014e4f93de053afa2  d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 726ab74b97f9021014e4f93de053afa2  d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 726ab74b97f9021014e4f93de053afa2  d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 726ab74b97f9021014e4f93de053afa2  d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 726ab74b97f9021014e4f93de053afa2  d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 726ab74b97f9021014e4f93de053afa2  d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 726ab74b97f9021014e4f93de053afa2  d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 726ab74b97f9021014e4f93de053afa2  d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 726ab74b97f9021014e4f93de053afa2  d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 726ab74b97f9021014e4f93de053afa2  d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 726ab74b97f9021014e4f93de053afa2  d71da88ac0a801670061eabfe4b28f77

 

exact issue repeating in client instance too. 


if we take  "a623cdb073a023002728660c4cf6a768" this sysid have not gone through   from second block of code 'sys_history_line' in OUTPUT.
var ttgr = new GlideRecord('sys_history_line');
ttgr.addEncodedQuery('set.id='+arr[i]);

if I manually go and check with Sysid there data existed  in table  , so script it has to check and return info message from second loop sys_history_line block  right. but it is not happening 

Supriya25_1-1714566209514.png

 

 



I have tried your code as well

var arr=[];
var gr = new GlideRecord('incident');
gr.addEncodedQuery('state=1');
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]);
}
}
OUTPUT :  Incident: INC0000054 d71da88ac0a801670061eabfe4b28f77 coming every time
[0:00:00.534] Script completed in scope global: script


Script execution history and recovery available here


*** Script: Gate 1 471bfbc7a9fe198101e77a3e10e5d47f
*** Script: Gate 1 0c5f3cece1b12010f877971dea0b1449
*** Script: Gate 1 552c48888c033300964f4932b03eb092
*** Script: Gate 1 57af7aec73d423002728660c4cf6a71c
*** Script: Gate 1 78271e1347c12200e0ef563dbb9a7109
*** Script: Gate 1 85071a1347c12200e0ef563dbb9a71c1
*** Script: Gate 1 965c9e5347c12200e0ef563dbb9a7156
*** Script: Gate 1 a623cdb073a023002728660c4cf6a768
*** Script: Gate 1 a83820b58f723300e7e16c7827bdeed2
*** Script: Gate 1 ed92e8d173d023002728660c4cf6a7bc
*** Script: Gate 1 f12ca184735123002728660c4cf6a7ef
*** Script: Gate 1 ff4c21c4735123002728660c4cf6a758
*** Script: Gate 1 a2496c05731110107418660c4cf6a711
*** Script: Gate 1 a9e30c7dc61122760116894de7bcc7bd
*** Script: Gate 1 d71da88ac0a801670061eabfe4b28f77
*** Script: Array Length 15
*** Script: Gate 2 Incident: INC0000054 d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 Incident: INC0000054 d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 Incident: INC0000054 d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 Incident: INC0000054 d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 Incident: INC0000054 d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 Incident: INC0000054 d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 Incident: INC0000054 d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 Incident: INC0000054 d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 Incident: INC0000054 d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 Incident: INC0000054 d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 Incident: INC0000054 d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 Incident: INC0000054 d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 Incident: INC0000054 d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 Incident: INC0000054 d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 Incident: INC0000054 d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 Incident: INC0000054 d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 Incident: INC0000054 d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 Incident: INC0000054 d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 Incident: INC0000054 d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 Incident: INC0000054 d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 Incident: INC0000054 d71da88ac0a801670061eabfe4b28f77
*** Script: Gate 2 Incident: INC0000054 d71da88ac0a801670061eabfe4b28f77

  

this message has to be repeated 30-times right .

gs.info('Gate 2 '+ttgr.set+"  "+arr[i]); 

, but it's just repeating 5times only. skipping remaining  sys-ids 


can you please explain me your exact requirement, what you are trying to achieve in order to get complete details.

I'm trying to update 33 -records with their Old Values

"Resolved", "Resolved By", "close notes" and set state = resolved

in order to get them I tried above code, but not sure where going wrong code only considering 5-sys_id only. it was surprising to me. why only 5, why not remaining too.