- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-09-2025 03:39 AM
Hello, I am trying populate the value of the duration when the state is in review state using a business rule. I tried debugging this its entering the if part inside else if but the duration value is not getting populated.Please help me resolve this issue.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-09-2025 05:32 AM
your business rule should be Before update
try these changes
1) both start and end should be of date/time type
2) calendar_duration field should be of duration type
3) also use start and end as GlideDateTime object and not string
(function executeRule(current, previous /*null when async*/) {
if (current.state == 10) { // In draft state
current.work_start = new GlideDateTime();
}
else if (current.state == 100) { // In review state
current.work_end = new GlideDateTime();
if (current.work_start && current.work_end) {
// Convert to GlideDateTime objects
var start = new GlideDateTime(current.work_start);
var end = new GlideDateTime(current.work_end);
var duration = GlideDateTime.subtract(start, end);
current.calendar_duration = duration;
}
}
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-09-2025 03:52 AM - edited ‎06-09-2025 03:53 AM
Hi @maithrimh ,
(function executeRule(current, previous /*null when async*/) {
if (current.state == 10 && previous.state != 10) {
// Entered Draft State
current.work_start = new GlideDateTime();
}
else if (current.state == 100 && previous.state != 100) {
// Entered In Review State
current.work_end = new GlideDateTime();
if (current.work_start && current.work_end) {
var gdStart = current.work_start.getGlideObject();
var gdEnd = current.work_end.getGlideObject();
var duration = gdEnd.subtract(gdStart); // Returns GlideDuration
current.calendar_duration = duration.getDurationValue();
}
}
})(current, previous);
ensure your Business Rule is set to run:
When: Before update
Condition: current.state == 100 (or check in script)
Fields to watch: state
Please check with this script,
If the solution is helpful. Please mark it as Helpful
Thanks
Arun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-09-2025 03:59 AM
var duration = gdEnd.subtract(gdStart);
Subtract function is not working. It is going inside if but is not displaying the value of duration after subtract.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-09-2025 04:02 AM - edited ‎06-09-2025 04:14 AM
Hi @maithrimh ,
try to debug values in each variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-09-2025 05:07 AM
Hello @Arun_Manoj ,
Is there any alternative for subtract function inside business rule? That is where it is getting stuck.