How can we trigger one mail which includes all the outdated articles

Arun61
Tera Contributor
I have a requirement i need to trigger a weekly mail to the CI Owners(we have field called band in user table if users band is d or a or c then should trigger) which include all the outdated knowledge articles in one email. i tried with the below script it is trigger duplicate mails.
example:- if Articles.cmdb_ci.owner is having 5 outdated articles it is triggering two or three duplicate mail and all three mails include 5 articles. duplicate mails should not trigger could anyone please check the below script and fix it.
 
Scheduled job script:-
checkRevDate();
function checkRevDate() {
    var aDt = new GlideDateTime();
    aDt.addDays(7);
    var cDate = aDt.toString().split(' ')[0];
    var kb = new GlideAggregate('kb_knowledge');
    kb.addQuery('workflow_state''outdated');
    kb.addQuery('valid_to''<=', cDate);
    kb.addQuery('cmdb_ci.install_status''10');
    //kb.addQuery('cmdb_ci.managed_by','!=','');
     kno.addQuery('cmdb_ci.managed_by.band''IN''D');
    kb.groupBy('managed_by');
    gs.log('serviceowner' + kb.cmdb_ci.managed_by);
    //kb.getRowCount();
   // kb.setLimit(1);
    kb.query();
    kbArray = [];
    while (kb.next()) {
        kbArray.push(kb.cmdb_ci.managed_by.toString());
    }
    var arrUtil = new global.ArrayUtil();
    var uKbArray = arrUtil.unique(kbArray);
    for (k = 0; k < uKbArray.length; k++) {
        var kno = new GlideRecord('kb_knowledge');
        kno.addQuery('workflow_state''outdated');
        kno.addQuery('valid_to''<=', cDate);
        kno.addQuery('cmdb_ci.install_status''10');
        //kno.addQuery('cmdb_ci.managed_by','!=','');
        kno.addQuery('cmdb_ci.managed_by.u_band''IN''D,');
        kno.addQuery('cmdb_ci.managed_by', kbArray[k]);
        gs.log('kno serviceowner' + kb.cmdb_ci.managed_by);
        kno.orderBy('number');
        //kno.setLimit(1);
        kno.query();
        var kbID = [];
         while(kno.next()) {
            //var userband = kno.cmdb_ci.managed_by.u_band.u_approval_level;
            //  if (userband == 'D'){
            kbID.push(kno.sys_id.toString());
            gs.log('rowcount' + kno.getRowCount());
            gs.log('rowlength' + kbID.length);
            if (kbID.length == kno.getRowCount()) {
                // gs.info(kno.cmdb_ci.managed_by.u_band.u_approval_level + "  EXCLUDEBAND@@" + kno.getRowCount();
                gs.eventQueue('outdated.kb.articles.service.owner', kno, kbArray[k], kbID);
           // }
            //}
        }
    }
}
 
 
email script:-
(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
        /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
        /* Optional GlideRecord */ event) {
 
    template.print("<table>");
    template.print("<tr><th align='left'>Number</th><th align='left'>Short Description</th><th align='left'>Valid to date</th></tr>");
    var kbRec = new GlideRecord('kb_knowledge');                                
    var sKbRec = event.parm2.split(',');                                        
    var tbl = current.getTableName();                                           
    for (x = 0; x < sKbRec.length; x++) {                                       
        kbRec.get(sKbRec[x]);                                                   
        var link = createLinkForObject(tbl,kbRec.sys_id,kbRec.number);          
        template.print("<tr><td>" + link + "</td><td style='padding: 4px;'>" + kbRec.short_description + "</td><td>" + kbRec.valid_to + "</td></tr>");
    }
    template.print("</table>");
})(current, template, email, email_action, event);

function createLinkForObject(strTableName, strSysID, strLabel) {                
    return '<a href="' + gs.getProperty('glide.servlet.uri') + 'nav_to.do?uri=' + gs.generateURL(strTableName, strSysID) + '">' + strLabel + '</a>';
}
 
3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@Arun61 

check this link where I shared solution for something similar last year. 1 email per user with business app details for that user only

Enhance it for your requirement

How to trigger multiple notifications for multiple users ? 

 

AnkurBawiskar_0-1695207075635.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Arun61 

Thank you for marking my response as helpful.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi Ankur,

Thank you for your response.

Notification is still triggering twice to the users.

Requirement:- We have a Configuration item reference field in KB articles. if 5 outdated articles having same CI service owner then we need to trigger a single mail that includes all the 5 outdated articles. 

Please help on this.

Arun61_2-1695287703835.png

 

           Arun61_1-1695287264686.png