Business Rule - Combining Two GlideDuration Fields

Anderson_R
Tera Contributor

Hi everyone, 

I’m trying to add two GlideDuration field values and overwrite the second GlideDuration field with the calculated total. I've been using methods from the GlideDuration class and had success with setValue() to transfer one duration, but I can’t get add() to work as expected in the script below. 

Documentation: GlideDuration | ServiceNow Developers

 

(function executeRule(current, previous /*null when async*/) {
    // Retrieve the task and time worked values
    var taskSysId = current.task.toString(); // Reference to wm_task table
    var newWorkTime = current.time_worked; // GlideDuration field

    // Check if task field has a value
    if (taskSysId) {
        // Fetch the corresponding work order task record in wm_task table
        var wmTaskRecord = new GlideRecord('wm_task');
        if (wmTaskRecord.get(taskSysId)) {
            // Retrieve the current value of u_wot_total_time
            var currentWorkTime = wmTaskRecord.u_wot_total_time; // GlideDuration field

            // Log the current and new work times using getDurationValue()
            gs.info("Adding currentWorkTime (" + currentWorkTime.getDurationValue() + ") and newWorkTime (" + newWorkTime.getDurationValue() + ")");

            // Add the current work time and new work time
            var totalWorkTime = currentWorkTime.add(newWorkTime);

            // Log the total work time
            gs.info("Total Work Time: " + totalWorkTime.getDurationValue());

            // Update the wm_task record with the new u_wot_total_time value
            wmTaskRecord.setValue('u_wot_total_time', totalWorkTime.getDurationValue());
            wmTaskRecord.update();
        }
    }
})(current, previous);

 


The log statements confirm that I'm retrieving the values correctly, but they're not being added as expected. For example:

Adding currentWorkTime (01:00:00) and newWorkTime (02:00:00)
Total Work Time: undefined

 

Any guidance or examples on how to add two GlideDuration fields would be greatly appreciated!

Thank you!



1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

If this is in the Global scope, you can use get/setDateNumericValue.  Here's what it would look like in your script:

(function executeRule(current, previous /*null when async*/) {
    // Retrieve the task and time worked values
    var taskSysId = current.task.toString(); // Reference to wm_task table
    var newWorkTime = current.time_worked.getGlideObject().getNumericValue(); // GlideDuration field

    // Check if task field has a value
    if (taskSysId) {
        // Fetch the corresponding work order task record in wm_task table
        var wmTaskRecord = new GlideRecord('wm_task');
        if (wmTaskRecord.get(taskSysId)) {
            // Retrieve the current value of u_wot_total_time
            var currentWorkTime = wmTaskRecord.u_wot_total_time.getGlideObject().getNumericValue(); // GlideDuration field

            // Log the current and new work times using getDurationValue()
            gs.info("Adding currentWorkTime (" + currentWorkTime.getDurationValue() + ") and newWorkTime (" + newWorkTime.getDurationValue() + ")");

            // Add the current work time and new work time
            //var totalWorkTime = currentWorkTime.add(newWorkTime);

            // Log the total work time
            //gs.info("Total Work Time: " + totalWorkTime.getDurationValue());

            // Update the wm_task record with the new u_wot_total_time value
            wmTaskRecord.u_wot_total_time.setDateNumericValue(currentWorkTime + newWorkTime);
            wmTaskRecord.update();
        }
    }
})(current, previous);

Since you are adding, you might need to wrap the script variable assignments or addition in parseInt.  If this is in a scoped app or otherwise doesn't work, here's an alternate method that I have also used, but didn't test in this specific case:

