The CreatorCon Call for Content is officially open! Get started here.

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

@Community Alums Its showing all attachments of all the child elements.

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

@Community Alums Got the issue with 131 attachments being shown on all incidents.

 

Please update script as below.

 

(function refineQuery(current, parent) {

var inc = new GlideRecord("incident");
inc.addQuery("parent_incident="+parent.sys_id.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

Hello,

 

Thanks for helping so much on this - we are getting close!

 

So adding an attachment to child does display the attachment on parent.

 

But, is there a way for the related attachments on both parent and child to always show all attachments, regardless of if they are on the parent or child.

 

e.g. adding the attachment to the child, it will display the attachment in the related list on the child record as well as the parent.

 

Also, adding an attachment to the parent record does not display the attachment in the related attachment on the child record.

 

Thanks again,

Alex

@Community Alums Yes the same script can be updated as follows.

 

(function refineQuery(current, parent) {

var inc = new GlideRecord("incident");
inc.addQuery("parent_incident="+parent.sys_id.toString());
inc.query();

var allChildIncidents = [parent.sys_id.toString()];
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 all the answers as correct answers.

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