how to add variable from business rule to email script

nabeel55
Tera Contributor

i got a list of managers in a variable in the business rule, and now i want to send a notification to the list of managers who stored in a variable with the email script how do it?

in the email script 

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

          // Add your code her
   
    // Your array of managers  [how to add  array of managers from business rule here in email script]
    var managers = [
        { email: 'manager1@example.com', name: 'Manager 1' },
        { email: 'manager2@example.com', name: 'Manager 2' },
        // Add more managers as needed
    ];

    // Loop through the array of managers and add them to the CC list
    for (var i = 0; i < managers.length; i++) {
        email.addAddress("cc", managers[i].email, managers[i].name);
    }

})(current, template, email, email_action, event);
1 ACCEPTED SOLUTION

saurabh_dubey
Kilo Sage
//Inside Business rule you can write as
var managers = [
        { email: 'manager1@example.com', name: 'Manager 1' },
        { email: 'manager2@example.com', name: 'Manager 2' },
        // Add more managers as needed
    ];
gs.eventQueue('your_event_name',current,current.number,managers);

// inside email template you can use by... managers array with the help of event object....
for(var i=0;i<event.managers.length;i++){
email.addAddress("cc",event.managers[i].email,event.managers[i].name)
}

Do mark my answer helpful and hit the solution button...

 

Thanks 

Saurabh Dubey.

View solution in original post

3 REPLIES 3

AshishKM
Kilo Patron
Kilo Patron

Hi @nabeel55 , 

Use the event based email notification and trigger that event with recipient details in the loop. 

In BR, iterate the manager's arrayList and trigger the event for email notification.

 

-Thanks,

AshishKM 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

saurabh_dubey
Kilo Sage
//Inside Business rule you can write as
var managers = [
        { email: 'manager1@example.com', name: 'Manager 1' },
        { email: 'manager2@example.com', name: 'Manager 2' },
        // Add more managers as needed
    ];
gs.eventQueue('your_event_name',current,current.number,managers);

// inside email template you can use by... managers array with the help of event object....
for(var i=0;i<event.managers.length;i++){
email.addAddress("cc",event.managers[i].email,event.managers[i].name)
}

Do mark my answer helpful and hit the solution button...

 

Thanks 

Saurabh Dubey.

Hi @nabeel55 ,

 

Please refer this code if you are going to implement it... I tried it on my personal instance as well so this will help you.

 

// Your Business Rule will look like this.
(function executeRule(current, previous /*null when async*/ ) {
    var managers = [{
            email: 'abraham.lincoln@example.com',
            name: 'Abraham Lincoln'
        },
        {
            email: 'aileen.mottern@example.com',
            name: 'Aileen Mottern'
        },
    ];

     gs.eventQueue('sending_mail_managers', current, current.number, JSON.stringify(managers));
})(current, previous);

// Your email script I have logged the contents so that you can understand
// Add your code here
	var mails = JSON.parse(event.parm2);
	gs.info("Lets getb the JSON object in cc"+typeof event.parm2);
    for (var i = 0; i < mails.length; i++) {
        email.addAddress("cc", mails[i].email, mails[i].name);
		gs.info("managers arre here "+mails[i].email);
    }
// This is the code I have tried it and logged it so you can see the variables

 

Do mark this solution as accepted and mark this as helpful... so that it can reach to others... 

 

Thanks and regards,

Saurabh Dubey.