How to send email if notification unchecked to "send to event creator"

Rajababu
Giga Guru

Hi ,

I need help from anyone can give me some idea on this .

I want send email notification only if user belongs to "u_userslist" field value on incident .

Now what the issue,

1- Notification  when "send event creator" check box "True" :

Then if I am creating "incident" whether i will be in "u_userlist" field exists or not ,It will trigger the email for me also.

2- Notification when "send event creator" check box "false"

If i am creating the "incident" record and my email id added to "u_userlist" also ,I will not receive email till the time i will not send event creator check box true .

-----------------------------------------------------------------------------------------------------------------------------------------------

I want make  sure , if "send event creator" check box "false" into the notification  and email id added to "u_userlist" then I should receive email.

1 ACCEPTED SOLUTION

shloke04
Kilo Patron

Hi,

Please find the steps below to achieve your requirement:

Send to event creator if you switch it off it will not send it to the current logged in user even if the user is present in the list or not which is expected behavior OOB. Refer to HI article below:

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0813747

Also to note when this is switched off, Servicenow removed the event creator name from the recipient list even if you hard code the event creator name in the Notification Recipient list.

1) Not sure how your current Notification is configured. But I would suggest you can make use of Event here to achieve your requirement.

For example, I am using Watch List field on Catalog task to trigger Notification to the user selected in Watch List field with "Send to Event Creator" being switched OFF

2) First step is to create an Event. Navigate to Registry module and then create a new Event on Catalog task table(In your case it will be Incident Table )

find_real_file.png

2) Now once this is done, create a new After Update or Insert Business Rule as per your requirement on Catalog task Table and have proper Condition mentioned in the BR to ensure this BR runs only when required and not all the time, like for example only when say State Changes or Your field which contains the user list changes as shown below:

Script to be used:

Make sure to create these two event first in Registry module as per steps mentioned below:

switch.on.create.event.creator

switch.on.create.event.creator.off

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

    // Add your code here
    var getListusers = current.watch_list; // Replace your field here
    var loggedUser = gs.getUserID();
    if (getListusers.indexOf(loggedUser) > -1) { // Checks if Logged in User is added to the List field on the form or not
        sendTOCreator(true, getListusers);
    } else {
        sendTOCreator(false, getListusers);
    }

    function sendTOCreator(valueToCheck, getListusers) {
        var getListusersSplit = getListusers.split(',');
        if (valueToCheck == true) {

            gs.eventQueue('switch.on.create.event.creator', null, null, null); // This is called in Script Actions
            gs.sleep(18000);
            for (var i = 0; i < getListusersSplit.length; i++) {
                gs.eventQueue('send.email.task', current, getListusersSplit[i]);
            }
            gs.sleep(18000);
            gs.eventQueue('switch.on.create.event.creator.off', null, null, null);// This is called in Script Actions
        } else if (valueToCheck == false) {
            for (var j = 0; j < getListusersSplit.length; j++) {
                gs.eventQueue('send.email.task', current, getListusersSplit[j]);
            }

        }
    }


})(current, previous);

 

find_real_file.png

Now once this is done, you need to Navigate to Script Action module and create two new Script Actions as below and use the script as shared below:

1) To Enable Send to Event Creator checkbox as True so that email gets sent out.

var gr = new GlideRecord('sysevent_email_action');
gr.addQuery('sys_id','c5d9b7b707f00110f3e0f2ae7c1ed051'); // Use property instead of Hard coding Sys ID and Replace the Sys ID of the Notification which you want to send here.
gr.query();
if(gr.next()){
	gr.send_self = true;
	gr.update();
}

Make sure to select the name as shown below:

find_real_file.png

Now create another script action and use the script below as shown below:

var gr = new GlideRecord('sysevent_email_action');
gr.addQuery('sys_id','c5d9b7b707f00110f3e0f2ae7c1ed051'); // Use property instead of Hard coding Sys ID and Replace the Sys ID of the Notification which you want to send here.
gr.query();
if(gr.next()){
	gr.send_self = false;
	gr.update();
}

find_real_file.png

