Get the internal_type of parent table's column

Nisar3
Giga Guru

I've two fields in a custom table.

 

Field A -> type = table_name

Field B -> type = field_name (which is dependent on Field A)

 

This will list out all columns of child as well as parent table which is good. However, whenever a new record is created into this table, I need to know the internal_name of the Field B (it works fine if Field B is part of child table but runs into problem when it's from parent)

 

For example, if I select "Incident" in Field A and "Additional Comments" (which is present in parent Task and not incident) in Field B, what would I write in the "insert" BR to know the internal_name of "comments" which return "Journal"

 

Because if I write the following in the BR

var dictionaryEntry = new GlideRecord('sys_dictionary');
dictionaryEntry.addQuery('name', current.table);
dictionaryEntry.addQuery('element', current.column);
dictionaryEntry.query();
if (dictionaryEntry.next()) {
        gs.print(dictionaryEntry.internal_type.label);
 }

it won't go into the "if" block because current.table (incident) and current.column (comments) is not valid.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

@Nisar3 

you will have to check if that field is extended one from parent table

try this

// Function to get the internal name of the field
function getInternalName(tableName, fieldName) {
    var dictionaryEntry = new GlideRecord('sys_dictionary');
    dictionaryEntry.addQuery('name', tableName);
    dictionaryEntry.addQuery('element', fieldName);
    dictionaryEntry.query();

    if (dictionaryEntry.next()) {
        return dictionaryEntry.internal_type.label;
    } else {
        // Check if the table has a parent table
        var tableGR = new GlideRecord('sys_db_object');
        tableGR.addQuery('name', tableName);
        tableGR.query();

        if (tableGR.next() && tableGR.super_class) {
            // Recursively check the parent table
            return getInternalName(tableGR.super_class.name, fieldName);
        } else {
            return null;
        }
    }
}

// Get the internal name of the field
var internalName = getInternalName('incident', 'comments');
if (internalName) {
    gs.info('Internal Name: ' + internalName);
} else {
    gs.info('Field not found in the table hierarchy.');
}

Output:

AnkurBawiskar_0-1746017893907.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron

@Nisar3 

you will have to check if that field is extended one from parent table

try this

// Function to get the internal name of the field
function getInternalName(tableName, fieldName) {
    var dictionaryEntry = new GlideRecord('sys_dictionary');
    dictionaryEntry.addQuery('name', tableName);
    dictionaryEntry.addQuery('element', fieldName);
    dictionaryEntry.query();

    if (dictionaryEntry.next()) {
        return dictionaryEntry.internal_type.label;
    } else {
        // Check if the table has a parent table
        var tableGR = new GlideRecord('sys_db_object');
        tableGR.addQuery('name', tableName);
        tableGR.query();

        if (tableGR.next() && tableGR.super_class) {
            // Recursively check the parent table
            return getInternalName(tableGR.super_class.name, fieldName);
        } else {
            return null;
        }
    }
}

// Get the internal name of the field
var internalName = getInternalName('incident', 'comments');
if (internalName) {
    gs.info('Internal Name: ' + internalName);
} else {
    gs.info('Field not found in the table hierarchy.');
}

Output:

AnkurBawiskar_0-1746017893907.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

@Nisar3 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

I like that it is recurssive in nature. We could have multi level inheritance for some tables.