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

Community Alums
Not applicable

Thanks Geoffrey,


i have written same code which you have sent.but in no display field is specified for different tables i want to get the field name.


Thanks,


So you need to know if it is defaulting to a number or name field?


Community Alums
Not applicable

yes Geoffrey,



Thanks


It's actually fairly complex.   As Paul pointed out, the display field can be in any parent table. And there are also Dictionary Overrides that can change the display value.


Community Alums
Not applicable

Thanks Geoffrey,



i have written same code which you have sent.but in no display field is


specified for different tables i want to get the field name.



Thank



On Thu, Sep 8, 2016 at 3:08 PM, geoffrey.sage <