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.

Need to send the notification to approver when comment added in request table

suuriyas
Tera Contributor

HI Community,

 

I have a requirement, when additional comment is update in REQ (request) then the comments need to be sent to the approver of the RITM (notification need to be sent).

So i have created a event reg and business rule to trigger that but notification is not getting triggered

 

update after BR on sc request table

(function executeRule(current, previous /*null when async*/ ) {
    var lastComments = current.comments.getJournalEntry(1);
    var approverList = [];
    var sysApprovalGR = new GlideRecord("sysapproval_approver");
    sysApprovalGR.addEncodedQuery("sysapproval=" + current.getUniqueValue() + "^state=requested");
    sysApprovalGR.query();
    while (sysApprovalGR.next()) {
        approverList.push(sysApprovalGR.getValue("approver"));
    }
    if (approverList.length && lastComments) {
        gs.eventQueue("sc_req.commented.approver.req", current, approverList, lastComments);
    }

})(current, previous);
BR Condition
suuriyas_0-1744117110350.png

 

event 

suuriyas_1-1744117186511.png

notification event is fired and written on sc request table

suuriyas_2-1744117259595.png

 

can you guys help me what is wrong in here

 

Thanks in Advance

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@suuriyas 

did the event get processed?

your script is wrong, approval is always for RITM and not on REQ so your addQuery won't work

try this

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

    var lastComments = current.comments.getJournalEntry(1);
    var approverList = [];
    var ritm = new GlideRecord('sc_req_item');
    ritm.addQuery('request', current.sys_id);
    ritm.query();
    if (ritm.next()) {
        var sysApprovalGR = new GlideRecord("sysapproval_approver");
        sysApprovalGR.addEncodedQuery("sysapproval=" + ritm.getUniqueValue() + "^state=requested");
        sysApprovalGR.query();
        while (sysApprovalGR.next()) {
            approverList.push(sysApprovalGR.getValue("approver"));
        }
        if (approverList.length > 0 && lastComments) {
            gs.eventQueue("sc_req.commented.approver.req", current, approverList.toString(), lastComments);
        }
    }

})(current, previous);

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

View solution in original post

2 REPLIES 2

SANDEEP28
Mega Sage

@suuriyas I tried your code in PDI and it triggered notification. 

 

Event is fired

 

SANDEEP28_0-1744118316395.png

Emails are present in Email log table - (sys_email)

 

SANDEEP28_1-1744118357488.png

 

Below is my notification record. Compare this with your notification record. See if you added any additional condition.

 

 

SANDEEP28_2-1744118444475.png

 

SANDEEP28_3-1744118500527.png

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

 

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@suuriyas 

did the event get processed?

your script is wrong, approval is always for RITM and not on REQ so your addQuery won't work

try this

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

    var lastComments = current.comments.getJournalEntry(1);
    var approverList = [];
    var ritm = new GlideRecord('sc_req_item');
    ritm.addQuery('request', current.sys_id);
    ritm.query();
    if (ritm.next()) {
        var sysApprovalGR = new GlideRecord("sysapproval_approver");
        sysApprovalGR.addEncodedQuery("sysapproval=" + ritm.getUniqueValue() + "^state=requested");
        sysApprovalGR.query();
        while (sysApprovalGR.next()) {
            approverList.push(sysApprovalGR.getValue("approver"));
        }
        if (approverList.length > 0 && lastComments) {
            gs.eventQueue("sc_req.commented.approver.req", current, approverList.toString(), lastComments);
        }
    }

})(current, previous);

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