Multi Row Variable Set Display Value

suryareddyn
Giga Expert

I have a requirement to get MVR values . Below is the code to get all variables including MVR. All MVR reference values are returning sys id instead of display value. Is there any way to get display value with out mentioning the reference table name?  

 

Code should work for any MVR reference fields.

 

 Example : 

0 - User: 04dca91aeb009c104b8a067ed3961345
var current;
var retString="\n";
var gr = new GlideRecord('sc_req_item');
if (gr.get('31367908875bc950d451cb77cebb3565')) {        // RITM sys id
    current = gr;
}

var count = 0;
for (vars in current.variable_pool) {
    count++;
    break;
}

var hasVars = count > 0; //check that this record has variables

if (hasVars) {//If a variable pool exists then collect variables with valid content
    var table = current.getTableName();
    var itemVars = current.variables.getElements(true);

    for (var i = 0; i < itemVars.length; i++) {
        var varToUse = itemVars[i];
        var isMultiRow = varToUse.isMultiRow();
        if (isMultiRow) {
            var vCount = varToUse.getRowCount();
            if (vCount > 0) {
                var rows = varToUse.getRows();
                var vId = itemVars[i].getLabel();

                for (var j = 0; j < varToUse.getRowCount(); j++) {
                    var row = rows[j];
                    var cells = row.getCells();

                    for (var k = 0; k < cells.length; k++) {
                        var cell = cells[k];
                       // gs.print('Row ' + j +' - ' + cell.getLabel() + ': ' + cell);
                       retString +=   j +' - ' + cell.getLabel() + ': ' + cell + "\n";
                    }
                }
            }
        }

        else {
            var thisQuestion = varToUse.getQuestion();
           // gs.print(thisQuestion.getLabel() + ': ' + thisQuestion.value.toString().trim());
           retString += thisQuestion.getLabel() + ': ' + thisQuestion.getDisplayValue().toString().trim() + "\n";
        }
    }
    gs.print(retString);
}
1 ACCEPTED SOLUTION

suryareddyn
Giga Expert

Below code returning display values:

 

 

var current = new GlideRecord('sc_req_item');
current.get('31367908875bc950d451cb77cebb3565');

var mrvs = current.variables.potential_students_remote;

var totalRows = mrvs.getRowCount();

for (var i = 0; i < totalRows; i++) {
//gs.addInfoMessage('Display Value: ' + mrvs.getRow(i).getCell('student').getCellDisplayValue());
}

var current;
var retString="<br>";
var gr = new GlideRecord('sc_req_item');
if (gr.get('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')) {
current = gr;
}

var count = 0;
for (vars in current.variable_pool) {
count++;
break;
}

var hasVars = count > 0; //check that this record has variables

if (hasVars) {//If a variable pool exists then collect variables with valid content
var table = current.getTableName();
var itemVars = current.variables.getElements(true);

for (var i = 0; i < itemVars.length; i++) {
var varToUse = itemVars[i];
var isMultiRow = varToUse.isMultiRow();
if (isMultiRow) {
var vCount = varToUse.getRowCount();
if (vCount > 0) {
var rows = varToUse.getRows();
var vId = itemVars[i].getLabel();

for (var j = 0; j < varToUse.getRowCount(); j++) {
var row = rows[j];
var cells = row.getCells();

for (var k = 0; k < cells.length; k++) {
var cell = cells[k];
var cell1=cells[k].getCellDisplayValue().toString().trim();

// gs.print('Row ' + j +' - ' + cell.getLabel() + ': ' + cell);
retString += j +' - ' + cell.getLabel() + ': ' + cell1 + "\n";
}
}
}
}

else {
var thisQuestion = varToUse.getQuestion();
// gs.print(thisQuestion.getLabel() + ': ' + thisQuestion.value.toString().trim());
retString += thisQuestion.getLabel() + ': ' + thisQuestion.getDisplayValue().toString().trim() + "\n";
}
}
gs.print(retString);
}

View solution in original post

6 REPLIES 6

Community Alums
Not applicable

Hi @suryareddyn ,

These threads have similar discussion which will help you :

https://community.servicenow.com/community?id=community_question&sys_id=3658cf96db3d17004816f3231f96...

https://community.servicenow.com/community?id=community_question&sys_id=905f4fe1dbdcdbc01dcaf3231f96...

Mark my answer correct & Helpful, if Applicable.

Thanks,
Sandeep

getDisplayValue() wont work for Multi Row Variable set. It gives undefined value

Community Alums
Not applicable

Hi @suryareddyn ,

In workflow for example, you could already do g_form.variables.your_mrvs_name. This will return the JSON format for the MRVS.

In Catalog Item, outside the MRVS, is a bit tougher. Though still possible. See this article wrote by mark on this:
Accessing Multi Row Variable Set value outside the Multi Row Variable Set [Catalog Item, Client Side...

Mark my answer correct & Helpful, if Applicable.

Thanks,
Sandeep

Sandeep,

 

Above code returns the format I need . Only issue is returning sys id instead of display value for Multi row variable set reference value.

 

Example :

 

Create MVR with 2 fields (1. Title - Text Variable 

2. User - Reference to User ) Above query will return User value as sys id instead of User Name