How to know the field name which is set display as true in table?

Community Alums
Not applicable

I have a table X, in that i have three fields as a,b,c.

if i reference this table i have set 'a' field as display to true.

if i give the table name i want to get field name of which i was set display to true.

can any one help me in this issue.

1 ACCEPTED SOLUTION

Geoffrey2
ServiceNow Employee
ServiceNow Employee

Try this code.   It does a complete search. It worked on all the tables I tested.



function getDisplayField(table) {


      var i, currentTable, deo, dict;



      var hierarchy = String(new TableUtils(table).getHierarchy());


      hierarchy = hierarchy.substring(1,hierarchy.length-1).split(', '); // turn List into Array



      for (i = 0; i < hierarchy.length; i++) {


              currentTable = hierarchy[i];



              // Check for Dictionary Entry Overrides first


              deo = new GlideRecord('sys_dictionary_override');


              deo.addQuery('name', currentTable);


              deo.addQuery('display_override', true);


              deo.query();


              if (deo.next())


                      return String(deo.element);



              // Check the Dictionary next


              dict = new GlideRecord('sys_dictionary');


              dict.addQuery('name', currentTable);


              dict.addQuery('display', true);


              dict.query();


              if (dict.next())


                      return String(dict.element);


      }



      // No display values have been defined.   Look for a default field


      var defaults = ['number','u_number','name','u_name'];


      for (i = 0; i < defaults.length; i++) {


              dict = new GlideRecord('sys_dictionary');


              dict.addQuery('name', 'IN', hierarchy);


              dict.addQuery('element', defaults[i]);


              dict.query();


              if (dict.next())


                      return String(defaults[i]);


      }



      // No display value


      return 'sys_id';


}


var displayField = getDisplayField('sys_user');


gs.log('displayField: ' + displayField);


View solution in original post

20 REPLIES 20

BALAJI40
Mega Sage

Hi Prasad,



  reference fields always displays the field which displays true only. If you did not give any value true means it will show sys id.


So based on script you need which field is true? or any other scenario you are asking?


Community Alums
Not applicable

Thanks Balaji for your quick response, Actually for some tables (for example user and department tables) there is no field set as display true.


in such case script returning no value.



Thanks,


Community Alums
Not applicable

Thanks Balaji for your quick response, Actually for some tables (for


example user and department tables) there is no field set as display true.



in such case script returning no value.





Thanks,



On Thu, Sep 8, 2016 at 3:01 PM, balajireddy <


Geoffrey2
ServiceNow Employee
ServiceNow Employee

I'm not sure if there is a specific function for that. But you could always do a GlideRecord on the Dictionary.



var displayField;


var dict = new GlideRecord('sys_dictionary');


dict.addQuery('name', 'table_X');


dict.addQuery('display', true);


dict.query();


if (dict.next()) {


        displayField = String(dict.element); // field a


}



You might also need to consider that if no display field is specified, it will automatically default to number, u_number, name or u_name if they exist.