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

That won't always work, as some tables do not have any fields with display set to true, i.e. extended tables.



gs.print(getDisplayFieldForTable('incident') );



function getDisplayFieldForTable(tableName){


  var displayField = null;


  var gth = new TableUtils(tableName);


  var baseTableName = gth.getAbsoluteBase(tableName);



  if (!gs.nil(baseTableName) ) {


  displayField = checkDictionaryOverrideForDisplayField(tableName, baseTableName);


  }



  if (gs.nil(displayField) ) {


  displayField = checkDictionaryForDisplayField(baseTableName);


  }



  if (gs.nil(displayField)) {


  getDisplayFieldForTable(baseTableName);


  }



  return displayField;


}




function checkDictionaryOverrideForDisplayField(tableName, baseTableName) {


  var displayField = null;


  var grDictionaryOverride = new GlideRecord('sys_dictionary_override');


  grDictionaryOverride.addQuery('base_table',baseTableName);


  grDictionaryOverride.addQuery('name',tableName); //Table field


  grDictionaryOverride.addQuery('display_override','true');


  grDictionaryOverride.query();


  if (grDictionaryOverride.next() ) {


  displayField = grDictionaryEntry.getValue('element');


  }


  return displayField;


}




function checkDictionaryForDisplayField(tableName) {


  var displayField = null;


  var grDictionaryEntry = new GlideRecord('sys_dictionary');


  grDictionaryEntry.addQuery('name',tableName); //Table field


  grDictionaryEntry.addQuery('display','true');


  grDictionaryEntry.query();


  if (grDictionaryEntry.next()){


  displayField = grDictionaryEntry.getValue('element');


  }


  return displayField;


}


number



Updated to account for dictionary overrides



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Ahh good point.   I hadn't considered extended tables.


You might also need to check each table in the hierarchy and also check for Dictionary Overrides.


Community Alums
Not applicable

Thanks Paul,


your code is not working for sys_user table.



Thanks,


Community Alums
Not applicable

Thanks Paul,



your code is not working for sys_user table.





Thanks,



On Thu, Sep 8, 2016 at 3:18 PM, paablo <community-no-reply@servicenow.com>


I have updated my code to account for Dictionary Overrides.



Please mark the solution as correct if it helped/solved your issue



Thanks



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022