
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2016 02:17 AM
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.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2016 03:34 AM
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);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2016 03:42 AM
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,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2016 02:56 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2016 03:34 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2021 05:28 AM
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);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2021 09:58 AM
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.