Need help with a piece of code
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2023 12:33 PM - edited 12-14-2023 12:34 PM
My requirement is to update the state of an incident to "in progress" if the pause duration is more than 24 hours. The SLA is paused when the agent changes the state to "on Hold".
I found this piece of code on the community but I am not sure if this is working exactly for my situation as I cannot see the 24 hour time being calculated.
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();
}
I am new to Servicenow coding. Can someone pls help me understand this code, line by line preferably?
many thanks in advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2023 01:32 PM
The issue is with 'javascript:gs.beginningOfToday()'. This makes sense in a list or report since JavaScript needs to be explicitly stated to be executed. But in an encoded query where we are already scripting, we don't need to explicitly state javascript. Try the below.
var grIncident = new GlideRecord("incident");
grIncident.addEncodedQuery("state=3^sys_updated_on" + gs.beginningOfToday());
grIncident.autoSysFields(false); // use if you don't want update system fields
grIncident.setWorkflow(false);
grIncident.query();
while(grIncident.next()) {
grIncident.setValue("state",2);
grIncident.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2023 08:01 PM
Hi @Ghata
The API beginningOfToday will give you the result of the date time for the beginning of today in GMT. So it might not give you the correct duration with 24 hours.
If you would like to base on the Update Time directly on that Incident record. You can try the sample script below.
var grIncident = new GlideRecord('incident');
grIncident.addQuery('state', 3);
grIncident.query();
while(grIncident.next()){
//The current date time
var gdtNow = new GlideDateTime();
//The updated date time + 1 days
var updatedTime = grIncident.getDisplayValue('sys_updated_on');
var gdtUpdatedTime = new GlideDateTime(updatedTime);
gdtUpdatedTime.addDaysUTC(1);
if(gdtUpdatedTime <= gdtNow){
grIncident.setValue('state', 2);
grIncident.update();
}
}
Your requirement seems to base on the SLA, so you can consider to use the Pause Time field in the Task SLA [task_sla] table.
Cheers,
Tai Vu