Skip to content

Commit 84baa00

Browse files
docs: add example for displaying field inheritance in ServiceNow tables (#2199)
1 parent 11d9b79 commit 84baa00

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Display Base Table for Each Field
2+
3+
This code snippet demonstrates how to identify the base table where each field originates from in ServiceNow's table inheritance hierarchy.
4+
5+
## Functionality
6+
7+
The script uses `GlideRecordUtil` to retrieve all parent tables for a given table, then iterates through each field in a record to display which base table the field was defined in using the `getBaseTableName()` method from the GlideElement API.
8+
9+
## Use Case
10+
11+
This is particularly useful when working with extended tables to understand:
12+
- Which fields are inherited from parent tables
13+
- Which fields are defined locally on the current table
14+
- The complete table inheritance structure
15+
16+
## Example Output
17+
18+
For a table like `db_image`, the output shows each field name alongside its originating table:
19+
```
20+
Fields Base table
21+
sys_id sys_metadata
22+
sys_created_on sys_metadata
23+
image db_image
24+
name db_image
25+
...
26+
```
27+
28+
## Related Methods
29+
30+
- `getBaseTableName()` - Returns the name of the table where the field was originally defined
31+
- `GlideRecordUtil.getTables()` - Returns parent tables in the inheritance hierarchy
32+
- `GlideRecordUtil.getFields()` - Returns an array of all field names for a GlideRecord
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var tableName = 'db_image';
2+
3+
var gru = new GlideRecordUtil();
4+
5+
var parentTables = gru.getTables(tableName);
6+
gs.print("Parent tables: " + parentTables);
7+
8+
var gr = new GlideRecord(tableName);
9+
gr.setlimit(1);
10+
gr.query();
11+
12+
if(gr.next()){
13+
var fieldNames = gru.getFields(gr);
14+
gs.print('Fields\t\tBase table');
15+
for (var i = 0; i < fieldNames.length; i++) {
16+
gs.print(fieldNames[i] + "\t\t" + gr[fieldNames[i]].getBaseTableName());
17+
}
18+
}
19+

0 commit comments

Comments
 (0)