Fetch values instead of sys ids : Question & Answer table

SM16
Tera Expert

Hi Team,


I have below script to extract variables for a custom table from question_answer table. But for reference fields, value being return is SysId. How can I get the display value?

 

var obj = {};
var gr = new GlideRecord('question_answer');
gr.addQuery('table_sys_id',current.sys_id);
gr.addQuery('value','!=','');
gr.query();
while(gr.next()){
obj[gr.question.name.toString()] = gr.value.toString();
}
3 REPLIES 3

Community Alums
Not applicable

Hi @SM16 ,

Reference fields contain the sys_id value of the record in the referenced table.  There is a 'getDisplayValue()' method in the GlideRecord class.

Sample code :

// get an incident that refers to a printer
var gr = new GlideRecord('incident');
gr.addQuery('u_serial_number', '!=','' );
gr.query();
gr.next();
var sys_id = gr.sys_id;

// print the raw value of the reference column u_serial_number
gs.info( "1. " + gr.u_serial_number ); // "1. 004f47b1db90e014023fe66505961913"
gs.info( "1a. " + gr.u_serial_number.toString() ); // "1a. 004f47b1db90e014023fe66505961913"

// print the display value of the same column
gs.info( "2. " + gr.getDisplayValue('u_serial_number') ); // "2. Lexmark CX860dte"

 

 

SM16
Tera Expert

@Ranjit Nimbalka @Community Alums  - You guys are responding to the question even before reading it. 

 

Question_aswer table stores the values of a variable associated with a record and there is a value field in the table that just holds the sys id of a referenced variable. Using getDisplayValue() doesn't help here at all.

 

I have to get the table name of the variable and query the table to get the corresponding value.

Gopal Allu
Tera Expert

Hi @SM16 ,

 

Try this approach for your sysid value you are getting in value field for getting the name of that record.

function findAnywhere(sysIDToFind, getHTML) {
    if (getHTML !== true && getHTML !== 'true') {
        getHTML = false;
    }
    var grCheck;
    var tableName;
    var url = gs.getProperty('glide.servlet.uri');
    var grTable = new GlideRecord('sys_db_object');
    //Make sure we're not looking at a ts (text search) table.
    grTable.addEncodedQuery('sys_update_nameISNOTEMPTY^nameISNOTEMPTY^nameNOT LIKEts_');
    grTable.query();
    while (grTable.next()) {
        tableName = grTable.getValue('name');
        grCheck = new GlideRecord(tableName);
        if (grCheck.get(sysIDToFind)) {
            // write code for sending the display value of name field for that record to your array.
        }
    }
}

Take this article for reference of this code: Locate any record in any table, by sys_id in ServiceNow - ServiceNow Developer Pro-Tips (snprotips.c...

 

If it is helpful, please mark it as helpful/correct.

 

Thanks and Regards,

Allu Gopal.