
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2022 02:00 AM
Hi All,
I used this link from the forums to create a Related Attachments relationship on the sys_attachment table.
Script -
(function refineQuery(current, parent) {
var tableName = parent.getTableName();
var queryString = "table_name=" + tableName + " ^table_sys_id=" + parent.getValue("sys_id"); //default query
switch (tableName){
//add your table-specific blocks from below
//===== Requests =====
case "sc_request":
queryString = "table_nameINsc_request,sc_req_item,sc_task^table_sys_idIN" + parent.getValue("sys_id");
//find the related Requested Items
queryString += u_getRelatedRecords("sc_req_item", "request", parent.getValue("sys_id"));
//and then the Catalog Tasks
queryString += u_getRelatedRecords("sc_task", "request_item.request", parent.getValue("sys_id"));
break;
//===== Requested Items =====
case "sc_req_item":
queryString = "table_nameINsc_request,sc_req_item,sc_task^table_sys_idIN" + parent.getValue("request") + "," + parent.getValue("sys_id");
//find the related Catalog Tasks
queryString += u_getRelatedRecords("sc_task", "request_item", parent.getValue("sys_id"));
break;
//===== Catalog Tasks =====
case "sc_task":
queryString = "table_nameINsc_request,sc_req_item,sc_task^table_sys_idIN" + parent.request_item.request.toString() + "," + parent.getValue("request_item");
//find the related Catalog Tasks
queryString += u_getRelatedRecords("sc_task", "request_item", parent.getValue("request_item"));
break;
//===== Incidents =====
case "incident":
queryString = "table_nameINincident,new_call^table_sys_idIN" + parent.getValue("sys_id");
//find the related New Call
queryString += u_getRelatedRecords("new_call", "transferred_to", parent.getValue("sys_id"));
break;
//===== Incident Tasks =====
case "incident_task":
queryString = "table_nameINincident,incident_task^table_sys_idIN" +
parent.incident.toString() + "," + parent.getValue("incident");
//find the related Change Tasks
queryString += u_getRelatedRecords("incident_task", "incident",
parent.getValue("incident"));
break;
//===== Service Desk Calls =====
case "new_call":
queryString = "table_nameINincident,new_call^table_sys_idIN" + parent.getValue("sys_id") + "," + parent.getValue("transferred_to");
break;
}
current.addEncodedQuery(queryString);
function u_getRelatedRecords(table, field, sysId){
var result = "";
var gr = new GlideRecord(table);
gr.addQuery(field, sysId);
gr.query();
while (gr.next()){
result += "," + gr.getValue("sys_id");
}
return result;
}
})(current, parent);
This is working great for REQ, SCTASK but I cannot figure out how to get it working for Parent incidents and Child incidents.
Any ideas?
Thanks!
Alex
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2022 03:40 AM
@Community Alums Tried and tested solution. Please update the script as follows:
(function refineQuery(current, parent) {
var inc = new GlideRecord("incident");
inc.addQuery("parent_incident="+parent.parent_incident.toString());
inc.query();
var allChildIncidents = [];
while(inc.next()){
allChildIncidents.push(inc.sys_id.toString());
}
var queryString = "table_name="+parent.getTableName()+"^table_sys_idIN" + allChildIncidents.toString();
current.addEncodedQuery(queryString);
})(current, parent);
Please mark as correct answer if this solves your issue.
ServiceNow Community Rising Star, Class of 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2022 03:12 AM
@Community Alums Create new relationship as in image below:
Script:
(function refineQuery(current, parent) {
var queryString = "table_nameIN"+parent.getTableName()+"^table_sys_idIN" + parent.getValue("sys_id");
current.addEncodedQuery(queryString);
})(current, parent);
Then on incident form, do configure related list and add "related attachememts" related list.
Please mark as correct answer if this solves your issue.
ServiceNow Community Rising Star, Class of 2023

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2022 03:25 AM
Hi there,
Thanks for getting back to me.
I have tried what you suggested and it's still not working 😞
What I am testing is adding an attachment to the child record and checking if it shows in related attachments on the parent record (incident) but nothing shows on the parent.
Any ideas?
Thanks again,
Alex
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2022 03:40 AM
@Community Alums Tried and tested solution. Please update the script as follows:
(function refineQuery(current, parent) {
var inc = new GlideRecord("incident");
inc.addQuery("parent_incident="+parent.parent_incident.toString());
inc.query();
var allChildIncidents = [];
while(inc.next()){
allChildIncidents.push(inc.sys_id.toString());
}
var queryString = "table_name="+parent.getTableName()+"^table_sys_idIN" + allChildIncidents.toString();
current.addEncodedQuery(queryString);
})(current, parent);
Please mark as correct answer if this solves your issue.
ServiceNow Community Rising Star, Class of 2023

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2022 03:56 AM - edited 12-12-2022 03:57 AM
It's showing 131 attachments on every incident record now -