Related lists Archives - ServiceNow Guru https://servicenowguru.com/tag/related-lists/ ServiceNow Consulting Scripting Administration Development Tue, 28 May 2024 21:46:43 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.2 https://servicenowguru.com/wp-content/uploads/2024/05/cropped-SNGuru-Icon-32x32.png Related lists Archives - ServiceNow Guru https://servicenowguru.com/tag/related-lists/ 32 32 Enabling Related List Exports in ServiceNow https://servicenowguru.com/system-ui/enabling-related-list-exports-servicenow/ https://servicenowguru.com/system-ui/enabling-related-list-exports-servicenow/#comments Wed, 23 Jul 2014 11:26:45 +0000 https://servicenowguru.wpengine.com/?p=5321 ServiceNow lists contain a lot of very nice capability that allows you to display and access data in a lot of different ways. One of these is the ability to right-click a list header and export the list data in a variety of formats…for external reporting or otherwise. For example, this is particularly useful as

The post Enabling Related List Exports in ServiceNow appeared first on ServiceNow Guru.

]]>
ServiceNow lists contain a lot of very nice capability that allows you to display and access data in a lot of different ways. One of these is the ability to right-click a list header and export the list data in a variety of formats…for external reporting or otherwise. For example, this is particularly useful as an admin when you need to export a list of records to XML and move them to another ServiceNow instance.

While this functionality works great on standard lists, it is completely absent on related lists at the bottom of records displayed in the form view. I can’t count the number of times I wished I could export a filtered related list in the same way you can with a standard list. Fortunately, with a few slight adjustments to some context menu actions in the system, there is a way!

Related List Export

The Solution…

Enabling export capability for related list records in your system can be accomplished by creating some new records in the ‘System UI -> UI Context Menus’ module in your left nav. These records are based on their existing, standard list counterparts so I can show you how to add the ‘Export -XML’ option here and you can add additional options for other formats as needed. Below is the configuration setup for the ‘Export’ menu container and the ‘XML’ export menu option.

‘Export’ UI Context Menu
Table: Global
Menu: List header
Type: Menu
Name: Export
Order: 1045
Active: True
Run onShow Script: True
Condition: ListProperties.isRelatedList() && !ListProperties.isRefList()
onShow Script:

/**
* This client-side script is used to dynamically change the context menu before it is displayed.
* It is called before showing the context menu if the Run onShow script flag is checked.
*
* The following variables are available to the script:
* 'g_menu' the context menu that is about to be shown
* 'g_item' the current context menu item
* 'g_list' the GlideList2 that the script is running against (only valid for List context menus)
* 'g_fieldName' the name of the field that the context menu is running against (only valid for List context menus)
* 'g_fieldLabel' the label of the field that the context menu is running against (only valid for List context menus)
* 'g_sysId' the sys_id of the row or form that the script is running against
*//*Hide the 'Export' context menu for defined related lists
No 'query' parameter available for accurate exports*/
if(g_list.getListName().indexOf('.REL:') > -1){
g_menu.setHidden(g_item);
}

Once you’ve added the ‘Export’ menu option for related lists as shown above, you can add options for the specific types of export options within the menu. Here is the setup for the ‘XML’ export option which you can add by navigating to ‘System UI -> UI Context Menus’ in your left navigation pane. Make sure to put it under the correct Parent menu so that it shows up where you expect!

‘XML’ UI Context Menu
Table: Global
Menu: List header
Type: Action
Name: XML
Parent: Export (Reference the record you just created above)
Order: 300
Active: True
Run onShow Script: False
Condition: gs.hasRole(‘admin’);
Action Script:

/**
* Script executed on the Client for this menu action
*
* The following variables are available to the script:
* 'g_list' the GlideList2 that the script is running against (only valid for List context menus)
* 'g_fieldName' the name of the field that the context menu is running against (only valid for List context menus)
* 'g_sysId' the sys_id of the row or form that the script is running against
* 'rowSysId' is also set to the sys_id of the row to support legacy actions, but g_sysId is preferred
*/
runContextAction();function runContextAction() {
/*Alert if context menu for defined related lists
No 'query' parameter available for accurate exports*/
if(g_list.getListName().indexOf('.REL:') > -1){
alert('Related list exports are not supported for custom defined related lists.');
return false;
}
var relField = g_list.getRelated().split(".");
var sysparm_rows = g_list.grandTotalRows;
var num_rows = parseInt(sysparm_rows);
var sysparm_query = relField[1] + "=" + gel('sys_uniqueValue').value + "^" + g_list.getQuery({orderby: true, fixed: true});var sysparm_view = g_list.view;
if (num_rows < g_export_warn_threshold) {
var dialog = new GwtPollDialog(g_list.tableName, sysparm_query, sysparm_rows, sysparm_view, 'unload_xml');
dialog.execute();
return;
}
var dialog = new GwtExportScheduleDialog(g_list.tableName, sysparm_query, sysparm_rows, sysparm_view, 'unload_xml');
dialog.execute();
}

