Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Duration Calculation not working

ceraulo
Mega Guru

Hello!

I have 2 date/time fields - Opened Date and Populated Date.

I have a before BR that populates the Populated Date.

I also have a Duration field.

I created an Update before BR which runs when the Populated Date is not empty. My script is:

(function executeRule(current, previous /*null when async*/ ) {

    getTimeDiff();

    function getTimeDiff() {
        var openDate = current.opened_date.getGlideObject();
        var popDate = current.u_populated_date.getGlideObject();

        current.u_duration = gs.dateDiff(openDate.getDisplayValueInternal(), popDate.getDisplayValueInternal(), false);
    }

})(current, previous);

 

This script is not working.

Please help troubleshoot the script.

Thank you.

1 ACCEPTED SOLUTION

Snehangshu Sark
Mega Guru

Hi @ceraulo ,

 

use the below script, it's tested.

function executeRule(current, previous /*null when async*/ ) {

getTimeDiff();

function getTimeDiff() {

current.u_duration = gs.dateDiff(current.opened_date.getDisplayValue(), current.u_populated_date.getDisplayValue(), false);
}

})(current, previous);

 

Regards,

Snehangshu Sarkar

Please mark my answer as correct if it resolves your query.

View solution in original post

3 REPLIES 3

Omender Singh
Tera Guru

Please try this script - 

 

 

function executeRule(current, previous /*null when async*/ ) {

getTimeDiff();

function getTimeDiff() {
var openDate = current.opened_date.getDisplayValue();
var popDate = current.u_populated_date.getDisplayValue();

current.u_duration = gs.dateDiff(openDate, popDate, false);
}

})(current, previous);

Snehangshu Sark
Mega Guru

Hi @ceraulo ,

 

use the below script, it's tested.

function executeRule(current, previous /*null when async*/ ) {

getTimeDiff();

function getTimeDiff() {

current.u_duration = gs.dateDiff(current.opened_date.getDisplayValue(), current.u_populated_date.getDisplayValue(), false);
}

})(current, previous);

 

Regards,

Snehangshu Sarkar

Please mark my answer as correct if it resolves your query.

It worked when I used only this line of code in the BR:

current.u_duration = gs.dateDiff(current.opened_date.getDisplayValue(), current.u_populated_date.getDisplayValue());