Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

How to add multiple duration fields and populate the result on another duration field

Alon Grod
Tera Expert

Hi,

I have the table u_impact_services which has the field u_total_dur of type Duration.

 

I need to write Business Rule on the incident table that will glide record u_impact_services table and add the field u_total_dur from multiple record and then populate the answer in a Duration field on the incident table

How can I achieve that? I tried this but its not working

 

 

 

 

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

    var totalDuration = new GlideDuration(); // Initialize a GlideDuration object

    var gr = new GlideRecord('u_impact_services');
    gr.addEncodedQuery('u_incident.ref_incident.problem_id=' + current.sys_id + '^u_impact_total_durISNOTEMPTY');
    gr.query();
    while (gr.next()) {
        var impactDuration = new GlideDuration(gr.u_impact_total_dur); 
        totalDuration.add(impactDuration); 
    }

 
    current.u_total_impact_duration = totalDuration.getDurationValue(); 


})(current, previous);

 

 

 

  

2 REPLIES 2

maheshkhatal
Giga Sage

@Alon Grod Can you try this in background script at every juncture and try to log if you are getting possible values.

(function executeRule(current, previous /*null when async*/) {
    // Initialize a GlideDuration object to store the total duration
    var totalDuration = new GlideDuration(0); // Start with 0 milliseconds

    // Query the u_impact_services table
    var gr = new GlideRecord('u_impact_services');
    gr.addEncodedQuery('u_incident=' + current.sys_id + '^u_total_durISNOTEMPTY'); // Adjust the query as needed
    gr.query();

    // Loop through the records and sum the u_total_dur field
    while (gr.next()) {
        var impactDuration = new GlideDuration(gr.getValue('u_total_dur')); // Get the duration value
        totalDuration.add(impactDuration); // Add to the total duration
    }

    // Set the total duration on the incident record
    current.u_total_impact_duration = totalDuration; // Assign the GlideDuration object directly
 
})(current, previous);

Thanks,

Mahesh.

DrewW
Mega Sage

I think a GlideAggregate will sum a GlideDuration field.  If not you can use this

 

https://www.servicenow.com/community/now-platform-forum/how-do-i-convert-a-glideduration-field-to-nu...

 

And then add everything up.