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

Created Relationship between sc_task and sys_attachment - Closing tasks duplicates attachments

mythNOW
Tera Expert

Hello everyone,

 

I have created a relationship (System Definition > Relationship) to show any attachments uploaded from sc_task to also show at the RITM.  This is working great, however when performing these steps at the sc_task page, duplicates are created when the task is closed complete.  Here is the script I used:

 

(function refineQuery(current, parent) {
    var tableName = parent.getTableName();
    var queryString = "table_name=" + tableName + " ^table_sys_id=" + parent.getValue("sys_id"); //default query

    switch (tableName) {
             //*********** 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;

 
      //***************SC 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;
    }

    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);

 

1. 

mythNOW_0-1723144768300.png

For my related list tab, I select New, and upload an attachment.

 

mythNOW_1-1723144914085.png

The task closes to closed complete. Since the task is closed, the attachment is now visible on the top of the RITM.

mythNOW_2-1723150103081.png

 

This is great, this is what I need to send attachments when approvals are required. However, this workflow calls upon another task, but I noticed the attachments are duplicated. (two different sys_id's)

mythNOW_3-1723150207967.png

What is causing the duplication?  Thank you in advance.

 

2 REPLIES 2

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

Hi @mythNOW - Would you please clarify if the two attachments shown in your screenshot of the RITM Attachments related list are both attached to the Catalog Task? Or is one attached to the TASK, and the other attached to the RITM? For the sake of troubleshooting, you can personalize the list layout and show "Table name" if you haven't already.

 

Ultimately, I would assume that what's going on with the attachments is unrelated to relationship. More likely, there is some implement using GlideSysAttachment.copy.

Hello sheldondotswift,

 

Thank you for reaching out, I instead opted to change to this script for my use case.  Doing this instead did not duplicate the attachment.  

 

Application: Global

Applies to table: sc_task

Queries from table: sys_attachment

 

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');