Changes on Incident form -time calculation of fields using business rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-23-2024 11:03 PM
Please check the script and provide a solution.
The business rule is designed to work under the following conditions:
1. When the "In Progress" type changes.
2. When the state changes to "Referred."
This rule calculates the duration for different types of "In Progress" fields (change WIP, TIG WIP, request WIP, and vendor WIP) when their types change. The duration should be populated in the respective fields to reflect how long each type has been in progress.
The time is calculated correctly when the WIP type changes. However, if we work with a single WIP type without changing it, the time is not calculated. The rule should take the current time and the start time to populate the duration, but this is not happening. The issue arises when the WIP type remains the same. please check the BR script!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-23-2024 11:30 PM
Hi @Dipu_9999
Can you use the below modified code :-
(function executeRule(current, previous /*null when async*/) {
var now = new GlideDateTime(); // Current time
// Function to calculate duration
function calculateDuration(previousDateTime, currentDateTime) {
return GlideDateTime.subtract(currentDateTime, previousDateTime);
}
// Initialize durations
var vendorWIPDuration = new GlideDuration(current.u_vendor_wip_dur || '0 00:00:00');
var changeWIPDuration = new GlideDuration(current.u_change_wip_dur || '0 00:00:00');
var requestWIPDuration = new GlideDuration(current.u_request_wip_dur || '0 00:00:00');
var tigWIPDuration = new GlideDuration(current.u_tig_wip_dur || '0 00:00:00');
// Check if the in-progress reason has changed
if (previous.u_in_progress_reason == current.u_in_progress_reason) {
// The WIP type hasn't changed, calculate the duration based on the current time
if (current.u_in_progress_reason == 'vendor_wip') {
var vendorDuration = calculateDuration(new GlideDateTime(current.u_vendor_duration), now);
vendorWIPDuration.add(vendorDuration);
current.u_status_vendor = vendorWIPDuration.getDurationValue();
} else if (current.u_in_progress_reason == 'request_wip') {
var requestDuration = calculateDuration(new GlideDateTime(current.u_request_duration), now);
requestWIPDuration.add(requestDuration);
current.u_status_request = requestWIPDuration.getDurationValue();
} else if (current.u_in_progress_reason == 'change_wip') {
var changeDuration = calculateDuration(new GlideDateTime(current.u_change_duration), now);
changeWIPDuration.add(changeDuration);
current.u_status_change = changeWIPDuration.getDurationValue();
} else if (current.u_in_progress_reason == 'tig_wip') {
var tigDuration = calculateDuration(new GlideDateTime(current.u_tig_duration), now);
tigWIPDuration.add(tigDuration);
current.u_status_tig = tigWIPDuration.getDurationValue();
}
} else {
// The WIP type has changed, calculate duration based on previous and current values
if (previous.getValue('u_in_progress_reason') == 'vendor_wip') {
var vendorDuration = calculateDuration(new GlideDateTime(previous.u_vendor_duration), now);
vendorWIPDuration.add(vendorDuration);
current.u_status_vendor = vendorWIPDuration.getDurationValue();
} else if (previous.getValue('u_in_progress_reason') == 'request_wip') {
var requestDuration = calculateDuration(new GlideDateTime(previous.u_request_duration), now);
requestWIPDuration.add(requestDuration);
current.u_status_request = requestWIPDuration.getDurationValue();
} else if (previous.getValue('u_in_progress_reason') == 'change_wip') {
var changeDuration = calculateDuration(new GlideDateTime(previous.u_change_duration), now);
changeWIPDuration.add(changeDuration);
current.u_status_change = changeWIPDuration.getDurationValue();
} else if (previous.getValue('u_in_progress_reason') == 'tig_wip') {
var tigDuration = calculateDuration(new GlideDateTime(previous.u_tig_duration), now);
tigWIPDuration.add(tigDuration);
current.u_status_tig = tigWIPDuration.getDurationValue();
}
}
// Set the current time when the WIP reason changes
if (current.u_in_progress_reason == 'tig_wip') {
current.u_tig_duration = now;
} else if (current.u_in_progress_reason == 'change_wip') {
current.u_change_duration = now;
} else if (current.u_in_progress_reason == 'request_wip') {
current.u_request_duration = now;
} else if (current.u_in_progress_reason == 'vendor_wip') {
current.u_vendor_duration = now;
}
current.update();
})(current, previous);
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-23-2024 11:37 PM
HI @Ravi Gaurav its still not working if the type is not changed then time never comes. please check the attachment