(function executeRule(current, previous /*null when async*/) {
    // Retrieve the task and time worked values
    var taskSysId = current.task.toString(); // Reference to wm_task table
    var newWorkTime = current.time_worked.dateNumericValue(); // GlideDuration field

    // Check if task field has a value
    if (taskSysId) {
        // Fetch the corresponding work order task record in wm_task table
        var wmTaskRecord = new GlideRecord('wm_task');
        if (wmTaskRecord.get(taskSysId)) {
            // Retrieve the current value of u_wot_total_time
            var currentWorkTime = wmTaskRecord.u_wot_total_time.dateNumericValue(); // GlideDuration field

            // Log the current and new work times using getDurationValue()
            gs.info("Adding currentWorkTime (" + currentWorkTime.getDurationValue() + ") and newWorkTime (" + newWorkTime.getDurationValue() + ")");

            // Add the current work time and new work time
            var totalWorkTime = currentWorkTime + newWorkTime;

            // Log the total work time
            gs.info("Total Work Time: " + totalWorkTime.getDurationValue());

            // Update the wm_task record with the new u_wot_total_time value
            wmTaskRecord.u_wot_total_time.setDateNumericValue(totalWorkTime);
            wmTaskRecord.update();
        }
    }
})(current, previous);

 

View solution in original post

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

If this is in the Global scope, you can use get/setDateNumericValue.  Here's what it would look like in your script:

(function executeRule(current, previous /*null when async*/) {
    // Retrieve the task and time worked values
    var taskSysId = current.task.toString(); // Reference to wm_task table
    var newWorkTime = current.time_worked.getGlideObject().getNumericValue(); // GlideDuration field

    // Check if task field has a value
    if (taskSysId) {
        // Fetch the corresponding work order task record in wm_task table
        var wmTaskRecord = new GlideRecord('wm_task');
        if (wmTaskRecord.get(taskSysId)) {
            // Retrieve the current value of u_wot_total_time
            var currentWorkTime = wmTaskRecord.u_wot_total_time.getGlideObject().getNumericValue(); // GlideDuration field

            // Log the current and new work times using getDurationValue()
            gs.info("Adding currentWorkTime (" + currentWorkTime.getDurationValue() + ") and newWorkTime (" + newWorkTime.getDurationValue() + ")");

            // Add the current work time and new work time
            //var totalWorkTime = currentWorkTime.add(newWorkTime);

            // Log the total work time
            //gs.info("Total Work Time: " + totalWorkTime.getDurationValue());

            // Update the wm_task record with the new u_wot_total_time value
            wmTaskRecord.u_wot_total_time.setDateNumericValue(currentWorkTime + newWorkTime);
            wmTaskRecord.update();
        }
    }
})(current, previous);

Since you are adding, you might need to wrap the script variable assignments or addition in parseInt.  If this is in a scoped app or otherwise doesn't work, here's an alternate method that I have also used, but didn't test in this specific case:

(function executeRule(current, previous /*null when async*/) {
    // Retrieve the task and time worked values
    var taskSysId = current.task.toString(); // Reference to wm_task table
    var newWorkTime = current.time_worked.dateNumericValue(); // GlideDuration field

    // Check if task field has a value
    if (taskSysId) {
        // Fetch the corresponding work order task record in wm_task table
        var wmTaskRecord = new GlideRecord('wm_task');
        if (wmTaskRecord.get(taskSysId)) {
            // Retrieve the current value of u_wot_total_time
            var currentWorkTime = wmTaskRecord.u_wot_total_time.dateNumericValue(); // GlideDuration field

            // Log the current and new work times using getDurationValue()
            gs.info("Adding currentWorkTime (" + currentWorkTime.getDurationValue() + ") and newWorkTime (" + newWorkTime.getDurationValue() + ")");

            // Add the current work time and new work time
            var totalWorkTime = currentWorkTime + newWorkTime;

            // Log the total work time
            gs.info("Total Work Time: " + totalWorkTime.getDurationValue());

            // Update the wm_task record with the new u_wot_total_time value
            wmTaskRecord.u_wot_total_time.setDateNumericValue(totalWorkTime);
            wmTaskRecord.update();
        }
    }
})(current, previous);

 

Wow, the first script you provided works flawlessly. I will definitely be adding getNumericValue() and setDateNumericValue () to my toolbox. 

Thank you so much!