Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Why email notification is not triggering when record is deleted?

Ankita Kolhe
Tera Contributor

Hello Community,

 

I want to trigger email notification when record is getting deleted. However, it's not working.

 

Before Delete Business rule:

AnkitaKolhe_0-1789547771691.png

 

Event registry:

AnkitaKolhe_1-1789547867874.png

 

Notification:

AnkitaKolhe_2-1789547961772.pngAnkitaKolhe_3-1789547982576.png

 

AnkitaKolhe_4-1789548024249.png

 

 

Mail script:

AnkitaKolhe_5-1789548088834.png

 

I can see event logs but with status error.

Could anyone please help why this is not working?

 

Thanks,

Ankita

 

 

 

6 REPLIES 6

Ankur Bawiskar
Tera Patron

@Ankita Kolhe 

images are not clear.

check this blog

When table records deletion, sent an Email to respective Users through Event Queue , Email notificat... 

💡 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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar ,

The notification screenshots are not visible on the link you shared.

 

Thanks

@Ankita Kolhe 

for your case, what debugging did you do?

is your event picking correct user and getting triggered and processed?

share proper images please

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

@Ankur Bawiskar 

 

Before delete BR:

 

  var todayDate = new GlideDateTime();
        var mobilityProfilesGR = new GlideRecord('x_roho_mms_mobility_profile');
        mobilityProfilesGR.addQuery('supplier', current.supplier);
        mobilityProfilesGR.query();

        while (mobilityProfilesGR.next()) {

            var userDetails = {
                name: mobilityProfilesGR.user.getDisplayValue(),
                supplier: mobilityProfilesGR.getDisplayValue('supplier')
            };

            gs.info('testak: ' + mobilityProfilesGR.user.email.toString() + ' ' + userDetails.name + ' ' + userDetails.supplier);
            gs.eventQueue('x_roho_mms.supplier_whitelist_deleted', null, mobilityProfilesGR.user.email.toString(), JSON.stringify(userDetails));


            mobilityProfilesGR.non_chargable_supplier = true;
            mobilityProfilesGR.non_chargeable_since = todayDate;
            mobilityProfilesGR.update();
     
Created a notification with the event registry and calling the mail script from mail body.
 
Mail script:
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {
    try {
        gs.info('testakParm1 = ' + event.parm1);
        gs.info('testakParm2 = ' + event.parm2);

        var userDetails = JSON.parse(event.parm2);
        gs.info('testakhh: ' + userDetails.name + ' ' + userDetails.supplier);

        // Set recipient
        // email.addAddress('to', userDetails.email_id);

        // Set subject
        email.setSubject('Supplier ' + userDetails.supplier + ' Removed from Whitelist');
        template.print('Hello ' + userDetails.name + ',<br/><br/>');
        template.print('The supplier <b>' + userDetails.supplier + '</b> has been removed from the whitelist.<br/><br/>');
        template.print('Regards,<br/>MMS Team');
    } catch (e) {
        gs.error('Mail Script Error: ' + e);
    }

})(current, template, email, email_action, event);
 
The logs in BR is displaying, event logs with status 'error' and email script logs are not working.