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

Hi stmurphy,

Thanks for your code sharing.  I also have a similar requirement, but I need to add an email address, instead of a user. Reason is that I need to force (and test if exists) a secondary email address.

When I try to add this email to "recipient_users, it doesn't work. I tried your code (to add an email address):

email_action.recipient_users += ",test@test.com,"; 

I even tried (to replace existing users/emails addresses): 
email_action.setValue('recipient_users',mailaddress); 

Paradoxaly, logging displays correctly the email address that I set by script: 
gs.log('resulting recipient_users : '+email_action.recipient_users);

But when I look to sys_email, recipiends are not good (as if my code wasn't executed). 

Is there any complementary action to apply, configuration to verify  ?

Tried on OOB dev instance (Jakarta) and another one, unsuccesfully.

 
Thanks,

Arnaud

Lavanya D
Kilo Contributor

Hi ,

I have the same requirement of sending a secondary OOB email to a email address "abc@gmail.com". Please help me out with this email script.

 

Thanks,

Lavanya

 

Lavanya,

 

I think you can only use a user account with this. If you make a user account with this email address then supply the sys_id of that user it should work.

 

Lavanya,

 

You can cc email addresses that are not users in the system by using the email object.

https://docs.servicenow.com/bundle/newyork-servicenow-platform/page/script/server-scripting/reference/r_ExScptEmlNtfn.html