unable to insert mvrs data into email notification

kam4
Tera Contributor

Hi Al,

I have an issue where i have create an email notification and try to insert mvrs data into the email notification content but it was not able to show anything, I attach the screenshot for your reference.

 

email notification Table

Requested Item (SC_re_item)

 

Email script

namePrintMRVSData

scripts:

"(function runMailScript(current, template, email, email_action, event) {
// Get the sys_id of the requested item
var ritmSysId = current.sys_id;

// Get the MRVS data (example: 'u_mrvs_variable_name')
var mrvsData = current.variables.u_vs_user_info_celonis; // Replace with your MRVS variable name

// Check if data exists
if (mrvsData && mrvsData.getRowCount() > 0) {
var tableHtml = '<table>';
tableHtml += '<tr><th>Column 1</th><th>Column 2</th></tr>'; // Replace with your column headers

for (var i = 0; i < mrvsData.getRowCount(); i++) {
var row = mrvsData.getRow(i);
tableHtml += '<tr>';
tableHtml += '<td>' + row.u_region + '</td>'; // Replace with your column values
tableHtml += '<td>' + row.u_region + '</td>';
tableHtml += '</tr>';
}
tableHtml += '</table>';

 

template.print(tableHtml);
} else {
template.print('No data found in Multi-row variable set.');
}
})(current, template, email, email_action, event);"

 

 

 

10 REPLIES 10

Arun_Manoj
Mega Sage

Hi @kam4 ,

please check this community post

https://www.servicenow.com/community/itsm-forum/send-notification-to-email-addresses-in-multi-row-va...

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,

Arun Manoj

kam4
Tera Contributor

Dear Arun,

 

Thank you for your sharing and I have tried it the same script, but it does not able to show the information in the email notification.

Ankur Bawiskar
Tera Patron
Tera Patron

@kam4 

this should help you

Multi-row variable set in Notifications via Notifications Email Script 

update your script as this

(function runMailScript(current, template, email, email_action, event) {
    // Replace with your actual MRVS variable name
    var mrvsVarName = 'u_vs_user_info_celonis';
    var mrvsData = current.variables[mrvsVarName];

    if (mrvsData) {
        // Parse the JSON string
        var rows = JSON.parse(mrvsData + '');
        if (rows.length > 0) {
            var tableHtml = '<table border="1"><tr><th>Region</th></tr>';
            for (var i = 0; i < rows.length; i++) {
                tableHtml += '<tr><td>' + rows[i].u_region + '</td></tr>';
            }
            tableHtml += '</table>';
            template.print(tableHtml);
        } else {
            template.print('No data found in Multi-row variable set.');
        }
    } else {
        template.print('No data found in Multi-row variable set.');
    }
})(current, template, email, email_action, event);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Dear Ankur,

 

Thank you for your solution but it was able to show the table or information shown the email notification.

 

here the email scripts I have change accordingly.

"

(function runMailScript(current, template, email, email_action, event) {
    // Replace with your actual MRVS variable name
    var mrvsVarName = 'u_vs_user_info_celonis';
    var mrvsData = current.variables.u_region;

    if (mrvsData) {
        // Parse the JSON string
        var rows = JSON.parse(mrvsData + '');
        if (rows.length > 0) {
            var tableHtml = '<table border="1"><tr><th>Region</th></tr>';
            for (var i = 0; i < rows.length; i++) {
                tableHtml += '<tr><td>' + rows[i].u_region + '</td></tr>';
            }
            tableHtml += '</table>';
            template.print(tableHtml);
        } else {
            template.print('No data found in Multi-row variable set.');
        }
    } else {
        template.print('No data found in Multi-row variable set.');
    }
})(current, template, email, email_action, event);"