Add millisecond to Duration field in Business Rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā11-06-2023 12:26 PM
Hello,
I'm trying to create a script to add a millisecond to a duration field on a record (this will fire another existing BR).
I was looking at GlideDuration() class to make a calculation but can't make it work.
This is my script (existing duration field name - work_effort). I'll appreciate any suggestions!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā11-06-2023 01:23 PM
Hi Tanya20,
Seems you will need to use the fields used to calculate work_effort to calculate a new GlideDuration value as I see in the API documentation that 'add(Number milliseconds) only works on GlideDateTime fields.
Also, I believe you will loose that resolution when converting to GlideDuration as the work_effort value is in seconds.
Maybe someone else here will have a suggestion, but you may want to find another way for what you are after. Add a new integer field that represents milliseconds.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā11-06-2023 08:01 PM
Hi @Tanya20 ,
The GlideDuration object doesn't have an add method that allows you to add milliseconds directly. Instead, you can convert the duration to milliseconds, perform the addition, and then convert it back to a duration. Here's how you can modify your script:
(function executeRule(current, previous /*null when async*/) {
// Get the duration field value
var workEffort = new GlideDuration(current.work_effort);
// Convert the duration to milliseconds
var workEffortMilliseconds = workEffort.getNumericValue();
// Add one millisecond
workEffortMilliseconds += 1;
// Create a new GlideDuration from the updated milliseconds value
var updatedWorkEffort = new GlideDuration(workEffortMilliseconds);
// Update the field with the modified duration
current.work_effort = updatedWorkEffort.getDisplayValue();
current.setWorkflow(false);
current.update();
current.setWorkflow(true);
})(current, previous);
Mark as accepted solution & hit helpful if it suffice your requirement.
BR, Sohith
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā11-07-2023 06:11 AM
Hi @Sohithanjan G,
Tried the solution above, didn't work.
