Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Rusty,



I figured it out. It was a silly typo in my naming of the array:



I had "emailAddr" instead of "emailList" after I removed "current" and revised the code.



So the following code does now work:



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");



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



if (!emailList.nil()) {


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


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


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


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


} // End of for loop


} // End of if statement



I knew it was something so minute and silly but I just couldn't see it after staring at the problem for so long.



Thanks for your help on this.



Solo


Hi Solo,   pls mark my answer as helpful if I have helped you in anyway.   Thanks!


Either way, it still doesn't add those addresses to the bcc line. I tried with your typo and without ☺



I can't get that gs.log message to print out. It looks like it isn't even entering the lop but I don't know why.



Solo


Hi Solo,



Are you still looking for solution on this.



Thank you,




Ashutosh


Hi Ashutosh,



Rusty pointed out my main issue. However, in response to your questions I would say yes and no.



Yes because although I am able to get some of the addresses on the blind copy line, it appears to split the notifications going out into 3 separate emails because of this system   property: glide.email.smtp.max_recipients=100. So only one of the emails sent out has users on the bcc line while the other two don't. That is what I am trying to figure out now so if you have any suggestions, that would be appreciated. Increasing that system property still doesn't seem to correct the issue.



Thanks!