Adding email addresses from (event.parm1) to bcc in a notification email script isn't working

display
Kilo Guru

This is probably the simplest and most documented example, however, I am unable to get it to work and after staring at this issue for a few hours, I decided to ping the community to see if someone can see why this isn't working.

Goal: When a UI page is filled in with information, capture the information and email it to a bunch of users but instead of listing all email addresses on a single TO line, add the email addresses to the Bcc field so that it appears the email only went out to a single user. That way, the recipient won't perform a reply all and send and email response to EVERYONE on the list.

What I did:

I have a custom UI page that passes an array of email address in event.parm1 and a JSON object in event.parm2 to the event manager to trigger an event.

This part works beautifully. I can see in the event logs that parm1 contains a list of email addresses separated by commas and parm2 contains my object.

I have an email notification that is triggered off of this event and it has the following line in the message HTML body:

${mail_script:process_support_alert}

I have a notification email script called "process_support_alert" with the following code:

var parser = new JSONParser();

var parsedData = parser.parse(event.parm2);

// Define an array and assign it the value of event.parm1

var emailList = [];

emailList = event.parm1;

// If the array with address is not empty, split the addresses based on a comma

if (!current.emailList.nil()) {

    var emailAddr = current.emailList.split(",");

// Add each address to the bcc line of the email

for (var i = 0; i < emailAddr.length; i++) {

      email.addAddress("bcc", emailAddr[i], "");

      gs.log("Email address and display name added to bcc line" + emailAddr[i]);

} // End of for loop

} // End of if statement

email.setSubject(parsedData.short_description);

email.setBody(parsedData.emailBody + "\n\n" + parsedData.contactName + " " + parsedData.contactPhone + "\n" + "Message was submitted on" + $sys_updated);

When I look at the email under the "Sent" mailbox, the "blind copied" field is empty. I see the list of email addresses in the "Recipients" field and the "To" field only.

I initially queried the user table so that I could also get the display value of the user of the email address but I stripped that out just to test if I could get any of the email addresses on the bcc line to begin with. So my full code looks like so:

var parser = new JSONParser();

var parsedData = parser.parse(event.parm2);

// Define an array and assign it the value of event.parm1

var emailList = [];

emailList = event.parm1;

// If the array with address is not empty, split the addresses based on a comma

if (!current.emailList.nil()) {

    var emailAddr = current.emailList.split(",");

// Add each address to the bcc line of the email

for (var i = 0; i < emailAddr.length; i++) {

    var getAddr = new GlideRecord('sys_user');

    getAddr.addQuery('email', emailAddr[i]);

    getAddr.query();

          while(getAddr.next()) {

                            email.addAddress("bcc", emailAddr[i], getAddr.name.getDisplayValue());

          } // End of while loop

      gs.log("Email address and display name added to bcc line:" + emailAddr[i]);

} // End of for loop

} // End of if statement

Nothing is sent to the log. What am I missing? I am sure it is something really silly but I can't seem to find my error.

Thanks!

Solo

1 ACCEPTED SOLUTION

ruzzty06
Tera Expert

you have this line



// Define an array and assign it the value of event.parm1


var emailList = [];


emailList = event.parm1;


// If the array with address is not empty, split the addresses based on a comma


if (!current.emailList.nil()) {


    var emailAddr = current.emailList.split(",");




but I think you are trying to refer to the variable not a field, so it should be just emailList, not current.emailList




and you could do this instead




var emailAddList=event.parm1.split('1');




Please mark as correct or helpful.


View solution in original post

9 REPLIES 9

ruzzty06
Tera Expert

you have this line



// Define an array and assign it the value of event.parm1


var emailList = [];


emailList = event.parm1;


// If the array with address is not empty, split the addresses based on a comma


if (!current.emailList.nil()) {


    var emailAddr = current.emailList.split(",");




but I think you are trying to refer to the variable not a field, so it should be just emailList, not current.emailList




and you could do this instead




var emailAddList=event.parm1.split('1');




Please mark as correct or helpful.


Good catch Rusty. I can't believe I missed that.



However, even after removing the keyword "current" and going through the process of collecting data from the UI page to trigger the event, the blind copied field is still empty on the email.



This is the most recent code:



var parser = new JSONParser();


var parsedData = parser.parse(event.parm2);


email.setFrom("suppalrt@colorado.edu");


email.setSubject(parsedData.short_description);


email.setBody(parsedData.emailBody + "\n\n" + parsedData.contactName + " " + parsedData.contactPhone + "\n");



// Define an array and assign it the value of event.parm1


var emailList = [];


emailList = event.parm1;



// If the array with address is not empty, split the addresses based on a comma


if (!emailList.nil()) {


    var emailAddr = emailList.split(",");



// Add each address to the bcc line of the email


for (var i = 0; i < emailAddr.length; i++) {


      email.addAddress("bcc", emailAddr[i], "");


      gs.log("Email address and display name added to bcc line" + emailAddr[i]);


} // End of for loop


} // End of if statement



I even tried the following based on your second suggestion:



var parser = new JSONParser();


var parsedData = parser.parse(event.parm2);


email.setFrom("suppalrt@colorado.edu");


email.setSubject(parsedData.short_description);


email.setBody(parsedData.emailBody + "\n\n" + parsedData.contactName + " " + parsedData.contactPhone + "\n" + "Message was submitted on");



var emailList = event.parm1.split('1');



if (!emailList.nil()) {


// Add each address to the bcc line of the email


for (var i = 0; i < emailList.length; i++) {


      email.addAddress('bcc', emailAddr[i], "");


      gs.log("Email address and display name added to bcc line" + emailList[i]);


} // End of for loop


} // End of if statement



Still no addresses on the blind copied line of the email.



SupportAlertIssue.png


sorry typo error in


var emailAddList=event.parm1.split('1');


should be


var emailAddList=event.parm1.split(',');



do you know if you are entering the loop? are there log messages?



Please mark as correct or helpful.


Check also if your email script is even executed? put a log on the first line.