Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

group alerts if there are 3 alerts if not dont group them

AnimeshP_96
Tera Guru

requirement is if 3 alerts is created in servicenow , group them which is working but now what is happening is 3 alerts are created but this rule is not matching and showing some different rule in activites under work notes ,some other rule is taking precendence. ..so now if 4 alerts re created it should not group it 

(function findCorrelatedAlerts(currentAlert) {

    var result = {};
    var table = "em_alert";
    //var cnt;
    var storeID = [];
    //var childalerts = [];
    var parent = [];
    var alertGr = new GlideRecord(table);
    alertGr.addNullQuery("parent");
    alertGr.orderBy("sys_created_on");
    alertGr.setLimit(3);
    alertGr.query();
    while (alertGr.next()) {
        gs.info("this is li3ne 13 from me");
        storeID.push(alertGr.getUniqueValue());

    }


    if (storeID.length === 3) {
        gs.info("this is2222229833 ");
        parent = currentAlert.sys_id;
    }
    gs.info("this is2379863 " + parent);
    result = {
        'parent': [String(currentAlert.sys_id)]
    };

    return JSON.stringify(result);

})(currentAlert);

pls can someone help me where i am going wrong..

in the below picture it is creating a new alert instead of making 607 as parent which is expected
AnimeshP_96_0-1762176452983.png

AnimeshP_96_0-1762240364470.png

 

 


Please accept the solution /mark this response as correct or helpful if it assisted you with your question.




Regards,
Animesh
8 REPLIES 8

TejasSN_LogicX
Tera Contributor

Hi @AnimeshP_96 ,

logic

(function findCorrelatedAlerts(currentAlert) {

var groupByField1 = currentAlert.resource;
var groupByField2 = currentAlert.metric_name;

var groupCondition = new GlideRecord('em_alert');

groupCondition.addQuery('resource', groupByField1);
groupCondition.addQuery('metric_name', groupByField2);

groupCondition.addNullQuery('parent');

groupCondition.addQuery('sys_id', '!=', currentAlert.sys_id);

groupCondition.query();

var existingAlerts = [];
while (groupCondition.next()) {
existingAlerts.push(String(groupCondition.getUniqueValue()));
}

if (existingAlerts.length === 2) {

gs.info("Custom 3-Alert Rule: SUCCESS. Grouping " + existingAlerts.length + " alerts under Parent: " + currentAlert.number);

return JSON.stringify({
'parent': existingAlerts
});

} else {

gs.info("Custom 3-Alert Rule: FAILURE. Found " + existingAlerts.length + " matching ungrouped alerts. Requires exactly 2 for grouping.");

return JSON.stringify({
'parent': []
});
}

})(currentAlert);

let me try @TejasSN_LogicX  thnx


Please accept the solution /mark this response as correct or helpful if it assisted you with your question.




Regards,
Animesh

Ankur Bawiskar
Tera Patron
Tera Patron

@AnimeshP_96 

what debugging did you do?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

so first log :   gs.info("this is li3ne 13 from me");  this is printed 6 times in log table but it should be 3 becuase setlimit(3) is provided in the script


Please accept the solution /mark this response as correct or helpful if it assisted you with your question.




Regards,
Animesh