That should be it! Once you’ve added the UI context menus, as shown above, you’ll be able to export from any related list in the system (with one exception noted below). I think that the most common use case is XML exports for admin users. If you want to enable other exports, you can combine the ‘XML’ setup provided above with the out-of-box Context menu items for CSV, XLS, or PDF.

One Exception!!!

I believe one of the reasons that this functionality isn’t included by default is that there are some related lists in the system (Defined Related Lists) that this won’t work for. The reason that Defined related Lists don’t support exports is that they don’t include a query/filter component that you can get to via the context menu. Because of this, you’ll end up exporting all of the records in the table instead of the filtered related list.

I’ve included checks in the UI Context Menu records above to disable this functionality on defined related lists in your system. While these types of lists are extremely useful they’re also much less common so it isn’t much of an issue. Here are a couple of links to help you understand more about Defined Related Lists in ServiceNow.

https://servicenowguru.com/system-definition/relationships/defined-related-lists/
http://wiki.servicenow.com/index.php?title=Creating_Defined_Related_Lists

The post Enabling Related List Exports in ServiceNow appeared first on ServiceNow Guru.

]]>
https://servicenowguru.com/system-ui/enabling-related-list-exports-servicenow/feed/ 7
Attach an event listener to a related list button https://servicenowguru.com/client-scripts-scripting/attach-event-listener-related-list-button/ https://servicenowguru.com/client-scripts-scripting/attach-event-listener-related-list-button/#comments Thu, 01 Dec 2011 17:37:18 +0000 https://servicenowguru.wpengine.com/?p=4065 Recently a colleague asked me for assistance with a customer request. The requirement was to pop up an information or alert message when a user clicked the ‘Edit’ button on the ‘Affected CIs’ related list. The alert message would give them some information or a warning, allow the user to confirm, and then continue on

The post Attach an event listener to a related list button appeared first on ServiceNow Guru.

]]>
Recently a colleague asked me for assistance with a customer request. The requirement was to pop up an information or alert message when a user clicked the ‘Edit’ button on the ‘Affected CIs’ related list. The alert message would give them some information or a warning, allow the user to confirm, and then continue on to the edit screen. ServiceNow gives you a lot of control over the behavior of some buttons with the configuration of UI actions. There are other buttons in the system, however, that you can’t easily control…including many related list buttons. In order to meet this requirement I created an ‘onLoad’ client script to attach an event listener and onclick function to the necessary button. Read on to see how it’s done!

Related List Event

There are two important pieces of this script that make it work. The first piece is to correctly identify the related list button that you want to respond to. For this script that requires two pieces of information, the button text (‘Edit…’) and the related list identifier.
The related list identifier is the tricky one to come up with. The surest way to find it is to use a dom inspector like firebug to identify it. It does follow a predictable pattern though so you can usually come up with the correct thing by piecing a few bits of information together like this…

.._list

So, the ‘Parent incident’ related list on the incident table is referenced by this ID…
incident.incident.parent_incident_list

And the ‘Affected CIs’ related list on the change request table is referenced by this ID…
change_request.task_ci.task_list

The second important piece of this script is the ‘Event.observe’ line (and the Firefox workaround above it). This is how we attach a new ‘onClick’ event to the button we’ve identified.

The full script looks like this. You can modify it for your own use by changing the buttonText and listID variables to identify the correct button. Then you tell it what to do when the button is clicked by putting your own client-side javascript inside of the ‘customOnClick’ function.

function onLoad() {
//Attach an event listener to the 'Edit' button on the 'Affected CIs' related list
var buttonText = 'Edit...';
var listID = 'change_request.task_ci.task_list';
//Find all buttons for the 'Affected CIs list'
var buttons = $(listID).select('button');
buttons.each(function(elmt) {
if(elmt.innerHTML.indexOf(buttonText) > -1){
//If the button text matches attach an event listener
Event.observe(elmt, 'click', customOnClick);
}
});
}

function customOnClick(){
alert('You clicked me!');
}

