how to get variable set values in email script from flow

mrunalnikhi
Tera Contributor

I am trying to read asset varibles from variable set. But it is coming as empty value with below script.
what is correct way to read array.object variable in email script. PFA for ref.

Email Script:

(function runMailScript(current, template, email, email_action, event) {

     var mrvs = current.variables.mrvs_asset_details;

    gs.info('MRVS rows: ' + current.variables.mrvs_asset_details.getRowCount());

    template.print('<table border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse;">');

    template.print(
        '<tr>' +
        '<th>S.No</th>' +
        '<th>Asset Tag</th>' +
        '<th>Serial Number</th>' +
        '<th>User ID</th>' +
        '<th>Employee Name</th>' +
        '<th>Location</th>' +
        '<th>Department</th>' +
        '<th>Asset Type</th>' +
        '</tr>'
    );

    for (var i = 0; i < mrvs.getRowCount(); i++) {

        var row = mrvs.getRow(i);

        template.print('<tr>');

        template.print('<td>' + (i + 1) + '</td>');
        template.print('<td>' + row.asset_tag + '</td>');
        template.print('<td>' + row.serial_number + '</td>');
        template.print('<td>' + row.mid + '</td>');
        template.print('<td>' + row.emp_name + '</td>');
        template.print('<td>' + row.location + '</td>');
        template.print('<td>' + row.department + '</td>');
        template.print('<td>' + row.asset_type + '</td>');

        template.print('</tr>');
    }

    template.print('</table>');

})(current, template, email, email_action, event);
mrunalnikhi_1-1795755774799.png

 


 

mrunalnikhi_2-1795755810077.pngmrunalnikhi_3-1795755909834.pngmrunalnikhi_4-1795756094528.png

 

7 REPLIES 7

I found that in flow execution get catalog variables, mrvs_asset_details itself has empty value 

but in RITM it is showing

Gaurav Vaze
Kilo Sage

I am not sure if the following is exactly the reason why the values are empty, but there is possibility that passing the current might not giving the email script enough glide ability, I suggest you do explicitly glide the current ritm
following is AI generated example script that you can try:

(function runMailScript(current, template, email, email_action, event) {

    // 1. Re-query the RITM so the variables pool initializes properly in Flow context
    var ritm = new GlideRecord('sc_req_item');
    if (!ritm.get(current.getUniqueValue())) {
        gs.error('MRVS Mail Script: Could not load RITM ' + current.getUniqueValue());
        return;
    }

    var mrvs = ritm.variables.mrvs_asset_details;

    // Debug logs (check System Logs > All to verify what is returned)
    gs.info('MRVS Debug - RITM: ' + ritm.number + ' | Type: ' + typeof mrvs + ' | Raw: ' + mrvs);

    if (!mrvs) {
        return;
    }

    // 2. Parse rows safely (works whether ServiceNow returns a JSON string or MRVS object)
    var rows = [];
    var rawString = mrvs.toString();

    if (rawString.startsWith('[')) {
        rows = JSON.parse(rawString);
    } else if (typeof mrvs.getRowCount === 'function') {
        for (var j = 0; j < mrvs.getRowCount(); j++) {
            var r = mrvs.getRow(j);
            rows.push({
                asset_tag: r.asset_tag.getDisplayValue(),
                serial_number: r.serial_number.getDisplayValue(),
                mid: r.mid.getDisplayValue(),
                emp_name: r.emp_name.getDisplayValue(),
                location: r.location.getDisplayValue(),
                department: r.department.getDisplayValue(),
                asset_type: r.asset_type.getDisplayValue()
            });
        }
    }

    if (rows.length === 0) {
        gs.info('MRVS Debug - 0 rows parsed for ' + ritm.number);
        return;
    }

    // 3. Render the HTML Table
    template.print('<table border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse;">');
    template.print(
        '<tr>' +
        '<th>S.No</th>' +
        '<th>Asset Tag</th>' +
        '<th>Serial Number</th>' +
        '<th>User ID</th>' +
        '<th>Employee Name</th>' +
        '<th>Location</th>' +
        '<th>Department</th>' +
        '<th>Asset Type</th>' +
        '</tr>'
    );

    for (var i = 0; i < rows.length; i++) {
        var row = rows[i];

        template.print('<tr>');
        template.print('<td>' + (i + 1) + '</td>');
        template.print('<td>' + (row.asset_tag || '') + '</td>');
        template.print('<td>' + (row.serial_number || '') + '</td>');
        template.print('<td>' + (row.mid || '') + '</td>');
        template.print('<td>' + (row.emp_name || '') + '</td>');
        template.print('<td>' + (row.location || '') + '</td>');
        template.print('<td>' + (row.department || '') + '</td>');
        template.print('<td>' + (row.asset_type || '') + '</td>');
        template.print('</tr>');
    }

    template.print('</table>');

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

 

Please go through the same before use, and mark this as helpful if it works

Thank you!

Gaurav Vaze
SN Developer

Ehab Pilloor
Giga Sage

Hi @mrunalnikhi,

 

Add MRVS in logs to verify that the JSON value is fetched in email script for debugging. Script looks good.

Add a 2-3 second delay before the notification is triggered. Make sure the user who is receiving has valid email format so that email log record is created.

 

If this response resolved your issue, please consider marking it as Helpful and accepting it as the Solution. Your feedback helps other ServiceNow Community members find reliable answers.

Kind Regards,

Ehab Pilloor