Copy attachments from RITM to sc_Task

Ak8977
Tera Expert

I need to copy attachment from RITM to sc_task.

1 ACCEPTED SOLUTION

Josh Pirozzi
Kilo Sage

Hello @Ak8977 ,

 

Instead of copying the attachment, we took the approach of creating a Relationship (sys_relationship.list), and adding the Relationship Tab under the Related Links on REQ/RITM/SCTASK. This ensures visibility to the Attachments regardless which level or Task of a Request you're working on, and that you don't duplicate Attachments.

 

Here is how we configured the Relationship:

  • Application = Global
  • Applies to table = Global
  • Queries from Table = Attachment (sys_attachment)
(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;
    }

    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);
 
 
Hope this helps!
 
- Josh

View solution in original post

7 REPLIES 7

Can you please share the script for that? Thanks 🙂

@Niccolo Please create a separate question for this request and tag me in it. I will provide the script.

Josh Pirozzi
Kilo Sage

Hello @Ak8977 ,

 

Instead of copying the attachment, we took the approach of creating a Relationship (sys_relationship.list), and adding the Relationship Tab under the Related Links on REQ/RITM/SCTASK. This ensures visibility to the Attachments regardless which level or Task of a Request you're working on, and that you don't duplicate Attachments.

 

Here is how we configured the Relationship:

  • Application = Global
  • Applies to table = Global
  • Queries from Table = Attachment (sys_attachment)
(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;
    }

    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);
 
 
Hope this helps!
 
- Josh