logo

NJP

Displaying Multi-Row Variable Sets on Catalog Tasks

Import · Feb 25, 2020 · article

Unfortunately, ServiceNow's OOB Flow actions do not copy multi-row variable sets, so if you want your MRVS to be visible on a catalog task you have to navigate back to the requested item related to them.

Here's a nice little work around using a client script and a script includes, using a little jQuery to insert a table on-the-fly that looks like this on the catalog task...

image

This approach caters for any number of multi-row variable sets on a requested item and will look up select box and reference values. Here's how it works...

An on load client script runs, using a Glide Ajax call to check to see if there are any mrvs. Don't forget to turn OFF the isolate script option on your client script or it won't be able to access jQuery. Your client script should be defined on the table sc_task.

function onLoad() {

    if(g_form.getValue('request_item')!=''){

        //Check for a multi-row variable set
        var gaMRVS = new GlideAjax('multRowVariableSets');
        gaMRVS.addParam('sysparm_name','getQA');
        gaMRVS.addParam('sysparm_sys_id',g_form.getValue('request_item'));
        gaMRVS.getXMLAnswer(displayResults);

    }

    ///////////////////////////////////////////////////////
    function displayResults(results){

        var allMRVS = JSON.parse(results), htmlTable = '';

        allMRVS.forEach(function(mvrs){

            if(mvrs.details.length !=0){

                //First, sort results by row then by order
                mvrs.details.sort(function(a, b) {if (a.row === b.row) {return a.order > b.order ? 1 : -1;} return a.row > b.row ? 1 : -1;});

                //We'll add our MRVS as a table beneath the description field
                htmlTable+= '<div class="form-group"><div><label class="col-xs-12 col-md-1_5 col-lg-2 control-label"><span aria-label="" data-dynamic-title="" mandatory="false" oclass="" class=" "></span><span title="" class="label-text" data-html="false" data-original-title="" aria-expanded="false">'+mvrs.name+'</span></label></div><div class="col-xs-10 col-md-9 col-lg-8 form-field input_controls"><table style="border-collapse: collapse;border-right:1px solid silver;border-bottom:1px solid silver;width:100%"><tr>';

                //Get the first row number
                var rowNumber = mvrs.details[0].row;

                //get column headers
                mvrs.details.forEach(function(thisEntry){
                    if(thisEntry.row == rowNumber){
                        htmlTable+= '<td style="padding:10px !important;border-left:1px solid silver;border-top:1px solid silver;background-color:WhiteSmoke;">'+thisEntry.question+'</td>';
                    }
                });

                rowNumber = '';

                //add each row
                mvrs.details.forEach(function(thisEntry){
                    //insert a new row each time the row number changes
                    if(thisEntry.row != rowNumber) htmlTable+= '</tr><tr>';
                    //add each individual cell value (knowing it has been sorted correctly so they'll all be in order)
                    htmlTable+= '<td style="padding:10px !important;border-left:1px solid silver;border-top:1px solid silver;">'+thisEntry.value+'</td>';
                    rowNumber = thisEntry.row;
                });

                htmlTable+= '</tr></table></div>';
            }
        });

        jQuery('#element\\.sc_task\\.description').after(htmlTable);
    }
}

Then at the back end all we need is a Script Include to do the heavy lifting for us... Make sure and the script include is client callable.

var multRowVariableSets = Class.create();
multRowVariableSets.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getQA: function(){
        //Returns a JSON object representing all the MRVS for a particular requested item along with their values
        var sys_id = this.getParameter('sysparm_sys_id') || '2a175e5edb830090b3da126b3a9619b1';
        var allMRVS = [];
        var ritm = new GlideRecord('sc_req_item');
        if(ritm.get(sys_id)){

            //loop through the variables looking for multi-row variable sets
            for(var eachVar in ritm.variables){

                //found one!
                if(ritm.variables[eachVar].isMultiRow()){

                    //////////////////////////////////////////
                    //get Multi-Row Variable Set structure
                    var mrvsDefintion = {}, title='';
                    var mrvsStructure = new GlideRecord('item_option_new');
                    mrvsStructure.addEncodedQuery('active=true^variable_setISNOTEMPTY^variable_set.internal_name=' + eachVar);
                    mrvsStructure.orderBy('order');
                    mrvsStructure.query();

                    while(mrvsStructure.next()){
                        //What is the title of this MRVS?
                        if(title=='') title = mrvsStructure.variable_set.title.toString();
                        //What about each of the variables
                        mrvsDefintion[mrvsStructure.name.toString()] = {"name" :    mrvsStructure.name.toString(),
                                                                        "question": mrvsStructure.question_text.toString(),
                                                                        "sys_id":   mrvsStructure.sys_id.toString(),
                                                                        "type":     mrvsStructure.type.getDisplayValue(),
                                                                        "table":    mrvsStructure.type.getDisplayValue()=="Reference" ? mrvsStructure.reference.getValue() : "",
                                                                        "order":    mrvsStructure.order.toString(),
                                                                        "row":      "",
                                                                        "value":    ""};
                    }

                    //////////////////////////////////////////
                    //get the Multi-Row Variable Set values
                    var mrvsValue = [];
                    var mrvsAnswers = new GlideRecord('sc_multi_row_question_answer');
                    mrvsAnswers.addEncodedQuery('parent_id='+sys_id+'^variable_set.internal_name='+ eachVar);
                    mrvsAnswers.orderBy('row_index');
                    mrvsAnswers.query();

                    while(mrvsAnswers.next()){
                        var thisVariable = mrvsAnswers.item_option_new.name.toString();
                        if(mrvsDefintion.hasOwnProperty(thisVariable)){
                            //Get value 
                            var thisValue = mrvsAnswers.value.toString();

                            //if this is a reference field get the display value
                            if(mrvsDefintion[thisVariable].type=='Reference' && mrvsDefintion[thisVariable].table!=''){

                                var getDisplayVal = new GlideRecord(mrvsDefintion[thisVariable].table);
                                if(getDisplayVal.get(thisValue)){
                                    thisValue = getDisplayVal.getDisplayValue();
                                }
                            }

                            //If this is a select box with choices, get the question_choice (display value)
                            if(mrvsDefintion[thisVariable].type=='Select Box'){

                                var getQuestionChoice = new GlideRecord('question_choice');
                                getQuestionChoice.addEncodedQuery('question='+ mrvsDefintion[thisVariable].sys_id +'^value=' + thisValue);
                                getQuestionChoice.query();
                                if(getQuestionChoice.next()){
                                    thisValue = getQuestionChoice.text.toString();
                                }
                            }

                            mrvsDefintion[thisVariable].value = thisValue;
                            mrvsDefintion[thisVariable].row   = mrvsAnswers.row_index.toString();
                            mrvsValue.push(JSON.parse(JSON.stringify(mrvsDefintion[thisVariable])));//push in a clean object
                        }
                    }
                    allMRVS.push({"name": title, "details": mrvsValue});
                }
            }
        }
        return JSON.stringify(allMRVS);
    },

    type: 'multRowVariableSets'
});

Have fun

View original source

https://www.servicenow.com/community/now-platform-articles/displaying-multi-row-variable-sets-on-catalog-tasks/ta-p/2309253