How to calculate the state duration from the starting to the current time using on Load Client script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-13-2022 05:00 AM
We got a requirement to calculate the on total Time of when the state is in on Hold State from beginning to the current time using On Load Client Script.
Any Suggestion will be appreciated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-13-2022 05:26 AM
Hi Samudra,
May I know this is for which table?
If this is for incident table then we already have "Incident Metric" table which stores duration state wise so you can get duration data from incident metric table.
If this is other than incident table then you could try writing metric on this table and get the data with client script.
There are different ways with which you can get data through client script from Metric table, I would suggest to use below approach.
1. Using Display BR
- Write Display BR and get duration from Metric table by writing script and store that data in scratchpad object.
- Write on Load Client script and access scratchpad object and perform your further operation.
2. You can use GlideAjax in client script and get the required data.
Let me know if you have any further queries.
Please mark this as Correct or helpful if it helps.
Thanks and Regards,
Abhijit
Regards,
Abhijit
ServiceNow MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-13-2022 08:17 AM
Hi Abhijit,
Thanks for your time. You are correct this is for the incident table.
As suggested by you I have used a BR which to calculate the Total on hold time based on the Metric table. The BR calculates the total on hold time when the state changes from the on hold to any other state, and it also sums up the duration when the the state again changes to on hold and then to other state.
But the req. is that we need to also calculate the real time on hold value when the form is reloaded.
We can achieve it using the On Load client script, but we are having some issues with the logic of client script.
Please can you help with the logic.
Thanks in advance.
Thanks,
Samudra
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-14-2022 11:19 PM
Hi Samudra,
Your scenario is not quite clear for me. Do you mean if incident current state is "On Hold" then you would like to calculate the time for which it is on hold? (because current state duration values are not stored on incident metric table).
If this is the case then currently we do not have any way to calculate time difference on client side. You would need to achieve this with server side script.
I would suggest to go with Display BR where you can calculate date difference from Today's Date and start field from Metric instance table as shown below. The start field on incident metric table stores value when incident was moved to that particular state( in your case it is on hold state).
Once you calculate duration in display BR then you can store that value on scratchpad object and access the same value in on load client script and perform your further task.
Let me know if you have any further queries.
Please mark this as Correct or Helpful if it helps.
Thanks and Regards,
Abhijit
Regards,
Abhijit
ServiceNow MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-29-2022 04:18 AM
Hello
Can u please share the BR to calculate the total on Hold time , i have some requirment but here i am calculating the Total Time from Business elapsed time from SLA table
P:N: This is not for incident table its for Case table
I have written the BR but it not giving me Appropriate result .
if (current.workflow_status_mob.changesFrom('On Hold - Account Team') || (current.workflow_status_mob=="On Hold - Account Team")) {
var gr = new GlideAggregate("task_sla");
gr.addAggregate('SUM', 'business_duration');
gr.addQuery('task.number', caseID);
gr.addQuery('sla.name', 'MCOE On Hold -Account Team (MIS)');
gr.setGroup(false);
gr.query();
while (gr.next()) {
gs.addInfoMessage("BusinessDuration"+gr.business_duration);
var sum = gr.getAggregate('SUM', 'business_duration');
gs.addInfoMessage("SUMTIME" + sum);
var duration = new GlideDuration(sum);
var duration2 = new GlideDuration(duration);
gs.addInfoMessage("Duration" + duration2.getDisplayValue());
var totaltime = duration2.getDisplayValue();
gs.addInfoMessage("TOTALTIME" + totaltime);
current.total_time_on_hold_account_team = totaltime;
current.update();