send email to manager including employee name inside email body Multirowvariable set

si21
Tera Guru

Hi experts,

We have MRVS record producer with variables Employee and Manager. Currently we event triggered by Business rule that is sending email notification to each manager in each row separately.

 

How can add employee name related to each row manager in the email body. Current email script is displaying employee from all rows.

 

for example, in the below scenario currently is going to John and Gaurang separately but in both emails, Despina and JT Holtshlag names are displayed in email body.

Requirement: in the email sent to john, only employee Desipina name should be in email body

si21_1-1732773375632.png

 

 

 

 

si21_0-1732773291817.png

 

 

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

        // Add your code here
        var mrvs;
        mrvs = current.variables.for_my_employee_s; //mention your mrvs internal name here
        var rowCount = mrvs.getRowCount();

        for (var i = 0; i < rowCount; i++) {
            var row = mrvs.getRow(i);
            var grp = new GlideRecord('sys_user');
            grp.addQuery('sys_id', row.employee_name);
            grp.query();
            if (grp.next()) {
                template.print(grp.name);

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

 TIA

 

3 REPLIES 3

mahemoodhus
Tera Contributor

Below is an example email script for your notification:

// Assuming 'mrvs_variable' is the name of your MRVS variable var mrvsName = 'mrvs_variable'; var emailBody = ''; // Fetch the MRVS data var mrvs = JSON.parse(current.variables[mrvsName]); // Get the current email recipient's email address (manager's email) var recipientEmail = email.to; // Iterate through the MRVS rows mrvs.forEach(function (row) { var employee = row.employee; // Replace with your variable name for Employee var managerEmail = row.manager.email; // Replace with your variable name for Manager email // Check if the manager in this row matches the email recipient if (managerEmail === recipientEmail) { emailBody += 'Employee: ' + employee + '<br>'; // Customize the row display as needed } }); // If no matching employees were found for the manager, add a placeholder if (!emailBody) { emailBody = 'No employees found for you in this request.'; } // Append the email body to the notification template.print(emailBody);

 

 

How It Works

  1. Fetch MRVS Data: The current.variables[mrvsName] holds the JSON data of the MRVS. You parse it using JSON.parse().

  2. Filter by Manager: Match the manager.email field in the MRVS rows with the current email recipient’s email (email.to).

  3. Build and Print the Email Body: Dynamically construct a string containing only the employee names associated with the current manager and print it into the email.


Example Scenario

MRVS Data:

 

 
[ { "employee": "Despina", "manager": { "email": "john@example.com" } }, { "employee": "JT Holtshlag", "manager": { "email": "gaurang@example.com" } } ]

 

 

Email to John:

 

 
Subject: Notification for Employees Body: Employee: Despina

 

 

Email to Gaurang:

 

 
Subject: Notification for Employees Body: Employee: JT Holtshlag
 

 

si21
Tera Guru

Hi @Brad Bowman  , could you please provide your inputs here.

Juhi Poddar
Kilo Patron

Hello @si21 

Please try this script

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

    // Retrieve the MRVS variable
    var mrvs = current.variables.for_my_employee_s; // Replace with your MRVS internal name
    var rowCount = mrvs.getRowCount();

    // Get the email recipient (manager)
    var emailRecipient = email.recipients.toString();

    // Initialize a variable to store employees for this recipient
    var employeeNames = [];

    // Loop through each row in the MRVS
    for (var i = 0; i < rowCount; i++) {
        var row = mrvs.getRow(i);

        // Check if the manager's email matches the recipient email
        if (row.employee_manager.email == emailRecipient) {
            // Add the employee's name to the array
            employeeNames.push(row.employee_name.getDisplayValue());
        }
    }

    // If employees are found for the manager, include them in the email body
    if (employeeNames.length > 0) {
        template.print("The following employees are associated with you:<br>");
        template.print(employeeNames.join("<br>"));
    } else {
        template.print("No employees are assigned to you in this request.");
    }
})(current, template, email, email_action, event);

 

"If you found my answer helpful, please give it a like and mark it as the "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar