logo

NJP

Easily Retrieve a Table Display Field Name

Import · Mar 28, 2021 · article

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.

View original source

https://www.servicenow.com/community/developer-articles/easily-retrieve-a-table-display-field-name/ta-p/2315548