Please help with email notification trigger.

Erica2
Tera Contributor

Hello,

When a change request is approved, I need to trigger a notification to both the "Created By" and "Opened By" users to let them the change has been approved.

 

I have set up the event registry, business rules, and notification, but the email is not being triggered. Could someone please take a look and suggest how to fix this?  Thank you

 

Registry:

Erica2_0-1737081688430.png

 

Business Rule on Change Request Table:

 

Erica2_1-1737081759521.png

 

Advanced Tab:

 

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


    var gr = new GlideRecord('sysapproval_approver');
    gr.addQuery('sysapproval', current.sys_id);
    gr.addQuery('state', 'approved');
    gr.query();

    while (gr.next()) {
        gs.info('Start');
        gs.eventQueue('Change.approved.notification', current, gr.created_by, gr.openend_by, current);
        gs.info('End');
    }

})(current, previous);

 

Notification:

Erica2_2-1737081889563.png

 

Erica2_3-1737081926056.png

 

Erica2_4-1737081972239.png

 

 

 

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@Erica2 

created by and opened by will be same for CHG

So you will have to tweak it like this

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

    var gr = new GlideRecord('sysapproval_approver');
    gr.addQuery('sysapproval', current.sys_id);
    gr.addQuery('state', 'approved');
    gr.query();
    while (gr.next()) {
        gs.info('Start');
        gs.eventQueue('Change.approved.notification', current, current.opened_by.toString(), '');
        gs.info('End');
    }

})(current, previous);

I hope your BR is configured correctly then it will work fine

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

@Erica2 

so you are saying you want to send email to requested_by as well?

if yes then do this

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

    var gr = new GlideRecord('sysapproval_approver');
    gr.addQuery('sysapproval', current.sys_id);
    gr.addQuery('state', 'approved');
    gr.query();
    while (gr.next()) {
        gs.info('Start');
        gs.eventQueue('Change.approved.notification', current, current.opened_by.toString() + ',' + current.requested_by.toString());
		//gs.eventQueue('Change.approved.notification', current, current.opened_by.toString(), '');
        gs.info('End');
    }

})(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

6 REPLIES 6

Sandeep Rajput
Tera Patron
Tera Patron

@Erica2 Are you sure if there is any opened by field on 

sysapproval_approver

 table?

Hi @Sandeep Rajput 

There is an opened_by file on the approval table:

Erica2_0-1737083690347.png

 

Is Business Rule script looks correct to you?

 

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


    var gr = new GlideRecord('sysapproval_approver');
    gr.addQuery('sysapproval', current.sys_id);
    gr.addQuery('state', 'approved');
    gr.query();

    while (gr.next()) {
        gs.info('Start');
        gs.eventQueue('Change.approved.notification', current, gr.opened_by, gr.created_by, current);
        gs.info('End');
    }

})(current, previous);

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Erica2 

created by and opened by will be same for CHG

So you will have to tweak it like this

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

    var gr = new GlideRecord('sysapproval_approver');
    gr.addQuery('sysapproval', current.sys_id);
    gr.addQuery('state', 'approved');
    gr.query();
    while (gr.next()) {
        gs.info('Start');
        gs.eventQueue('Change.approved.notification', current, current.opened_by.toString(), '');
        gs.info('End');
    }

})(current, previous);

I hope your BR is configured correctly then it will work fine

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 Bawiskar 

Thank you so much for your help. I would like to learn how to add a second parameter for the "requested for" field. Could you please guide me on how to do that?

 

I tried below script and that did not goes well.  Thanks you

 

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

    var gr = new GlideRecord('sysapproval_approver');
    gr.addQuery('sysapproval', current.sys_id);
    gr.addQuery('state', 'approved');
    gr.query();
    while (gr.next()) {
        gs.info('Start');
        gs.eventQueue('Change.approved.notification', current, current.opened_by.toString(), current.requested_by.toString());
		//gs.eventQueue('Change.approved.notification', current, current.opened_by.toString(), '');
        gs.info('End');
    }

})(current, previous);