Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Add millisecond to Duration field in Business Rule

Tanya20
Tera Guru

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!

 

(function executeRule(current, previous /*null when async*/) {
 
//get the duration field value
var workEffort = new GlideDuration(current.work_effort);
 
//add one millisecond to duration field
workEffort.add(1);
 
//update the field with modified duration
current.work_effort = workEffort.getDisplayValue();
 
current.setWorkflow(false);
current.update();
current.setWorkflow(true);
 
})(current, previous);
3 REPLIES 3

Bert_c1
Kilo Patron

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.

 

API search for add

 

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.

Sohithanjan G
Kilo Sage

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

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

Hi @Sohithanjan G,

Tried the solution above, didn't work.