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 paul,


but sys_user is solo class right? there is no base table for sys_user.


in that case field name not getting.



Thanks,


arnabwa
Giga Guru

Hi Rajendra,



Go to the concerned table >> Open >> See the "Table Columns" area >> You will see a column named "Display" >> Here you will find a particular row being kept as true. This is the row that is available when we reference it from a form. If you do not see the Display field, Click on the gear icon and select it from the Slush bucket.



Hope you got it.



Thanks,


Arnab


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);


Thanks - great script, but... I ran it for 'cmdb_ci_server' and it returned:

*** Script: displayField: u_number

I modified the script a bit adding the table the attribute was found in and it returned:

*** Script: displayField: u_number, defined in table: cmdb_ci_tape_server

So I believe it should use TableUtils(table).getTables() and not TableUtils(table).getHierarchy() - this way only the "parent" path is checked. With this modification the function returned (correct imho)

*** Script: displayField: name, defined in table: cmdb

So finally:

// from: https://community.servicenow.com/community?id=community_question&sys_id=e18e4b21dbdcdbc01dcaf3231f9619f8
// credits to: Geoffrey (https://community.servicenow.com/community?id=community_user_profile&user=43521e29dbd81fc09c9ffb651f961955)


function getDisplayField(table) {
	// MM: added table to results so returning object
	var result = {}
	
	var i, currentTable, deo, dict;
	// MM: getting hierarchy includes child tables - which is wrong! should be getTables (only parents)
	//var hierarchy = String(new TableUtils(table).getHierarchy());
	var hierarchy = String(new TableUtils(table).getTables());
	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())
		{
			// MM: added table to results so returning object
			result.table = currentTable;
			result.field = String(deo.element);
			return result;
		}
		// Check the Dictionary next
		dict = new GlideRecord('sys_dictionary');
		dict.addQuery('name', currentTable);
		dict.addQuery('display', true); 
		dict.query();
		if (dict.next())
		{
			// MM: added table to results so returning object
			result.table = currentTable;
			result.field = String(dict.element);
			return result;
		}			
	}
	// 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())
		{
			// MM: added table to results so returning object
			result.table = currentTable;
			result.field = String(defaults[i]);
			return result;
		}				
	}
	// No display value
	// MM: added table to results so returning object
	result.table = currentTable;
	result.field = 'sys_id';	
	return result;
}

var displayField = getDisplayField('sys_user');

gs.log('displayField: ' + displayField.field + ", defined in table: "+displayField.table);

Kieran Anson
Kilo Patron

For those that find this in the future from Google searches. I've created an article that provides a condensed version now that there is a isDisplay() function.

Easily Retrieve a Table Display Field Name