|
| 1 | +Widget Name: Dynamic Table and Record Selector |
| 2 | + |
| 3 | +Overview: |
| 4 | +This ServiceNow Service Portal widget allows users to dynamically select any table and then choose a record from that table. The widget automatically determines which field should be shown as the display field for the selected table. It also handles parent table inheritance and provides fallback options for display fields. |
| 5 | + |
| 6 | +Main Features: |
| 7 | + |
| 8 | +Lists all tables from the sys_db_object table. |
| 9 | + |
| 10 | +Automatically finds the correct display field (field with display=true). |
| 11 | + |
| 12 | +Supports parent table lookup if the child table does not have a display field. |
| 13 | + |
| 14 | +Provides fallback checks for fields named "name", "number", or defaults to "sys_id". |
| 15 | + |
| 16 | +Uses ServiceNow REST APIs to fetch metadata and record data dynamically. |
| 17 | + |
| 18 | +Works with the standard sn-record-picker directive in Service Portal. |
| 19 | + |
| 20 | +How It Works: |
| 21 | + |
| 22 | +The first record picker displays all tables from sys_db_object using the label field. |
| 23 | + |
| 24 | +When the user selects a table, the widget fetches the actual table name and label using the sys_id. |
| 25 | + |
| 26 | +The controller calls the getDisplayField function to determine which field should be displayed in the record picker. |
| 27 | + |
| 28 | +It checks sys_dictionary for a field with display=true. |
| 29 | + |
| 30 | +If found, that field is used as the display field. |
| 31 | + |
| 32 | +If not found, it checks if the table has a parent (super_class). |
| 33 | + |
| 34 | +If a parent exists, it recursively checks the parent table. |
| 35 | + |
| 36 | +If there is no parent, it uses fallback checks for "name", then "number", and finally "sys_id". |
| 37 | + |
| 38 | +The second record picker then displays the records from the selected table using the determined display field. |
| 39 | + |
| 40 | +When the user selects a record, its sys_id is stored in the variable TableSysId. |
| 41 | + |
| 42 | +Example Flow: |
| 43 | + |
| 44 | +Select “Incident” from the table picker. |
| 45 | + |
| 46 | +The widget detects that the display field is “number”. |
| 47 | + |
| 48 | +The record picker lists incident numbers. |
| 49 | + |
| 50 | +When a record is selected, its sys_id is saved for further use. |
| 51 | + |
| 52 | +Technologies Used: |
| 53 | + |
| 54 | +ServiceNow Service Portal |
| 55 | + |
| 56 | +AngularJS (spUtil, spModal) |
| 57 | + |
| 58 | +ServiceNow REST API: |
| 59 | + |
| 60 | +/api/now/table/sys_db_object |
| 61 | + |
| 62 | +/api/now/table/sys_dictionary |
| 63 | + |
| 64 | +Use Cases: |
| 65 | + |
| 66 | +Creating dynamic reference selectors for any table. |
| 67 | + |
| 68 | +Building tools that link or map data between tables. |
| 69 | + |
| 70 | +CMDB record selection where tables may have inheritance. |
| 71 | + |
| 72 | +Generic admin utilities or catalog forms needing flexible input. |
| 73 | + |
| 74 | +File Components: |
| 75 | + |
| 76 | +HTML Template: Contains two sn-record-picker elements for selecting table and record. |
| 77 | + |
| 78 | +Client Controller (JS): Handles field change events, fetches table metadata, determines display fields, and manages recursion logic. |
0 commit comments