Skip to Content

Show Related Records

Add a form button to open a popup showing related records from another table.

Form Button Table: incident
#ui-action #form-button #related-records #popup #dialog #client

Script Code

JavaScript
1// Client-side code
2function showRelatedRecords() {
3  var sysId = g_form.getUniqueValue();
4  var recordNumber = g_form.getValue('number');
5
6  // Configuration: Define related table and relationship
7  var relatedTable = 'sc_task';
8  var relationshipField = 'request_item';
9
10  // Build query for related records
11  var query = relationshipField + '=' + sysId;
12
13  // Open popup with related list
14  var dialogClass = GlideModal ? GlideModal : GlideDialogWindow;
15  var dialog = new dialogClass('related_records_dialog');
16  dialog.setTitle('Related Tasks for ' + recordNumber);
17  dialog.setPreference('table', relatedTable);
18  dialog.setPreference('query', query);
19  dialog.setPreference('sysparm_view', 'default');
20
21  // Set dialog size
22  dialog.setWidth(800);
23  dialog.setHeight(600);
24
25  dialog.render();
26
27  // Alternative: Open in new window
28  /*

29}
30  window.open(url, '_blank', 'width=1200,height=800');
31  */
32}

How to Use

1. Create a new UI Action on your table 2. Set Name to "Show Related Records" 3. Check "Form button" checkbox 4. Check "Client" checkbox 5. Set Order: 150 6. Add condition: !current.isNewRecord() 7. Paste the code above 8. Customize relatedTable and relationshipField variables 9. Optionally create a custom UI Page for better formatting 10. Test by opening a record with related records

Explore More Scripts

Browse our complete library of ServiceNow scripts

View All Scripts