Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Getting all the tables based on field values

Community Alums
Not applicable

Hi,

I know the name of my company ABC. It is a field value. Is there a way I can get all the tables with this field value? Kindly help.

Regards

Suman P.

4 REPLIES 4

AnveshKumar M
Tera Sage
Tera Sage

Hi @Community Alums ,

 

Yes you can write a Background Script which queries sys_dictionary table for a field type of Reference (I assume your company field is referring to core_company table) and refering the core_company table.

 

If you find any results, query the table to which the dictionary entry belongs to and where the field value belongs to sys_id of your company record in core_company.

 

 

If you are asking for String type, you can do the same but it will take longtime as the dictionary contains many string fields.

Thanks,
Anvesh

Community Alums
Not applicable

Hi @AnveshKumar M ,

Can you please help me with the background script?

Regards

Suman P.

@Community Alums 

If you are looking for,

Reference Fields:

var dictGr = new GlideRecord('sys_dictionary');
dictGr.addQuery('internal_type', 'reference');
dictGr.addQuery('reference', 'core_company');
dictGr.addQuery('active', true);
dictGr.query();

while(dictGr._next()){
    var tableName = dictGr.getValue('name') + '';
    var fieldName = dictGr.getValue('element') + '';
    var tblGr = new GlideRecord(tableName);
    tblGr.addQuery(fieldName, 'YOUR_CPMPANY_SYS_ID'); //Change it to your company record sys_id in core_company table
    tblGr.query();

    while(tblGr._next()){
        gs.print('Table: ' + tableName + "\t Field Name: " + fieldName + "\t Record Sys_ID: " + tblGr.getUniqueValue());
    }

}

 

String Fields:

Note: this might run for longer duration

 

var dictGr = new GlideRecord('sys_dictionary');
dictGr.addQuery('internal_type', 'string');
dictGr.addQuery('active', true);
dictGr.query();

while(dictGr._next()){
    var tableName = dictGr.getValue('name') + '';
    var fieldName = dictGr.getValue('element') + '';
    var tblGr = new GlideRecord(tableName);
    tblGr.addQuery(fieldName, 'YOUR_CPMPANY_NAME'); //Change it to your company name
    tblGr.query();

    while(tblGr._next()){
        gs.print('Table: ' + tableName + "\t Field Name: " + fieldName + "\t Record Sys_ID: " + tblGr.getUniqueValue());
    }

}

 

Thanks,
Anvesh

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

What's your business requirement here?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader