How to send email to particular users in a group

kalyani23
Tera Contributor

Hi,

In my requirement i should send email to particular members in a group , but not for all members.

can someone help on how to achieve this.

Regards,

Kalyani

1 ACCEPTED SOLUTION

shloke04
Kilo Patron

Hi @kalyani 

You can achieve this using a Notification Email Script and use the code as below which will check if users of the group are belonging to same portfolio or not and will trigger accordingly as you require:

find_real_file.png.

Use the Script as below:

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

          // Add your code here
	var getUserRoleGroup = current.FieldName; //Replace "FieldName" with your Field i.e.User Role Group
	var port = current.FieldName; //Replace "FieldName" with your Field i.e.Portfolio
	
	var getGroupMembers = fetchGroupMembers(getUserRoleGroup);
	var getUserDetails = validateUser(getGroupMembers,port);
	
	function fetchGroupMembers(groupID){
		var arr = [];
		var gr = new GlideRecord('sys_user_grmember');
		gr.addQuery('group',groupID);
		gr.query();
		while(gr.next()){
			arr.push(gr.user.toString());
		}
		return arr.toString();
	}
	
	function validateUser(users,port){
		var splitUsers = users.split(',');
		for(var i =0;i<splitUsers.length;i++){
			var checkPortfolio = validatePortfolio(splitUsers[i]);
		}
		
	}
	
	function validatePortfolio(userID,port){
		var gr1 = new GlideRecord('sys_user');
		gr1.addQuery('sys_id',userID);
		gr1.query();
		if(gr1.next()){
			if(gr1.FieldName == port){
				email.setReplyTo(gr1.email.toString());
			}
		}
	}

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

Now call this Notification Script in your Notification body with below syntax as below:

${mail_script:Mail Script Name}

Just replace the field names in script above and let me know if you are stuck.

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

11 REPLIES 11

mail shoulActually based on other field, particular members need to be selected, and only for those the email should be sent.

find_real_file.png

here PM is the group, but members having the same portfolio , for them the email should go, but not for other members in same grp with different portfolio.

Hi Kalyani,

In that case, you can write email script and glide "sys_user_grmember" table and add the query for same portfolio for the group members. Add the members to the Cc list with the below code.

email.addAddress("cc", user address, user name);

Put that in a loop of all your recipients, and you're good to go.

 

Please Mark ✅ Correct/helpful, if applicable, Thanks!! 

Regards

Sulabh Garg

Please Mark ✅ Correct/helpful, if applicable, Thanks!!
Regards
Sulabh Garg

one more clarification, by using this will it add users in email to or email cc ?

email.addAddress("cc", user address, user name);

In email CC, you can also try this code to send in "To", but I am not sure if it works or not.

email.addAddress("to", user address, user name);

Please Mark ✅ Correct/helpful, if applicable, Thanks!!
Regards
Sulabh Garg

Ok thank you for providing help, i will try this, and once it works i will mark your answer as correct one.

Regards,

kalyani