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.

Email notifications should be sent individually to the list field users?

Udhya
Kilo Contributor

Dear All,

I have a requirement, there is list field on the form and added 4 users in that list field. An email notification has to be sent to the 4 users by addressing their names individually as Dear "X", Dear "Y", Dear "Z".

Not all 4 names in one email, it has to be separated. Dear "X" should get email by addressing his/her name alone.

How can I achieve it?

1 ACCEPTED SOLUTION

shloke04
Kilo Patron

Hi,

Please follow the steps below to achieve your requirement:

1) Create a Event by navigating to Registry module as shown below and create a event: 

For example I have done this for Catalog Task table but you can change it to the Table on which your List field is present.

find_real_file.png

2) Now create a After Update Business Rule on your table where the List field is present and use the script below:

Script to be used:

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

    // Add your code here
    var getListusers = current.watch_list; // Replace "watch_list" with your field name
	var splitusers = getListusers.split(',');
	for(var i=0;i<splitusers.length;i++){
		gs.eventQueue('send.email.task',current,splitusers[i]); // Replace "send.email.task" with the Event which you created.
	}

})(current, previous);

find_real_file.png

Make Sure to add a condition to the BR as below so that this BR does not run all the time:

Field Changes For example Watch List Changes

3) Now create a Notification email script and use the code below:

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

          // Add your code here
	var gr = new GlideRecord('sys_user');
	gr.get(event.parm1);
	template.print(gr.name);

})(current, template, email, email_action, event);

find_real_file.png

3) Now Navigate to your Notifications module and select the trigger as Event based and select the event which you have configured in step 1

Also make sure to select "Parm1" and "Send to Event Creator" checkbox as True as shown below:

In the Body of the email, you need to call the Notification Script created in above step:

${mail_script:Name of your Email Script}

For example ${mail_script:getRecipientName} // getRecipientName is the name of my Email Script

find_real_file.png

find_real_file.pngfind_real_file.png

Result:

I Have updated my ticket to update Watch list field with two users "System Administrator and Abel Tuter" and below are the email which got triggered with individual names:

find_real_file.png

\find_real_file.png

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

3 REPLIES 3

Saurav11
Kilo Patron
Kilo Patron

Hello Udhya,

 

var arr = [];
var arr1 = [];

var arrayUtil = new ArrayUtil();


if(arrayUtil.contains(arr1,current.u_field_name.toString()))
{
continue;
}
arr1.push(current.u_field_name.toString());


for (var i = 0; i < arr1.length; i++)
{
gs.eventQueue('eventname',current,current.sys_id,arr1[i]);
}

 

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

 

Anil Lande
Kilo Patron

Hi,

You cannot set dynamic body consents for different users when they are in to/cc list of same email.

If you want to send user specific email then you need to make it event bases and trigger event 4 times and populate the name of user who is going to receive this email.

If all users are in to/cc list then you cannot set user name dynamically.

You can consider an example when you send mail to your team from your mail box. If we are sending an email to team then we do not mention name. because it is generic message, same is with ServiceNow.

If you want to include each user name then you need to send an individual email.

 

Thanks
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

shloke04
Kilo Patron

Hi,

Please follow the steps below to achieve your requirement:

1) Create a Event by navigating to Registry module as shown below and create a event: 

For example I have done this for Catalog Task table but you can change it to the Table on which your List field is present.

find_real_file.png

2) Now create a After Update Business Rule on your table where the List field is present and use the script below:

Script to be used:

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

    // Add your code here
    var getListusers = current.watch_list; // Replace "watch_list" with your field name
	var splitusers = getListusers.split(',');
	for(var i=0;i<splitusers.length;i++){
		gs.eventQueue('send.email.task',current,splitusers[i]); // Replace "send.email.task" with the Event which you created.
	}

})(current, previous);

find_real_file.png

Make Sure to add a condition to the BR as below so that this BR does not run all the time:

Field Changes For example Watch List Changes

3) Now create a Notification email script and use the code below:

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

          // Add your code here
	var gr = new GlideRecord('sys_user');
	gr.get(event.parm1);
	template.print(gr.name);

})(current, template, email, email_action, event);

find_real_file.png

3) Now Navigate to your Notifications module and select the trigger as Event based and select the event which you have configured in step 1

Also make sure to select "Parm1" and "Send to Event Creator" checkbox as True as shown below:

In the Body of the email, you need to call the Notification Script created in above step:

${mail_script:Name of your Email Script}

For example ${mail_script:getRecipientName} // getRecipientName is the name of my Email Script

find_real_file.png

find_real_file.pngfind_real_file.png

Result:

I Have updated my ticket to update Watch list field with two users "System Administrator and Abel Tuter" and below are the email which got triggered with individual names:

find_real_file.png

\find_real_file.png

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