- 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 04:35 PM
I did checked by adding the info message its not executing , I validated the fields every thing seems to be correct
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2020 05:08 PM
The code that I sent is code that should be included within the body of the business rule, and not necessarily copied and pasted to replace the script of the business rule entirely. So, you would copy and paste this exact text, and then you could do more info messages, etc., to see where the code isn't working. I'm just making sure you've got the code structured correctly, since there are lots of little, easy mistakes that could be made when writing any kind of code. Here is what the entire script should look like after the script before is included in the body of the business rule's 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();
// Maybe a check to make sure that there are any records being returned from this query?
if( ! G.hasNext() ){
gs.addInfoMessage("There aren't any records being returned by the query" );
return;
}
while( G.next() ){
// Add the u_total_cost to runningCount
runningCount += parseFloat( G.u_total_cost.toString() );
}
// Maybe a check to see if the running count has been added to at all
gs.addInfoMessage( "Running count: " + runningCount );
// Check if the total is over 100
if( runningCount >= 100 ){
// Do whatever you want based on meeting this condition
// 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);
Also, in the line where I increment runningCount, as I am looping through the GlideRecord, even though the field u_total_count is a string-type field, GlideRecords can be tricky in what they return when you are requesting the value of a field, so I will just double check, and make sure that the value of u_total_count is being returned as a string, by adding the ".toString()" method to the end of the value.
Now there are a few debug messages in there to give you some clues. You can work your way through the code by just displaying what is happening at different parts of the code, by doing the gs.addInfoMessage(), etc., just to follow where the code is breaking. That is what I have added to the code I am including in this response, as a starting point, that might give some clues as to what is happening. I also, when coding, do a thing where I'll do like gs.addInfoMessage('hit'), just to see if that line of code gets hit, if I am concerned that some lines of code are not being executed. If the script spits out any clues, we can keep kicking it around if you want. I'd start by being sure that the business rule is being executed by just adding a "gs.addInfoMessage('hit')" to the top of the code, just to make sure the business rule is being run.
Good luck
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2020 05:20 PM
- 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 06:06 PM
Thank you so much that works and one more last question
How to trigger a Flow based on the this
I see some articles for the trigger flow from BR
sn_fd.FlowAPI.executeFlow('global.My_flow_internal_name, inputs);
https://developer.servicenow.com/blog.do?p=/post/new-in-london-new-ways-to-trigger-flows/
Not sure what the input is ? and what do we do with the trigger conditions in the flow ?