The post Attach an event listener to a related list button appeared first on ServiceNow Guru.

]]>
https://servicenowguru.com/client-scripts-scripting/attach-event-listener-related-list-button/feed/ 4
Hiding Related Lists and Embedded Lists https://servicenowguru.com/client-scripts-scripting/hiding-related-lists-embedded-lists/ https://servicenowguru.com/client-scripts-scripting/hiding-related-lists-embedded-lists/#comments Wed, 15 Jun 2011 12:19:22 +0000 https://servicenowguru.wpengine.com/?p=3816 I just saw a question on the ServiceNow forums asking how you could show and hide an embedded related list on a form. I just created some client scripts to accomplish this task so I figured I would share them here. I’ll also review the methods and limitations for showing and hiding standard related lists

The post Hiding Related Lists and Embedded Lists appeared first on ServiceNow Guru.

]]>
I just saw a question on the ServiceNow forums asking how you could show and hide an embedded related list on a form. I just created some client scripts to accomplish this task so I figured I would share them here. I’ll also review the methods and limitations for showing and hiding standard related lists using client scripting.

First of all, if you need to understand the basics about these concepts I’ll refer you to the ServiceNow wiki. Here are a couple of links that should be useful…

Related Lists
Embedded Lists

Hiding and Showing Related Lists

There are some built-in methods for showing and hiding standard related lists. Simply provide the table name of the related list you want to hide (or the ‘Related list’ field value from the list control in some cases). One limitation you’ll want to be aware of is that these methods hide the related list, but they don’t hide the tab if you’re using tabbed forms.

I’m using the ‘Affected CIs’ related list as an example in these scripts. Replace ‘task_ci’ below with the table name of your related list.

Hiding a Related List

g_form.hideRelatedList('task_ci');

Showing a Related List

g_form.showRelatedList('task_ci');

Hiding and Showing Embedded Lists

These scripts were created by me to show and hide form-embedded lists. In this case, it’s easier to target the label of the embedded list so you need to use the list label instead of the table name.

I’m using the ‘Affected CIs’ related list as an example in these scripts. Replace ‘Affected CIs’ below with the label of your embedded list.

Hiding an Embedded List

var list = $$('div[tab_caption="Affected CIs"]')[0];
if(list.hasClassName('embedded')){
   list.hide();
}

Showing an Embedded List

var list = $$('div[tab_caption="Affected CIs"]')[0];
if(list.hasClassName('embedded')){
   list.show();
}

The post Hiding Related Lists and Embedded Lists appeared first on ServiceNow Guru.

]]>
https://servicenowguru.com/client-scripts-scripting/hiding-related-lists-embedded-lists/feed/ 10
Changing the Active Tab Selection in ServiceNow https://servicenowguru.com/client-scripts-scripting/changing-active-tab-selection-servicenowcom/ https://servicenowguru.com/client-scripts-scripting/changing-active-tab-selection-servicenowcom/#comments Wed, 29 Sep 2010 16:41:16 +0000 https://servicenowguru.wpengine.com/?p=2149 ServiceNow has a nice tabbed form interface that allows users to save some screen real estate by collapsing all form sections and related lists and presenting them in a tabbed format. Tabbed forms are explained on the ServiceNow wiki. This post explains how you can use client scripting to change the active tab selection in

The post Changing the Active Tab Selection in ServiceNow appeared first on ServiceNow Guru.

]]>
ServiceNow has a nice tabbed form interface that allows users to save some screen real estate by collapsing all form sections and related lists and presenting them in a tabbed format. Tabbed forms are explained on the ServiceNow wiki.
This post explains how you can use client scripting to change the active tab selection in a form section or a related list. This might come in handy if you want to make a particular tab of information more visible in different situations. Special thanks to Joseph Bennett for doing the real work figuring this out for me :).



These functions are pretty simple. The key is figuring out the ID number of the tab that you want to select. Tabs are numbered in order, left to right, starting at 0.

Change Active Tab Selection for Form Sections

So, if I wanted to select the ‘Change, Backout, and Test Plan’ tab in the image above I would use this line of code in a client script…

g_tabs2Sections.setActive(1);

You can change the active tab selection by name by using a script like this (where ‘Change, Backout, and Test Plan’ is the name of your tab)

var tabName = 'Change, Backout, and Test Plan';
var myTabs = $('tabs2_section').select('span[class="tab_header"]');
var answer = 0;
for (i = 0; i < myTabs.length; i++) { var inner = myTabs[i].innerHTML.replace(/&nbsp;/g, ' '); if(inner.indexOf(tabName) > -1) {
answer = i;
break;
}
}
//Display the selected section
g_tabs2Sections.setActive(answer);

Change Active Tab Selection for Related Lists

If I wanted to select the ‘Affected CIs’ related list tab in the image above I would use this line of code in a client script…

