Related Attachments not showing Parent/Child incidents attachments?

Community Alums
Not applicable

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

 

 

 

 

 

1 ACCEPTED SOLUTION

@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.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

View solution in original post

8 REPLIES 8

jaheerhattiwale
Mega Sage
Mega Sage

@Community Alums Create new relationship as in image below:

jaheerhattiwale_4-1670843413421.png

 

 

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.jaheerhattiwale_3-1670843384695.png

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Community Alums
Not applicable

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

@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.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Community Alums
Not applicable

It's showing 131 attachments on every incident record now -

 

sn1.PNG

 

sn1.PNG