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
11 REPLIES 11

AnimeshP_96
Tera Guru

AnimeshP_96_0-1762257425183.png

AnimeshP_96_1-1762257491124.png


this is the log that i have printed it is coming but alerts are not grouping for the rule which matches
CODE which is have written:


 var alertGr = new GlideRecord(table);
    alertGr.addEncodedQuery("sourceLIKEIRIS^severity=4^state!=Closed");
    alertGr.orderByDesc("sys_created_on");
    alertGr.addNullQuery("parent");
    alertGr.setLimit(3);
    alertGr.query();
    while (alertGr.next()) {
        gs.info("tesetq line 16");
        storeID.push(alertGr.getUniqueValue());
    }

    gs.info("ary12912 = " + storeID);

    for (var i = 0; i < storeID.length; i++) {
        gs.info("lieee23=");
        result = {
            "parent": storeID
        }
        var getStr=JSON.stringify(result);
       
        gs.info("this_eline_me27 is " + getStr);
        return JSON.stringify(result);
    }
    return false;

@Ankur Bawiskar 
@TejasSN_LogicX  
can you guy'z help me in debugging thnx in advance.!


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




Regards,
Animesh

@AnimeshP_96 

try to use GlideAggregate

var alertGrCount = new GlideAggregate(table);
alertGrCount.addEncodedQuery("sourceLIKEIRIS^severity=4^state!=Closed^parentISEMPTY");
alertGrCount.groupBy('parent');
alertGrCount.query();

var totalMatchingAlerts = 0;
while (alertGrCount.next()) {
    totalMatchingAlerts++;
}

gs.info("Total matching alerts: " + totalMatchingAlerts);

// Only group if exactly 3 alerts match
if (totalMatchingAlerts === 3) {
    var alertGr = new GlideRecord(table);
    alertGr.addEncodedQuery("sourceLIKEIRIS^severity=4^state!=Closed^parentISEMPTY");
    alertGr.orderByDesc("sys_created_on");
    alertGr.setLimit(3);
    alertGr.query();

    var storeID = [];
    while (alertGr.next()) {
        storeID.push(alertGr.getUniqueValue());
    }

    gs.info("Alerts to group: " + storeID);

    var result = {
        "parent": storeID
    };
    var getStr = JSON.stringify(result);
    gs.info("Grouping result: " + getStr);

    return getStr;
} else {
    gs.info("Not grouping alerts since count != 3");
    return false;
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

let me try


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




Regards,
Animesh

tried but failed, it is not picking 3 as count applied logs

AnimeshP_96_0-1762321777391.png

 

AnimeshP_96_1-1762321814835.png

 

AnimeshP_96_2-1762321847709.png



CODE : 

var table = "em_alert";
    var ct = 0;
    var alertGrCount = new GlideAggregate(table);
    alertGrCount.addEncodedQuery('sourceLIKEIRIS^severity=4^state!=Closed^parentISEMPTY');
    alertGrCount.groupBy('parent');
    //alertGrCount.addAggregate('COUNT');
    alertGrCount.query();

    var totalMatchingAlerts = 0;
    while (alertGrCount.next()) {
        gs.info("Total matching alerts131: ");
         totalMatchingAlerts++;
       // ct = alertGrCount.getAggregate('COUNT','sys_created_on');
    }

    gs.info("Total matching alerts141: " + totalMatchingAlerts);

    // Only group if exactly 3 alerts match
    if (totalMatchingAlerts === 3) {
        gs.info("ine121 18 got");
        var alertGr = new GlideRecord(table);
        alertGr.addEncodedQuery("sourceLIKEIRIS^severity=4^state!=Closed^parentISEMPTY");
        alertGr.orderByDesc("sys_created_on");
        alertGr.setLimit(3);
        alertGr.query();

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




Regards,
Animesh

did you get time to review this ? @Ankur Bawiskar 


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




Regards,
Animesh