- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2024 09:53 PM
Hello Experts,
We need to calculate the incident state how much minutes "In progress" and store in "u_state_calc" field
for below code is working fine.
but whenever any other field is updated its capturing that time duration instead of WIP.
Requirement
1.How much minutes state was inprogress.
After - insert
Condition - stage changesfrom WIP
Field name - "u_state_calc"
Field type - "Integer"
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2024 04:15 AM
Hi @akin9,
Try with following BR.
Type : After -> Update
condition : state -> changes from -> in progess
code :
on incident form store field type is integer.
Please mark my response as correct and helpful if it helped solved your question.
Thanks,
Rohit Suryawanshi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2024 10:14 PM
Hi @akin9
- Record the start time when the state is set to "In Progress".
- Calculate the duration only when transitioning out of the "In Progress" state.
try below script
(function executeRule(current, previous /*null when async*/) {
if (current.operation() == 'update') {
// state has changed from "In Progress" (2) to other state
if (previous.stage == '2' && current.stage != '2') {
// fetch the start time from the previous record
var wipStartTime = new GlideDateTime(previous.u_wip_start_time);
var wipEndTime = new GlideDateTime();
// Calculate the duration in milliseconds
var durationMillis = wipEndTime.getNumericValue() - wipStartTime.getNumericValue();
var durationMins = Math.floor(durationMillis / (1000 * 60));
current.u_state_calc = durationMins;
}
// Check if the state has changed to "In Progress" (2)
else if (previous.stage != '2' && current.stage == '2') {
// Record the start time when entering "In Progress"
current.u_wip_start_time = new GlideDateTime().getValue();
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2024 10:43 PM
Hi @Sid_Takali
Thank you for the Quick reply i tried the above script but no luck.
I tried with 3 minutes out from inprogress but result "u_state_calc = 28,768,656".
We are not created this fied - "u_wip_start_time".
we have "u_statrt" field its capturing WIP start minutes from the sys_created_on using seperate BR.
Can we use this or any other approach .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2024 10:20 PM
Hi @akin9,
You may follow the following thread.
https://www.servicenow.com/community/developer-forum/calculating-how-much-time-elapsed-on-each-state...
Please mark my response as correct and helpful if it helped solved your question.
Thanks,
Rohit Suryawanshi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2024 10:53 PM
Hi @Rohit99
Thank you for the reply.
I Saw the solution using creating two custom fields.
We have created two custom fields.( both are Integer type to store minutes)
u_statrt - (to store Only WIP start minutes from the incident created.)
u_state_calc - (to store Only how long WIP minutes from the incident changed to WIP.)
Do you have any approach to this.Thank you!