Business rule for a count not working properly

Shruti08
Tera Expert

Hi All,

We have created a custom field for the alert form(em_alert table) called 'Overall Secondary Alerts Count' which should display the total secondary alerts' count.

The count should be updated whenever the secondary alert is tagged.

Used below script in the business rule for that:

(function executeRule(current, previous /*null when async*/) {
 
    var alertGR = new GlideRecord("em_alert");
    alertGR.get(current.parent);
    alertGR.u_secondary_alerts_count++;
    alertGR.update();

    alertGR.initialize();
    alertGR.get(previous.parent);
    alertGR.u_secondary_alerts_count++;
    alertGR.update();

})(current, previous);
 
 
But this script did not work for some. The secondary alert count is not updated. for example if there are 21 secondary alerts the field gets updated as 15 only.
 
Can someone please help here.
 
Thanks in advance,
Shruti
1 REPLY 1

Anand Kumar P
Giga Patron
Giga Patron

Hi @Shruti08 ,

Please try below script first it will query the secondary alerts related to the current alert and then update the Overall Secondary Alerts Count field on the parent alert.

 

(function executeRule(current, previous /*null when async*/) {
    var secondaryAlertGR = new GlideRecord("em_alert");
    secondaryAlertGR.addQuery("parent", current.sys_id);
    secondaryAlertGR.query();
    var secondaryAlertsCount = 0;
    while (secondaryAlertGR.next()) {
        secondaryAlertsCount++;
    }
    var parentAlertGR = new GlideRecord("em_alert");
    if (parentAlertGR.get(current.parent)) {
        parentAlertGR.u_secondary_alerts_count = secondaryAlertsCount;
        parentAlertGR.update();
    }

})(current, previous);

 

Please mark it as solution proposed if it is healpful.

 

Thanks,

Anand