Kieran Anson
Kilo Patron

With the UK being in lockdown I find myself digging more into how the Now platform works and the parts customers, developers and administrators don't see and is often portrayed as the magic behind the curtain.

An interesting question posted on the community peaked by curiosity was a method to retrieve the name of the field that is the display value of the table without needing to do extensive nested GlideRecord queries.

GlideRecord does provide getDisplayValue() which gives you the display value, but not the name of the field being used as the display field for the table.

Digging into the GlideElementDescriptor API I was able to find a function of isDIsplay() which isn't documented (boo!) that returned a boolean value. Wrapping this up into a function with the getElements() function allowed for a recursive lookup to identify the display value for the table.

This function takes into account:

  • Ability to pass a gliderecord or table name as a string
  • Recursive lookup as the display value can be inherited from a parent table
  • Defaulting to "name" or "u_name" if the above doesn't return a true response.
  • Defaulting to "sys_created_on" if the table doesn't contain a name field.

Thank you to @Allen A for linking to this docs page that allowed this function to be a bit more comprehensive.

/**
 
* Description: Returns the field name used as the display field for the table
 
* Parameters: GlideRecord or table name
 
* Returns: string: field name
*/
 
function getTableDisplayElement(tableGR){
	
	//We can accept either a GlideRecord or a string but we want a gliderecord
	tableGR = (tableGR instanceof GlideRecord) ? tableGR : new GlideRecord(tableGR);

	//Get all the fields for the table
	var elements = tableGR.getElements();
	//Scoped vs Global calls
	//Global gives us a java object
	//Scoped a js array
	var jsArr = Array.isArray(elements);

	var elementsLength = jsArr ? elements.length : elements.size();
	for (var i = 0; i < elementsLength; i++) {
		var element = jsArr ? elements[i] : elements.get(i);
		//if find the display field, return it and finish
		if(element.getED().isDisplay()){
			return element.getName();
		}
	}
	
	//We didn't find a display value on the current table
	//Get the parent table to see if we're inheriting the display value
	var parentTable = j2js(new TableUtils(tableGR.sys_class_name).getTables())[1];
	if(parentTable){
		//Call ourselves again
		getTableDisplayElement(parentTable);
	} else if (tableGR.isValidField('name')){
		return "name";
	} else if (tableGR.isValidField('u_name')){
		return "u_name";
	} else {
		return "sys_created_on";
	}
}

 

Hope this help! The code can be refactored into a more condensed script but for readability, I've left it expanded.

Comments
DrewW
Mega Sage
Mega Sage

Is there a reason that you cannot just use getDisplayName?

var catItem = new GlideRecord("sc_cat_item");
gs.print(catItem.getDisplayName());

Unless I am totally not understanding what you are trying to do.

Kieran Anson
Kilo Patron

👀 I'm sure there is and i'm really hoping it's not a gross oversight!

Marek Meres
Tera Expert

Thanks Drew for this!

Unfortunately this is not documented (I found it only for GlideUser) hence I also ended up with a custom script (https://community.servicenow.com/community?id=community_question&sys_id=e18e4b21dbdcdbc01dcaf3231f9619f8).

But actually I realized I could not just use getDisplayName() as I need the length of the display field. So I need to search dictionary and for many tables (change_request for instance) the attribute is inherited. Then I need the name of the table it is defined in anyway... unless there is another undocumented method giving me that? 😉

Best regards,

Marek

Kieran Anson
Kilo Patron

Hi @Marek Meres if I'm understanding correctly, you could use the following.

var chgGR = new GlideRecord("change_request");
var displayField = chgGR.getDisplayName();
gs.print("DisplayField is " + displayField);
var element = chgGR.getElement(displayField);
var descriptor = element.getED();
gs.print("Max length " + descriptor.getLength());
gs.print("From table " + element.getBaseTableName());

find_real_file.png

Marek Meres
Tera Expert

Thanks @Kieran Anson ! This is really great and much shorter than the previous version. Many thanks!

Version history
Last update:
‎03-28-2021 09:56 AM
Updated by: