Requesting help in scheduled job script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2024 09:56 PM - edited 04-30-2024 10:53 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2024 10:27 PM
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);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2024 10:31 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2024 10:35 PM - edited 04-30-2024 10:36 PM
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]);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2024 11:31 PM
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....