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

darshanr
Kilo Guru

Hi Mukesh,



You can add Email Address in your mail script by below method


email.addAddress("cc", "User's Email","Name");


email.addAddress("bcc", "Group's Email","Group Name");




But for 'To' you can make use of parm1 and parm2 functionality of Email Notifications


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Mukesh,



Please check the below thread which will be helpful


https://community.servicenow.com/message/695278#695278



Thanks


Pradeep Sharma


harikrish_v
Mega Guru

Hi Mukesh,



If you are triggering the email notification with an event, you can pass the recipients id as a parameter in the event call itself ie



gs.eventQueue("myevent.called", current, gs.getUserID(), gs.getUserName());



here parm1 is gs.getUserID() and parm2 is gs.getUserName(). In the email notification, you can select the advanced view, change 'Send when' to 'Event is fired' and you can see that in the 'Who will recieve' you have these options 'parm1 has recipient', 'parm2 has recipient', you can check that to send the email to them.



cc and   bcc can be controlled via script, you can refer this wiki link for more details:-https://wiki.servicenow.com/index.php?title=Scripting_for_Email_Notifications#Adding_CC_and_BCC_Reci...



In a nutshell, you can use the below script add people in cc and bcc:-


<mail_script>


      //email.addAddress(type, address, displayname);


      email.addAddress("cc", "john.copy@example.com","John Roberts");


      email.addAddress("bcc", "john.secret@example.com","John Roberts");


</mail_script>


Hope this helps.



Thanks & Regards,


Hari


HI Harish,



I am trying to send the email notification to bcc and cc but not to have an to address.



The lines you mentioned above <mail_script> email.addAddress......, should be put in the email notification body ?



Thanks,