The CreatorCon Call for Content is officially open! Get started here.

How to trigger 1 notification that sends emails to 3 different email addresses, based on conditions?

ronro2
Tera Contributor

Hello Guys!

I'm trying to trigger one notification based on dmn_demand, that will be able to send 3 different emails depending on certain conditions. 

 

In the notification I have configured a "Send when: Revvord inserted or updated" 

ronro2_0-1725363836270.png

 

And then in an Email Script I have the following script: 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

    // Definiera e-postadressen baserat på sys_id
    var emailAddress;
    var companyConcerned = current.u_vgr_companies_concerned;

	//email1
    if (companyConcerned.indexOf('3d1fbtest') !== -1) {
        emailAddress = 'email1@example.se';

	//email2
    } else if (companyConcerned.indexOf('b51fbtest') !== -1) {
        emailAddress = 'email2@example.se';
	
	//email3
    } else if (companyConcerned.indexOf('a11fbtest') !== -1) {
        emailAddress = 'email3@example.se';
    }

    // Sätt mottagare
    email.setTo(emailAddress);

})(current, template, email, email_action, event);

 

However, no email is triggered when I try to test by changing state on a record based on table dmn_demand. I have referred to my Email Script name under "What it will contain" like this: 

${mail_script:email_script_behov_beredning_verksamhet}

Is there anything that I've missed? Do I need an event or a Business Rule? 


Thanks in advance!

5 REPLIES 5

Community Alums
Not applicable

Hi @ronro2 ,

I edit your email script please use below script 

// Definiera e-postadressen baserat på sys_id
    var emailAddress;
    var companyConcerned = current.u_vgr_companies_concerned;

	//email1
    if (companyConcerned.indexOf('3d1fbtest') !== -1) {
        emailAddress = 'email1@example.se';
		email.addAddress('cc','email1@example.se','name')

	//email2
    } else if (companyConcerned.indexOf('b51fbtest') !== -1) {
        //emailAddress = 'email2@example.se';
		email.addAddress('cc','email2@example.se','name')
	
	//email3
    } else if (companyConcerned.indexOf('a11fbtest') !== -1) {
        //emailAddress = 'email3@example.se';
		email.addAddress('cc','email3@example.se','name')
    }

    // Sätt mottagare
    email.setTo(emailAddress);

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards

Sarthak

Can you explain why you put those two extra parameters, and why there is a new variable called addAddress?

 

 

'cc','email1@example.se','name'​

 


What do they do? The solution is not working, no notification is triggered. How do I connect the email script to the notification other than putting this string in the HTML body: 

${mail_script:email_script_behov_beredning_verksamhet}

Community Alums
Not applicable

Hi @ronro2 ,

It is predefine method for email scripting from serviceNow. You can refer below documentation for reference 

https://service-nerd.com/how-to-add-cc-and-bcc-to-an-outbound-email-in-servicenow/

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak

This is my refreshed script: 

 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {


    // Skapa en GlideRecord för att hämta data från dmn_demand-tabellen
    var demandGR = new GlideRecord('dmn_demand');
    demandGR.addQuery('sys_id', current.sys_id);
    demandGR.query();

    if (demandGR.next()) {
        // Kontrollera om specifika sys_id:n finns i u_vgr_companies_concerned
        var companiesConcerned = demandGR.u_vgr_companies_concerned.split(',');

        // Skaraborgs sjukhus
        if (companiesConcerned.indexOf('3d1fbe1edbde5f0804ba9464db961902') !== -1) {
            email.addAddress('bcc', 'sisfunktion.skas@vgregion.se', 'Skaraborgs sjukhus');

        // Södra Älvsborgs sjukhus
        } else if (companiesConcerned.indexOf('b51fbe1edbde5f0804ba9464db961901') !== -1) {
            email.addAddress('bcc', 'sis-beredning.sas@vgregion.se', 'Södra Älvsborgs sjukhus');

        // Regionstyrelsen
        } else if (companiesConcerned.indexOf('a11f7e1edbde5f0804ba9464db9619b5') !== -1) {
            email.addAddress('bcc', 'koncernkontoret.sis-funktion@vgregion.se', 'Regionstyrelsen');
        }
    }

})(current, template, email, email_action, event);

 


I tried to follow the tutorial in the link you sent me, but it is not working. Can the cause be that I have a When To Send condition in the notification instead of having everything in the email script?