glide Aggregate on a string field to get the count

RudhraKAM
Tera Guru

I have a requirement where I need to count the  values( string but only numbers allowed in that field )  u_actual_count

We have after  BR on insert or update of that field we need to aggregate that field and check if the count is greater than or equal to  100 then we need to trigger an event ( is it possible to trigger a flow using that ?)

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

    var totalCostSum = new GlideAggregate('u_environment');
    totalCostSum.addAggregate('SUM', 'u_actual_count');
   totalCostSum.groupBy('u_actual_count');
    totalCostSum.query();
    if (totalCostSum.next()) { // in case there is no result
        var allTotalCost = 0;
        allTotalCost = totalCostSum.getAggregate('SUM', 'u_actual_count');
        gs.print('SUM of total_cost: = ' + allTotalCost);
        if (allTotalCost >= "100") 
            gs.eventQueue('incident.commented');
        
    }

})(current, previous);

 

Can some one help me if there is any script error ?

and how to trigger a flow based on the event

I see some articles for the trigger flow from BR

sn_fd.FlowAPI.executeFlow('global.My_flow_internal_name, inputs);

What is the input we need to provide?

1 ACCEPTED SOLUTION

B_35
Tera Contributor

Sorry about that.  I guess that parseFloat() doesn't work for this, but Number() will.  Number() does the same thing, just converts whatever is inside of the parentheses to a number.  Here's that corrected script:

(function executeRule(current, previous /*null when async*/ ) {
    // Go check all u_environment records to see if the u_total_cost is above 100
        // Variable to keep running count
            var runningCount = 0;
        // Query and loop
            var G = new GlideRecord('u_environment');
                G.query();
            while( G.next() ){
                // Add the u_total_cost to runningCount
                    runningCount += Number( G.u_total_cost.toString() ); 
            }
        // Check if the total is over 100
            if( runningCount >= 100 ){
                // Do whatever you want based on meeting this conditions
                    // Print it out, if you want
                        gs.print( "The total cost is over 100" );
                    // Trigger an event, if you want
                        gs.eventQueue('incident.commented');
            }
})(current, previous);

View solution in original post

15 REPLIES 15

B_35
Tera Contributor

You're welcome, I'm glad that helped.

With your other question, it looks like that first article provides a link to the ServiceNow documentation of what "flowInputs" is.  It looks like it is a Map that can be populated in a couple of different ways based on what your needs are.  That first article links to the same documentation, but here is that same link to the documentation for that function you are looking at:

Documentation link

Good luck, I'm glad this worked out.  Thanks