Note: The above solution implement when it's a must use case for you. Please make sure to validate in your Non prod instance first and once you are confident then only proceed with this in Production. I have added a timer of 18 seconds just mentioning it specifically to call out, but I can't think of any other way to achieve this.

However this works for me in my PDI.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

View solution in original post

2 REPLIES 2

bammar
Kilo Sage
Kilo Sage

You can try keeping the box true then writing a mailscript to check if the creator is in the list and if so remove from the to field

OR you can make 2 notification- one with condition if opened by is on the list what to do - send to event creator true and if opened by is not on dont send to event creator

shloke04
Kilo Patron

Hi,

Please find the steps below to achieve your requirement:

Send to event creator if you switch it off it will not send it to the current logged in user even if the user is present in the list or not which is expected behavior OOB. Refer to HI article below:

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0813747

Also to note when this is switched off, Servicenow removed the event creator name from the recipient list even if you hard code the event creator name in the Notification Recipient list.

1) Not sure how your current Notification is configured. But I would suggest you can make use of Event here to achieve your requirement.

For example, I am using Watch List field on Catalog task to trigger Notification to the user selected in Watch List field with "Send to Event Creator" being switched OFF

2) First step is to create an Event. Navigate to Registry module and then create a new Event on Catalog task table(In your case it will be Incident Table )

find_real_file.png

2) Now once this is done, create a new After Update or Insert Business Rule as per your requirement on Catalog task Table and have proper Condition mentioned in the BR to ensure this BR runs only when required and not all the time, like for example only when say State Changes or Your field which contains the user list changes as shown below:

Script to be used:

Make sure to create these two event first in Registry module as per steps mentioned below:

switch.on.create.event.creator

switch.on.create.event.creator.off

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

    // Add your code here
    var getListusers = current.watch_list; // Replace your field here
    var loggedUser = gs.getUserID();
    if (getListusers.indexOf(loggedUser) > -1) { // Checks if Logged in User is added to the List field on the form or not
        sendTOCreator(true, getListusers);
    } else {
        sendTOCreator(false, getListusers);
    }

    function sendTOCreator(valueToCheck, getListusers) {
        var getListusersSplit = getListusers.split(',');
        if (valueToCheck == true) {

            gs.eventQueue('switch.on.create.event.creator', null, null, null); // This is called in Script Actions
            gs.sleep(18000);
            for (var i = 0; i < getListusersSplit.length; i++) {
                gs.eventQueue('send.email.task', current, getListusersSplit[i]);
            }
            gs.sleep(18000);
            gs.eventQueue('switch.on.create.event.creator.off', null, null, null);// This is called in Script Actions
        } else if (valueToCheck == false) {
            for (var j = 0; j < getListusersSplit.length; j++) {
                gs.eventQueue('send.email.task', current, getListusersSplit[j]);
            }

        }
    }


})(current, previous);

 

find_real_file.png

Now once this is done, you need to Navigate to Script Action module and create two new Script Actions as below and use the script as shared below:

1) To Enable Send to Event Creator checkbox as True so that email gets sent out.

var gr = new GlideRecord('sysevent_email_action');
gr.addQuery('sys_id','c5d9b7b707f00110f3e0f2ae7c1ed051'); // Use property instead of Hard coding Sys ID and Replace the Sys ID of the Notification which you want to send here.
gr.query();
if(gr.next()){
	gr.send_self = true;
	gr.update();
}

Make sure to select the name as shown below:

find_real_file.png

Now create another script action and use the script below as shown below:

var gr = new GlideRecord('sysevent_email_action');
gr.addQuery('sys_id','c5d9b7b707f00110f3e0f2ae7c1ed051'); // Use property instead of Hard coding Sys ID and Replace the Sys ID of the Notification which you want to send here.
gr.query();
if(gr.next()){
	gr.send_self = false;
	gr.update();
}

find_real_file.png

Note: The above solution implement when it's a must use case for you. Please make sure to validate in your Non prod instance first and once you are confident then only proceed with this in Production. I have added a timer of 18 seconds just mentioning it specifically to call out, but I can't think of any other way to achieve this.

However this works for me in my PDI.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke