scheduled job: set closure(closed_at) for closed items

chrisn_
Mega Guru

Hello everyone,

I am faced with the unique problem of having a large number of RITM records not having a closure or (closed_at) value set. This was caused due to a known issue in SN where if a workflow does not have a timer, task or approval in it then certain business rules do not have time to run and complete the overall request process. This includes not changing the active flag for RITM's and as I have learned the closed_at date. I have a business rule that has fixed this from now on, but I am now faced with fixing the existing records. I need to run a scheduled job that will look at closed RITM's that has no closed_at value in it and set it to the updated field value as their final update was the closed_at date. Any help with this would be greatly appreciated as I am still quite a novice to background scripting like this. This is what I am attempting but I am not getting a list of the records back like I'd hoped before moving on to attempting to set the closed_at field.

 

var count = 0;
var gr = new GlideRecord('sc_req_item');
gr.addQuery('active','false');
gr.addQuery('closed_at','');
gr.query();
while(gr.next()){
count++;
// gr.closed_at = gr.sys_updated_on;
// gr.update();
}
gs.print(count + ' records updated');

6 REPLIES 6

Ahmmed Ali
Mega Sage

Hello,

 

try below script.

var count = 0;
var gr = new GlideRecord('sc_req_item');
//gr.addQuery('active','false');
//gr.addQuery('closed_at','');

gr.addEncodedQuery("active=false^closed_atISEMPTY");
gr.query();
while(gr.next()){
count++;
// gr.closed_at = gr.sys_updated_on;
// gr.update();
}
gs.print(count + ' records updated');

 

Thanks,

Ali

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

Hi Ahmmed,

I tried running this script, but I am not able to see the actual results of this. I thought the gs.print would show up in the log. Am I missing something? I just want to make certain that the query is grabbing the right records before I try to actually us the closed_at and update portions.

Hello,

 

gs.print() prints the script on the screen. if you run the above query in background script, then it will show results on the screen.

 

if you are running the script in scheduled job and want to see the result in system logs, then try below script.

var count = 0;
var gr = new GlideRecord('sc_req_item');
//gr.addQuery('active','false');
//gr.addQuery('closed_at','');

gr.addEncodedQuery("active=false^closed_atISEMPTY");
gr.query();
while(gr.next()){
count++;
// gr.closed_at = gr.sys_updated_on;
// gr.update();
}
gs.log(count + ' records updated',"record count"); //this will create a log entry with source as record count.

 

you can view the same in system logs with above mentioned source.

 

Hope this helps.

 

Please let me know for any further query/issue.

 

Thanks,

Ali

 

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

Hi Ahmmed,

Unfortunately it still is not working, I think the issue is that it is not returning any results at all from the query of the RITM table. I took a look and the closed_at field belongs to the task table, are we potentially querying the wrong table? The underlying issue is that the workflow in question did not have a task associated with it. Unfortunately this has lead to multiple critical fields such as the active flag and closed_at fields were not set correctly. This is an issue being fixed in london. I am really hoping I do not have to update these by hand, but right now the script is not returning any records at all to be updated.