Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Trigger single email notification on condition

farci
Mega Expert

Hi Team,

I am trying to trigger single emails notification based on condition. My record counts in the table are dynamic. However I know the way to count them.

Below is the scenario how I want the system to behave

Suppose if I have 15 records in the table and they all belong to same supplier and reporting/service month. and Once the status of few records changes to 'Ready for Validation' I want to send a single notification and once all 15 record status changes to 'Ready for Validation'. then I want to trigger another notification.

The problem with my below script is, it sends out email notification for all the records instead of single notification. Basically i want two notifications one when the record count is less than 15 and one when record count is equal to 15.

This is a after Business rule.

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

(function(){

var gr = new GlideAggregate('x_isgi_isg_governa_pm_sl_s');

gr.addEncodedQuery('sp_name=Supplier^serv_monONLast month@javascript:gs.beginningOfLastMonth()@javascript:gs.endOfLastMonth()');

gr.addAggregate('COUNT','sp_name');

gr.setGroup(false);

gr.query();

while(gr.next()){

var gr1 = gr.getAggregate('COUNT','sp_name');

gs.addInfoMessage(gr1 + 'GlideAgrregate Count');

var gr2 = new GlideRecord('x_isgi_isg_governa_pm_sl_s');

gr2.addEncodedQuery('sp_name=Supplier^serv_monONLast month@javascript:gs.beginningOfLastMonth()@javascript:gs.endOfLastMonth()^s_wf=6^sys_updated_onRELATIVEGE@hour@ago@1');

gr2.query();

if(gr2.getRowCount() > 0 && gr2.getRowCount() < gr1){

gs.eventQueue("x_isgi_isg_governa.Par_Submit _ISG_ACN",current);

gs.addInfoMessage(gr2.getRowCount() + " getrow count");

}

else if (gr1 == gr2.getRowCount()){

gs.addInfoMessage('full');

gs.eventQueue("x_isgi_isg_governa.Full_Submit _ISG_ACN",current);

}

}

})();

})(current, previous);

Awaiting your quick response.

Regards,

Narmi

1 ACCEPTED SOLUTION

Hello Narmi,



Well, you could still create a parent table and add a new relation between the both. This way you won't need to modify the original table. I doubt, that this will be a simpler solution, but a cleaner one for sure.



But let's focus on what you are trying to archieve. Essentially your issue lies within the way your solution is handled: A business rule triggers on every record. Therefore every single record will trigger a notification (or let's say a notification action). This behaiviour will persist for business rules, since they trigger per record, not per table.



You could solve the issue by setting the business rules up in a way that they trigger a notification action, not the notification itself. This action (e.g. an explicit call of a script) is then handled by a script, which does the calculation/aggregation on the scope of the table. This means, that the calculation of the notificaiton would be done outside the record scope.



The other option would be to mark a record as soon as the initial notification has been send. This way the scope can be kept on the record level.



Again, i think your current way is neither ideal nor future proof. I would suggest to either set up a parent child relatioinship or to calculate the criterias outside of the record scope.



Greetings


Fabian


View solution in original post

10 REPLIES 10

Hi Fabian,



Thank You for the inputs, I could achieve by specifying on when the notification should be trigger within business rule by adding condition to the number of records if they are greater than 50% and less than 55%. As you said notifications is trigger on every record for the business rule. Hence opted this way to achieve a workaround solution.





Regards,


Narmi