- 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 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 05:36 AM
Hello @Ankur Bawiskar ,
Yeah it worked. Thank you!