How to set the 'to' and 'cc' fields in a notification

juan casas
Mega Expert

Hello,

 

I have two string fields with a bunch of emails.

My goal is to create a notification that has a dynamic 'to' and  'cc'.

The mechanism is as follows:

1. UI action

2. event 

3. email

 

I am able to fire the notification with correct subject and body, however I cant seem to get the 'to' and 'cc' fields to work..

 

    EMAIL SCRIPT


  var parser = JSON.parse(event.parm1); // parse the json
    
    //gs.error('TO_CC_Body_email_script fired :  ' + parser.to2 );    // notification passes the correct parameters
    
    // email.setReplyTo(parser.to2);   // not working
    // email.addAddress("to", 'email2@gmail.com');   // not working
    //email.setReplyTo("abc@yourcompany.com");   // not working
    email.setSubject("HARDCODED SUBJECT from email script!! ");
    template.print("PRINT STATEMENT " );

    
    

NOTIFICATION::

 

find_real_file.png

 

 

here is the email log::

 

find_real_file.png

As you can see, I am very close.  I was able to get the event to fire, but the 'to' and the 'cc' fields are not right.

help!

 

 

1 ACCEPTED SOLUTION

Yousaf
Giga Sage

Hi Juan,

I am not sure if You can set TO using email script. You can do that using event queue
How to Add "To" Address in email
How to set to address in email scripts?

whereas CC can be set using .email.addAddress('cc','email','name')
and to check cc it will not show like that 
yout have to go to Configure > form layout and bring "Copied" field to form 
There you can see everyone you added in CC


Mark Correct or Helpful if it helps.


***Mark Correct or Helpful if it helps.***

View solution in original post

3 REPLIES 3

Yousaf
Giga Sage

Hi Juan,

I am not sure if You can set TO using email script. You can do that using event queue
How to Add "To" Address in email
How to set to address in email scripts?

whereas CC can be set using .email.addAddress('cc','email','name')
and to check cc it will not show like that 
yout have to go to Configure > form layout and bring "Copied" field to form 
There you can see everyone you added in CC


Mark Correct or Helpful if it helps.


***Mark Correct or Helpful if it helps.***

Tony Chatfield1
Kilo Patron

Hi, this question is basically the same as the one you asked here

email script can do gs.error() but not send email ! - Developer Community - Question - ServiceNow Co...

and multiple questions for the same issue are unnecessary and less likely to result in a clear outcome\answer to your question.

As Yousaf has mentioned you can use mail_script to set CC but not TO, probably as TO must exists for an email notification to be triggered.

Otherwise your formatting seems to match the structure defined in the documentation
Example scripting for email notifications (servicenow.com)

Without details of your testing process I would guess that you are trying to set 'to' in your mail-script and this causes the script to fail and subsequent methods do not run?

As a solution I would update the sysevent, and send 'to' as a separate parameter (either parm1 or parm2) you can then set 'TO' in the who will receive section of your Notification using the field 'Event param X contains recipient'. Then you can user the object you are creating as your other event parm, to set other values in your mail_script.

 

Mohith Devatte
Tera Sage
Tera Sage

@juan casas 

FOR CC YOU can use

email.addAddress("cc","email_address_of_the_user","Name_of_ther_user");

For to address i think you are sending it in parm1 of the event by encoding the user in JSON 

instead of that send the user sys_id directly in the parm 1 like below

gs.eventQueue("your_event_name",your object , "user_sys_id","");

then in the notifications of to who will receive  and select the check box "event parm 2 contains recepient"

find_real_file.png