Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Print Reference Value in Description

Michael94
Tera Contributor

I am creating a business rule to print the full name of user from a reference type variable into the description field. Am I missing something?

 

 

 

// Variable table
var gr_questions = new GlideRecord('question_answer');
gr_questions.addQuery('table_sys_id', current.sys_id);
gr_questions.query();
while (gr_questions.next()) {
// Variable question label
var refq = gr_questions.getDisplayValue('question');
// Value stored in variable
var refa = gr_questions.getDisplayValue('value');
if(refa !="") {
var userName = new GlideRecord('sys_user');
userName.addQuery('sys_id', refa);
userName.query();
while(userName.next())
{
// print variable question and answer in description
desc.push(refq+" "+refa.getDisplayValue());
}
}
}

 

 

1 ACCEPTED SOLUTION

AshishKM
Kilo Patron
Kilo Patron

Hi @Michael94 ,

The value field is String type ( not the reference ).

try the below updated code.

var gr_questions = new GlideRecord('question_answer');
gr_questions.addQuery('table_sys_id', current.sys_id);
gr_questions.query();
while (gr_questions.next()) {
    var refq = gr_questions.getDisplayValue('question');
    var refa = gr_questions.getDisplayValue('value');
    if (refa != "") {
        var userName = new GlideRecord('sys_user');
        userName.addQuery('sys_id', refa);
        userName.query();
        //while(userName.next()) {
        //use if because there will be only one matching record
        if (userName.next()) {
            //desc.push(refq+" "+refa.getDisplayValue()); // refa is string value
            desc.push(refq + " " + userName.name); // or userName.getDisplayValue() 
        } // while closed
    } // if closed
} // while closed

 

or same result using get method for user name.

var gr_questions = new GlideRecord('question_answer');
gr_questions.addQuery('table_sys_id', current.sys_id);
gr_questions.query();
while (gr_questions.next()) {
    var refq = gr_questions.getDisplayValue('question');
    var refa = gr_questions.getDisplayValue('value');
    if (refa != "") {
        var userName = new GlideRecord('sys_user');
        userName.get(refa);   //use get method 
        desc.push(refq + " " + userName.name); // or desc.push(refq + " " + userName.getDisplayValue()); 
    } // if closed
} // while closed

 

-Thanks,

AshishKM

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

View solution in original post

1 REPLY 1

AshishKM
Kilo Patron
Kilo Patron

Hi @Michael94 ,

The value field is String type ( not the reference ).

try the below updated code.

var gr_questions = new GlideRecord('question_answer');
gr_questions.addQuery('table_sys_id', current.sys_id);
gr_questions.query();
while (gr_questions.next()) {
    var refq = gr_questions.getDisplayValue('question');
    var refa = gr_questions.getDisplayValue('value');
    if (refa != "") {
        var userName = new GlideRecord('sys_user');
        userName.addQuery('sys_id', refa);
        userName.query();
        //while(userName.next()) {
        //use if because there will be only one matching record
        if (userName.next()) {
            //desc.push(refq+" "+refa.getDisplayValue()); // refa is string value
            desc.push(refq + " " + userName.name); // or userName.getDisplayValue() 
        } // while closed
    } // if closed
} // while closed

 

or same result using get method for user name.

var gr_questions = new GlideRecord('question_answer');
gr_questions.addQuery('table_sys_id', current.sys_id);
gr_questions.query();
while (gr_questions.next()) {
    var refq = gr_questions.getDisplayValue('question');
    var refa = gr_questions.getDisplayValue('value');
    if (refa != "") {
        var userName = new GlideRecord('sys_user');
        userName.get(refa);   //use get method 
        desc.push(refq + " " + userName.name); // or desc.push(refq + " " + userName.getDisplayValue()); 
    } // if closed
} // while closed

 

-Thanks,

AshishKM

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution