- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2020 01:36 PM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2020 05:38 PM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2020 02:04 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2020 02:39 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2020 02:57 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2020 03:16 PM
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 ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2020 03:34 PM
Did you check if your script is executing the if (runningCount >= 100)