How to override "who will receive'' in mail script

rpoola
Tera Contributor

Hi all,

I got requirement like I need to check work email (new field in User table) exists. If it exists I need to trigger all notifications to work email other wise I need to send default email in User. Currently all notifications triggering to Default email.

As it is already in Production I cannot do changes to all notifications using param1 or param2. Is there anything we can do in mail script, so I can use common mail script to all notications.

I have seen email.addAddress(String type, String address, String displayname). But here we can use either CC or BCC. But I want to override email To.

Thanks

Ravikiran

1 REPLY 1

Sashi K1
Kilo Guru

Hi Ravikiran


We have achieved this using a BR on Email script. Which runs based on a property called 'work_email' true then it replaces out going messages with work email.


It worked well, but cautious. One mistake would potentially impacts your Email server. For example when a work_email value is empty, property is enabled, no proper condition on the BR, it would impact on outgoing Emails.



A proper script could easily updates outgoing mails, as long as you want all out going mails to be replaced without having a dependency on source sender.


---sample code



NOT TESTED   *** MY INTENTION IS TO SHOW A CODE STRUCTURE and share some logic. I just typed while composing mail. You can take this approach to replace your recipients list at run time with work mail if exists, otherwise they would keep their primary email.




onBefore insert BR


Table: sys_email



BR Advanced Script:


//check if work_email property is enabled, if yes continue otherwise skip BR



var emailUtil = new EmailAPIUtil();


var finalReceipients = emailUtil.replaceMailsIfExists(current.recipients+"");   //send all TO addresses to make sure they are replaced with work_Email




---- script include method ----



replaceMailsIfExists: function (recipients) {


    gs.log(" Email Exclude: Recipients received from Email : "+ recipients);


    var workEmailArr = this.loadBackUpEmailsForEachRecipient(recipients);


      gs.log(" Email Exclude: Emails received from workbased are"+workEmailArr.toString());



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


      gs.log(" Email Exclude: "+i+" check details "+recipients+" and indexing "+workEmailArr[i]);


   


//now replace each recipient id with work mail


if (JSUtil.notNil(recipients) && recipients.indexOf(workEmailArr[i]) != -1){


             


              gs.log(" Email Work: Backup user found was "+workEmailArr[i]);


              recipients = recipients.replace(recipients, workEmailArr[i]);


    }


}



gs.log(" Email Exclude: The final recipients list was "+recipients);


return recipients;


},



getBackUpMail: function (usrMail) {



    //Glide Record backUpWorkEmail from sys_user table.


  var gr = new GlideRecord("sys_user");


  gr.addQuery("primarymail",usrMail);


  gr.query();



  if (gr.next()) {


                usrMail = gr.work_email;


}



    return usrMail;


},




loadBackUpEmailsForEachRecipient: function(recipients) {


var finalList = [];


for (var user in recipients) {


var workMail = this.getBackUpRep(recipients[user]);


if(JSUtil.notNil(workMail))


{



finalList.push(execUser.toString());


}


}



return finalList;


},





---------



current.recipients = finalReceipients;