g_tabs2List.setActive(3);

You can change the active tab selection by name by using a script like this (where ‘Affected CIs’ is the name of your tab)

var tabName = 'Affected CIs';
var myTabs = $('tabs2_list').select('span[class="tab_header"]');
var answer = 0;
for (i = 0; i < myTabs.length; i++) { var inner = myTabs[i].innerHTML.replace(/&nbsp;/g, ' '); if(inner.indexOf(tabName) > -1) {
answer = i;
break;
}
}
//Display the selected section
g_tabs2List.setActive(answer);

Want to learn more about controlling the behavior of tabs and form sections in ServiceNow? Check out these other articles for more information!

The post Changing the Active Tab Selection in ServiceNow appeared first on ServiceNow Guru.

]]>
https://servicenowguru.com/client-scripts-scripting/changing-active-tab-selection-servicenowcom/feed/ 15
Creating a Many-to-Many relationship in ServiceNow https://servicenowguru.com/relationships/creating-manytomany-relationship-servicenowcom/ https://servicenowguru.com/relationships/creating-manytomany-relationship-servicenowcom/#comments Fri, 22 Jan 2010 20:09:05 +0000 https://servicenowguru.wpengine.com/?p=682 Service-now allows you to easily create relationships between tables without having to know a whole lot about the underlying table structure. If you’ve ever worked with a reference field then you’ve worked with one of these relationships. A reference field is simply a one-to-many relationship between 2 tables in ServiceNow. The ‘one’ end of the

The post Creating a Many-to-Many relationship in ServiceNow appeared first on ServiceNow Guru.

]]>
Service-now allows you to easily create relationships between tables without having to know a whole lot about the underlying table structure. If you’ve ever worked with a reference field then you’ve worked with one of these relationships. A reference field is simply a one-to-many relationship between 2 tables in ServiceNow. The ‘one’ end of the relationship is displayed as a reference field on the form. The ‘many’ end of the relationship is displayed on the referenced table as a related list.
In some cases, it may also be necessary to create a many-to-many relationship in your instance. Because ServiceNow ships with most of these relationships already defined, it’s rare that you’ll have to create one. If you do have to create a many-to-many relationship, here’s how you could do it.

For this example, we’ll assume that we need a many-to-many relationship between the Incident and the Change request tables. One Incident can be related to many changes (maybe several changes are required as a result of an incident) and one Change request can be related to many Incidents (implementation of a change results in many incidents).

1) Navigate to the m2m table
There are two tables in your ServiceNow instance that contain many-to-many relationship information. The first one (sys_collection) contains the out-of-box relationships and should be used only for reference purposes. The second one (sys_m2m) is where you’ll want to create your own many-to-many definitions. It’s probably a good idea to create your own modules to link to these tables so that you can easily access them if you’re going to be dealing with m2m tables very often. In most cases these tables won’t be used very much so it may be easier just to navigate to them directly in the application filter in your left navigation or by navigating directly by url. The easiest way is just to type ‘sys_m2m.list’ in your left navigation filter.

 

2) Create a new Many-to-Many relationship tableClick the ‘New’ button on the ‘sys_m2m’ table list and create a new record. There are really only 2 fields that you need to fill in since the rest gets automatically calculated for you. All you need to specify are the ‘From table’ and the ‘To table’ values. For this example we’ll specify ‘Incident’ as our ‘from table’ and ‘Change request’ as our ‘to table’. These values could be interchanged with no impact on the end result. You do want to pay attention to the values in the other fields though since they indicate what your table will be named along with how your related lists will be labeled. Although these are all calculated automatically, you can change them if needed.

 

3) Add the related list to your Change request table

Navigate to your Change request form, right-click the form header, and personalize the related lists for the form. Select the ‘Incidents’ list to add to your form. All One-to-Many lists have a ‘->’ separating the relationship. Many-to-Many lists (and custom formatters) have no separator.

 

4) Add the related list to your Incident tableFollow the same procedure for your Incident table. This list should show up as ‘Change requests’, but may show up with the name of your many-to-many table as shown below. If your related list doesn’t show up correctly, submit an incident to ServiceNow support.

 

5) Relate a record to test it outYour related lists will show up at the bottom of each form. Once you have added the related lists to both sides of the relationship, it is easy to relate records to each other using the ‘New’ and ‘Edit’ buttons on the related list.

The post Creating a Many-to-Many relationship in ServiceNow appeared first on ServiceNow Guru.

]]>
https://servicenowguru.com/relationships/creating-manytomany-relationship-servicenowcom/feed/ 14