We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

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

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

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

Rachamalla Srav
ServiceNow Employee

You can use getDisplayName() method available on GlideRecord

var gr = new GlideRecord("table_name");
gs.log(gr.getDisplayName());

This retrieves the name of the display column from the table.