logo

NJP

My collected list of useful UI Actions

Import · May 01, 2024 · article

tool-gcb78f6529_1280.jpg

If you want to get notified when new UI Actions are added by me, you can subscribe to this page!

Over time, every developer or system administrator builds little helpers that make working with ServiceNow more convenient and secure. This is also true for me, and in this article I present a few UI Actions that have been collecting in my toolbox.

What are UI Actions?

UI Actions are custom actions users can perform on records in the ServiceNow interface. They are typically associated with forms and lists and appear as buttons or context menu options. UI Actions allow users to initiate specific actions, such as creating records, updating fields, running scripts, or navigating to related pages, directly from the ServiceNow interface. They can be configured to execute client-side or server-side scripts to perform desired operations. UI Actions help users work faster and more efficiently by giving them quick access to common ServiceNow functions.

Please refer to the ServiceNow documentation pages for more information about UI Actions.

Open Scan Finding related Record in List View

In most of the cases, a finding record of an Instance Scan (table scan_finding) will reference the identified source record, and you can open that source record as usual. The problem here are source records from the sys_attachment table. These cannot be opened in the form view. Instead, ServiceNow starts a download of the underlying file. However, this takes away the possibility of examining the sys_attachment record in more detail in order to understand the cause of the finding.

Therefore, the following UI Action (related link in form view and context option in list view) will open the referenced record in the list view, thus allowing to add the required columns for more information.

UI Action

Table scan_finding
Form link true
List Context Menu true
Client true
Onclick openSourceRecordInListView()
Condition gs.hasRole('scan_user')
Script function openSourceRecordInListView() { var _grScanFinding = new GlideRecord('scan_finding'); _grScanFinding.addQuery('sys_id', rowSysId \

Open Attachment-related Record from List View

Basically, an attachment record at table sys_attachment is associated with a related record. For example, an Excel file attached to a data source. That relationship is designed as a Document ID reference at table sys_attachment. And because of records in the sys_attachment table do not have a form view, it is impossible to open the referenced record from the list view. Instead, you have to open the related target table manually and lookup the target record via its Sys ID which is a pretty bad user experience. Therefore, I built a list action for the context menu, allowing me to open the target record with a mouse click.

UI Action

Table sys_attachment
List Context Menu true
Client true
Onclick openRelatedRecord()
Condition !current.table_name.nil() && !current.table_sys_id.nil() && !current.table_name.startsWith('invisible.') && current.table_name != 'sys_attachment'
Script function openRelatedRecord() { var _grRecord = new GlideRecord('sys_attachment'); _grRecord.addQuery('sys_id', rowSysId); _grRecord.query(); if (_grRecord.next()) { var _strTableName = _grRecord.table_name.toString(); var _strRecordSysID = _grRecord.table_sys_id.toString(); if (_strTableName.startsWith('ZZ_YY')) { _strTableName = _strTableName.substring(5); } g_navigation.openPopup('/' + _strTableName + '.do?sys_id=' + _strRecordSysID); } }

Open Metadata Snapshot-related Records

In ServiceNow, we can add pure data records to an application via action "Create Application Files". These records are stored at table sys_metadata_link and thus associated to a certain application.

However, just as with the sys_attachment table, ServiceNow has forgotten to provide a UI Action that allows the original data record to be opened directly in the form as well as list view.

UI Action

Table sys_metadata_link
Form link true
List Context Menu true
Client true
Onclick openRelatedRecord()
Condition new GlideRecord(current.tablename.toString()).get(current.documentkey.toString())
Script function openRelatedRecord() { var _grRecord = new GlideRecord('sys_metadata_link'); _grRecord.addQuery('sys_id', rowSysId \

Create Application Files on Form View

In a list view of a non-artifact table (table NOT inheriting from sys_metadata) you have the list action "Create Application Files". This is always super helpful if you want to add pure data records to an application (for example technical users for integrations) for easier transfer to the next instance and make sure those records definitely exist in the target instance.

Unfortunately, that action is not available in the form view. However, with a slight modification of the original UI Action you also make it available for the form view.

UI Action

Table global
Form context menu true
Client true
Onclick popAddMetadataDialog();
Condition !current.isMetadata() && current.canCreate() && !SNC.MetadataLinkUtil.isTableMetadataLinkExempt(current.getTableName())
Script function popAddMetadataDialog() { var dialog = new GlideModal('add_selected_metadata_link_dialog'); dialog.setTitle( new GwtMessage().getMessage('Create Application File from Record') ); dialog.setPreference('current_table', g_form.getTableName()); dialog.setPreference('sys_id_list', g_form.getUniqueValue()); dialog.render(); }

Open in Instance

How often have you opened any configuration record in one instance and then the same record on a different instance for comparison reasons? Or the same for list views? A button that allows us to open the current URL on another instance would therefore be extremely helpful. And this is exactly what the following UI action offers.

MaikSkoddow_0-1717394024557.png

UI Action

Table global
Form button true
List banner button true
Client true
Onclick openInInstance()
Condition gs.hasRole("admin") && !RP.isRelatedList()
Script function openInInstance() { var _gm = new GlideModal(); var _strCurrentInstanceHost = top.location.host; var _objInstanceProperties = { 'DEV': { color: '#b32400', host : 'mycompany-dev.servicenow.com' }, 'TEST': { color: '#b38300', host : 'mycompany-test.servicenow.com' }, 'PROD': { color: '#302f4b', host : 'mycompany-prod.servicenow.com' } }; var _arrHtml = ['
']; for (var _strInstanceName in _objInstanceProperties) { var _strInstanceHost = _objInstanceProperties[_strInstanceName]['host']; var _strInstanceColor = _objInstanceProperties[_strInstanceName]['color']; var _strInstanceUrl = g_navigation.getURL().replace(_strCurrentInstanceHost, _strInstanceHost); if (_strInstanceHost !== _strCurrentInstanceHost) { _arrHtml.push( '' + _strInstanceName + '' ); } } _arrHtml.push('
'); _gm.setTitle('Open in Instance') _gm.renderWithContent(_arrHtml.join('')); }
View original source

https://www.servicenow.com/community/developer-articles/my-collected-list-of-useful-ui-actions/ta-p/2911821