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

I think I know what it might be.  If u_actual_cost is a string field, then you can't do things like adding and subtracting, and taking the sum of values in that field--even if you have some other validation that makes it so that only numbers can be put into that field.

You could (1) change the type of the field to be currency, since it looks like what you are talking about is money related, or (2) you could just use a normal glide record to check all records on the table, converting the string value of that string u_actual_cost field to a decimal using like parseFloat(), keeping a running total of the costs as you are looping through all of the records in the GlideRecord

Can you please help me with the code for below 

(2) you could just use a normal glide record to check all records on the table, converting the string value of that string u_actual_cost field to a decimal using like parseFloat(), keeping a running total of the costs as you are looping through all of the records in the GlideRecord

B_35
Tera Contributor

Sure

// 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 += parseFloat( G.u_total_cost ); 
        }
    // 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');
        }

You can adjust the query for the GlideRecord to focus in on some specific records in the u_environment table, or not, if you just want to check everything on the entire table.

Also, once the conditions are met, you can add whatever logic you want to inside of the last "if" statement.  Good luck

RudhraKAM
Tera Guru

Thanks B 

 

But some things wrong  , its not triggering the event (which line of the code is counting the total count of rows in the table ?

Did you check if your script is executing the if (runningCount >= 100)