How to calculate date time of a state when it moves from new to in progress on incident form?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2022 06:16 AM
I have a requirement on incident form.
i need to calculate time when state moves from new to in progress on incident form.
for this i have created a before update business rule and in advanced
condition state is in progress
var gdt1 = new GlideDateTime(current.sys_created_on);
var gdt2 = new GlideDateTime(current.sys_updated_on);
var statetime = GlideDateTime.subtract(gdt1, gdt2); //the difference between gdt1 and gdt2
current.state_time=statetime; //state_time field is date/time
i am getting weird values and getting inappropriate values. Please help me for the above calculation to track state time when state moves from new to in progress ...
- Labels:
-
Incident Management

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2022 06:21 AM
You can use below logic:
var gdt1 = new GlideDateTime(current.sys_created_on);
var gdt2 = new GlideDateTime(current.sys_updated_on);
var statetime = gs.dateDiff(gdt1.getDisplayValue(), gdt2.getDisplayValue(), true); //the difference between gdt1 and gdt2
current.state_time = new GlideDuration(Number(statetime)*1000);
Feel free to mark correct, If I answered your query.
Will be helpful for future visitors looking for similar questions 🙂
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2022 06:37 AM
i have tried above script but it was not working throwing error for the below line saying to use GlideDateTime.subtract instead gs.dateDiff
var statetime = gs.dateDiff(gdt1.getDisplayValue(), gdt2.getDisplayValue(), true); //the difference between gdt1 and gdt2
it also throwing error for the below line
current.state_time = new GlideDuration(Number(statetime)*1000);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2022 09:50 PM
Are you in scoped app?
Can you try to add logs for statetime field, what value you are getting there?
Also, you state_time field, I hope this is duration type field?
Aman Kumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2022 07:16 AM
Hi,
There is OOB functionality to do that Metrics
But if you want script approach
Try this.
var gdt1 = new GlideDateTime(current.sys_created_on);
var gdt2 = new GlideDateTime(current.sys_updated_on);
var statetime = gs.dateDiff(gdt1.getDisplayValue(), gdt2.getDisplayValue(),true);
current.state_time=statetime;
see this also to get an idea and make changes.
Mark Correct or Helpful if it helps.
***Mark Correct or Helpful if it helps.***