Relationships Archives - ServiceNow Guru https://servicenowguru.com/category/relationships/ 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 Relationships Archives - ServiceNow Guru https://servicenowguru.com/category/relationships/ 32 32 Defined Related Lists https://servicenowguru.com/relationships/defined-related-lists/ https://servicenowguru.com/relationships/defined-related-lists/#comments Fri, 30 Jul 2010 11:10:35 +0000 https://servicenowguru.wpengine.com/?p=1862 Defined Related Lists can be a very simple and useful tool to provide users with information in a related list directly on a form (even if that information is not directly associated with the record being viewed). The Service-now wiki contains documentation on this topic so I won’t cover that here. The point of this

The post Defined Related Lists appeared first on ServiceNow Guru.

]]>
Defined Related Lists can be a very simple and useful tool to provide users with information in a related list directly on a form (even if that information is not directly associated with the record being viewed). The Service-now wiki contains documentation on this topic so I won’t cover that here. The point of this article is to point out something that often gets overlooked when working with Defined Related Lists and to share a few Defined Related Lists that I’ve used in the past.

One of the familiar defined related lists is the ‘Incidents by Same Caller’ list that you can display at the bottom of your incident form. The name says it all, but it looks something like this…

If you navigate to ‘System Definition -> Relationships’ and open up the ‘Incidents by Same Caller’ relationship record you’ll see that it uses the following query to produce the resultant related list…

current.addQuery('caller_id', parent.caller_id);

But what happens when we set the Caller field with an empty value?

The image above shows that you’ll get exactly what you’re supposed to get, but probably not what you want. Depending on your setup, there might not be a whole lot of value in showing a list of incidents with no caller…at least at the bottom of your incident form. The solution to this is to filter out any records where the field you are querying is empty. In this case, we want to filter out any records where the Caller field is empty like this…

current.addQuery('caller_id', parent.caller_id);
current.addNotNullQuery('caller_id');

Adding the ‘addNotNullQuery’ line gives us the result we would expect at the bottom of our incident form.

Here are a few other examples of defined related lists I have implemented in the past. Please share any defined related lists you have come up with by commenting on this post below!

Approval – Affected CIs
–Shows a list of Affected CIs for the task being approved at the bottom of an approval form.
Name: Affected CIs
Applies to table: Approval (sysapproval_approver)
Queries from table: CIs Affected (task_ci)
Query with:

current.addQuery('task', parent.sysapproval);
current.addNotNullQuery('task');
All Request Attachments
–Shows a list of attachments from the parent request, request item, and catalog task records on a catalog task form.
Name: All Attachments
Applies to table: Catalog task (sc_task)
Queries from table: Attachment (sys_attachment)
Query with:

var qc = current.addQuery('table_sys_id', parent.sys_id);
qc.addOrCondition('table_sys_id', parent.request_item.sys_id);
qc.addOrCondition('table_sys_id', parent.request_item.request.sys_id);
current.addNotNullQuery('table_sys_id');
All Request task and Approval Attachments
–Shows a list of attachments from the parent request, request item, catalog task, and approval records on a request item form.
Name: All Attachments
Applies to table: Requested item (sc_req_item)
Queries from table: Attachment (sys_attachment)
Query with:

//Current record Attachments
var qc = current.addQuery('table_sys_id', parent.sys_id);//Request Attachments
qc.addOrCondition('table_sys_id', parent.request.sys_id);

//Catalog Task Attachments
var tsk = new GlideRecord('sc_task');
tsk.addQuery('request_item', parent.sys_id);
tsk.query();
var tskIDArr = [];
while(tsk.next()){
tskIDArr.push(tsk.sys_id.toString());
}
var tskIDStr = tskIDArr.join();
qc.addOrCondition('table_sys_id', 'IN', tskIDStr);

//Approval Attachments
var app = new GlideRecord('sysapproval_approver');
app.addQuery('document_id', parent.sys_id);
app.query();
var appIDArr = [];
while(app.next()){
appIDArr.push(app.sys_id.toString());
}
var appIDStr = appIDArr.join();
qc.addOrCondition('table_sys_id', 'IN', appIDStr);

//Do not include attachments not associated with a record
current.addNotNullQuery('table_sys_id');
Approval Task Attachments
–Shows a list of attachments from the associated approval task(s) on an approval form. In addition to displaying attachments for the specific task, this also shows attachments from the parent change request (for change tasks), the parent request item and request (for catalog tasks), the parent request (for catalog items), and the associated child items (for catalog requests).
Name: Approval Task Attachments
Applies to table: Approval (sysapproval_approver)
Queries from table: Attachment (sys_attachment)
Query with:

//Add attachments for the approval task
current.addNotNullQuery('table_sys_id');
var qc = current.addQuery('table_sys_id', parent.sysapproval);//Add parent task attachments based on the approval task type
var appTask = '';
if(parent.sysapproval.sys_class_name){
appTask = parent.sysapproval.sys_class_name;
}
if(appTask != ''){
var tsk = new GlideRecord(appTask);
tsk.get(parent.sysapproval);

//Check for approval task type and add necessary queries
if(appTask == 'change_task'){
//Query for attachments from the associated change
qc.addOrCondition('table_sys_id', tsk.change_request);
}
else if(appTask == 'sc_task'){
//Query for attachments from the associated item and request
qc.addOrCondition('table_sys_id', tsk.request_item);
qc.addOrCondition('table_sys_id', tsk.request_item.request);
}
else if(appTask == 'sc_req_item'){
//Query for attachments from the associated request
qc.addOrCondition('table_sys_id', tsk.request);
}
else if(appTask == 'sc_request'){
//Set up a variable to store item sys_ids
var items = '';
//Query for attachments from the associated request items
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('request', parent.sysapproval);
ritm.query();
while(ritm.next()){
items = items + ',' + ritm.sys_id.toString();
}
if(items != ''){
qc.addOrCondition('table_sys_id', 'IN', items);
}
}
}

The post Defined Related Lists appeared first on ServiceNow Guru.

]]>
https://servicenowguru.com/relationships/defined-related-lists/feed/ 39
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