- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-18-2022 11:27 AM
Can anyone please tell me what is the mistake with this code.
I am trying to update state field in the Incident table. If the state is on hold for more than 24 hrs it should reset to In progress.
The code is working. I am able to see the log messages in the sys_log table but the state field in the Incident table is not getting updated to In Progress.
This is a before business rule.
**************************************************************************************************************************************
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var gr = GlideRecord('incident');
gr.addQuery('state', '3');
gr.query();
while (gr.next()) {
var updated_date = gr.sys_updated_on;
var present_date = new GlideDateTime();
var gdt1 = new GlideDateTime(updated_date);
var miliseconds = GlideDateTime.subtract(gdt1, present_date).getNumericValue();
var total_hours = parseInt(Math.floor(((miliseconds / 1000) / 60) / 60));
gs.log("state before update = " + gr.state); //state before update=3
if (total_hours >= 24) {
gr.setValue('state', '2');
gs.log("state updated= " + gr.state); // state updated=2
}
}
})(current, previous);
Thanks in Advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-18-2022 12:05 PM
For this, you need to use a scheduled job that runs daily:
var s= new GlideRecord("incident");
s.addEncodedQuery("state=3^sys_updated_on<javascript:gs.beginningOfToday()");
s.autoSysFields(false); // use if you don't want update system fields
s.setWorkflow(false);
s.query();
while(s.next())
{
s.setValue("state",2);
s.update()
}
Mark Correct and helpful
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-18-2022 12:05 PM
For this, you need to use a scheduled job that runs daily:
var s= new GlideRecord("incident");
s.addEncodedQuery("state=3^sys_updated_on<javascript:gs.beginningOfToday()");
s.autoSysFields(false); // use if you don't want update system fields
s.setWorkflow(false);
s.query();
while(s.next())
{
s.setValue("state",2);
s.update()
}
Mark Correct and helpful
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-19-2022 02:37 AM
Hi,
you can also achieve this using flow designer with no script.
Did you explore that?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader