How to set "Who will receive" through mail script?

m_servicenow
Kilo Contributor

Hi Everyone,

I want to add "Who will receive' from <mail_script></mail_script>. is there any way by which we can set user/group filed in "Who will receive".

I m writing this code as below in <mail_script> in email template. But this is not working:

<mail_script>

var gr = new GlideRecord("change_request");

gr.addQuery("number", current.number);

gr.query();

if(gr.next())

{

  var emailto=gr.requested_by.user_name+"@example.com";

    email.setTo(emailto);

}

  </mail_script>

18 REPLIES 18

Can you please click on the related link "Advanced view" and check again. In case you still don't see this then personalize the form layout and make sure the field is present on the form.


johnram
ServiceNow Employee
ServiceNow Employee

The ServiceNow Wiki content is no longer supported. Updated information about this topic is located here: Scripting for Email Notifications
     
   


Visit http://docs.servicenow.com for the latest product documentation


Ravi Prasad1
Tera Guru

Never tried without To field. You just give a try.



Let me know the result as I am also interested to know the result !!!


Sure. THanks Ravi. There is another question which I have related to something else. Can you please see my question to the article



THanks,


Scott Murphy2
Giga Contributor

I have a similar requirement and used a mail script to modify the email_action.recipient_users directly.

(function runMailScript(current, template, email, email_action, event) {
	var user = new GlideRecord('sys_user');
	user.addEncodedQuery('user_name=' + current.sys_created_by);
	user.query();
	while (user.next()){
	email.addAddress("to", user.email,user.name);
	template.print(user.name);
	email_action.recipient_users += user.sys_id+",";	
	}
})(current, template, email, email_action, event);