Set the Calendar duration value on a Closed RITM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
I have an After Business rule that runs when an RITM is in the Status of Closed Complete and CLosed incomplete, to get the duration field to populate but it defaults to 0 secs everytime.
Can anyone provide some assistance or insight on resolving this issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
46m ago
Hello!
Was able to get this to work in a PDI.
Changes:
- I changed the business rule to a before, instead of an after. One of the issues with using after, is any field updates you are attempting to make are being done after the data is inserted/updated into the database. Which means, to update any values on current, you have to use current.update() which can cause recursion issues. By using before, the updates happen prior to the data being saved and does not require a current.update() in the script.
- I also put the restrictions on duration being empty or 0 into the firing conditions so the business rule is skipped completely if all conditions are not met. Not necessary, but is best practice.
Script:
(function executeRule(current, previous /*null when async*/) {
var opened = current.opened_at.toString();
var closed = current.closed_at.toString();
current.calendar_duration = gs.dateDiff(opened, closed);
})(current, previous);
Hope this helps!
Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
36m ago
Hi @oluseyiasol
Try with this:
Update the Business Rule Configuration
- When: Before
- Insert/Update: Checked
- Filter Conditions: [State] [is one of] [Closed Complete, Closed Incomplete]
(function executeRule(current, previous /*null when async*/) {
if (!current.opened_at.nil() && !current.closed_at.nil())
{
var start = new GlideDateTime(current.opened_at.getDisplayValue());
var end = new GlideDateTime(current.closed_at.getDisplayValue());
var duration = GlideDateTime.subtract(start, end);
current.business_duration = duration;
}
})(current, previous);
Also check solution given in the post: https://www.servicenow.com/community/developer-forum/how-to-set-business-duration-field-on-ritm/m-p/...
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti