Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

To include MultiRow variable set informations as a dynamic HTML table into Email notifications

Vignesh11
Tera Expert

 

Create a email notification script "mrvsInfo" and add it in to the required email notifications with this syntax -> ${mail_script:mrvsInfo}

 

name: mrvsInfo

script

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


    // Fetch the MRVS data and display them as table in notification

    var catItem, currentRitmSysId, mrvsTitle, mrvsSysID, queri, msg, varNameArray = [],
        varQuestionArray = [];
    currentRitmSysId = current.sys_id.toString();
    catItem = current.cat_item.toString();


    var checkIO = new GlideRecord('io_set_item');
    checkIO.addEncodedQuery('variable_set.type=one_to_many^sc_cat_item=' + catItem);
    checkIO.query();
    while (checkIO.next()) {
        mrvsTitle = checkIO.variable_set.title.toString();
        mrvsSysID = checkIO.variable_set.sys_id.toString();


        var variable = new GlideRecord('item_option_new');
        variable.addEncodedQuery('variable_set=' + mrvsSysID);
        variable.orderBy('order');
        variable.query();
        while (variable.next()) {
            varNameArray.push(variable.sys_id.toString());
            varQuestionArray.push(variable.question_text.toString());
        }
        var qnLength = varNameArray.length;
        varNameArray = varNameArray.toString().split(',');
        var headerRowWidt = 100 / qnLength;

        var rowIndexArr = [],
            newRowIndex, rowLength;
        var record = new GlideRecord('sc_multi_row_question_answer');
        record.addQuery('parent_idSTARTSWITH' + currentRitmSysId + '^variable_set=' + mrvsSysID);
        record.orderBy('row_index');
        record.query();
        while (record.next()) {
            rowIndexArr.push(record.row_index.toString());
        }

        var au = new ArrayUtil();
        newRowIndex = au.unique(rowIndexArr);
        rowLength = newRowIndex.length;
        newRowIndex = newRowIndex.toString().split(',');

        //MRVS Table construction in HTML
        if (mrvsTitle) {
            template.print("<br><b>" + mrvsTitle + " :</b><br><br>");
            template.print('<table border =1>');
            template.print('<tr bgcolor = "#EEEEEE">');
            for (var i = 0; i < varQuestionArray.length; i++) {
                template.print('<th style="width:' + headerRowWidt + '%"><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">' + varQuestionArray[i] + '</span></th>');
            }
            template.print("</tr>");

            for (j = 0; j < rowLength; j++) {
                msg = '';
                for (var k = 0; k < qnLength; k++) {

                    queri = 'parent_idSTARTSWITH' + currentRitmSysId + '^variable_set=' + mrvsSysID + '^row_index=' + newRowIndex[j] + '^item_option_new=' + varNameArray[k];

                    var record2 = new GlideRecord('sc_multi_row_question_answer');
                    record2.addEncodedQuery(queri);
                    record2.query();
                    if (record2.next()) {

                        msg += '<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">' + record2.value.getDisplayValue() + '</span></td>';

                    }
                }
                template.print('<tr>' + msg + '</tr>');
            }
            template.print("</table>");
        }
    }

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

//This function accept sys_id and table and returns the Display name.
function getName(sys_id, tblName) {
    var rec = new GlideRecord(tblName);
    if (rec.get(sys_id)) {
        return rec.getDisplayValue();
    }
}
 
#mrvs #multiRow #dynamicMRVS
0 REPLIES 0