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

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Ak8977 

 

Solution is here

https://www.servicenow.com/community/developer-articles/copy-attachments-ritm-to-task-task-to-ritm-w...

 

https://www.servicenow.com/community/now-platform-forum/i-need-to-copy-an-attachment-from-ritm-to-a-...

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Sandeep Rajput
Tera Patron
Tera Patron

@Ak8977 Please create a business rule on sc_task table as follows.

 

Screenshot 2024-02-07 at 5.48.56 PM.pngScreenshot 2024-02-07 at 5.49.19 PM.png

 

Here is the script.

 

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var attachment = new GlideSysAttachment();    
    var copiedAttachments = attachment.copy('sc_req_item', current.getValue('request_item'), 'sc_task', current.sys_id);
    gs.info('Copied attachments: ' + copiedAttachments);

})(current, previous);

Hope this helps.

The script is working when the user created a new request, but when the user update the record and added new attachment, sctask is not updating the attachment anymore

@Niccolo The business rule runs for insert operation. If you wish to make it available for update too then the script needs to be updated slightly.