How to copy attachments from RITM to SCTASK and vise versa

Snow113
Tera Contributor

If user added an attachment on RITM it should add to sctasks if there are multile tasks it should be added to them and the same way if user add on sc task that should be added on RITM.

Can anyone please let me know the best practise for this without having loops in attachment table

4 REPLIES 4

Namrata Ghorpad
Mega Sage
Mega Sage

Hi,

Please refer the below article.

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

 

Please mark my answer as helpful/correct if it helps you.

Regards,

Namrata

Amit Gujarathi
Giga Sage
Giga Sage

Hi @Snow113 ,
I trust you ared oing great.

  1. Create a business rule on the Requested Item [sc_req_item] table:
    • Set the When field to before and the Insert, Update, and Delete checkbox selected.
    • In the script, check if there is a new attachment added to the Requested Item (RITM).
    • If a new attachment is found, retrieve all associated tasks (sc_task) related to the RITM.
    • Loop through each task and create a new attachment record on the task, copying the attachment details from the RITM.

 

(function executeRule(current, previous) {
    if (current.operation() == 'insert') { // Check if new attachment is added to the RITM
        var tasks = new GlideRecord('sc_task');
        tasks.addQuery('request_item', current.sys_id);
        tasks.query(); // Retrieve associated tasks related to the RITM
        while (tasks.next()) {
            var taskAttachment = new GlideRecord('sys_attachment');
            taskAttachment.initialize();
            taskAttachment.table_name = 'sc_task';
            taskAttachment.table_sys_id = tasks.sys_id;
            taskAttachment.file_name = current.file_name;
            taskAttachment.file_size = current.file_size;
            taskAttachment.content_type = current.content_type;
            taskAttachment.file_contents = current.file_contents;
            taskAttachment.insert(); // Create a new attachment record on each task
        }
    }
})(current, previous);
​

 


Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



K Akhila
Kilo Guru

Hi Snow113,

 

 

Create 3 Business rules. 

1) sc_task = copy RITM level to TASK level(before - insert)
2) sc_task = copy TASK level to RITM level(before - insert/update)
3) sys_attachment = remove duplicate attachments(before - insert, update)


1) sc_task = copy RITM level to TASK level(before - insert) :
script : 
           (function executeRule(current, previous /*null when async*/ ) {
    var gr = new GlideRecord('sys_attachment');
    gr.addQuery("table_sys_id",current.sys_id);
    gr.query();
                        GlideSysAttachment.copy('sc_req_item', current.request_item, 'sc_task', current.sys_id);
    gs.info(current.number);
    
})(current, previous);

2) sc_task = copy TASK level to RITM level(after - update): 

script : 

(function executeRule(current, previous /*null when async*/ ) {
GlideSysAttachment.copy('sc_task', current.sys_id, 'sc_req_item', current.request_item);
 gs.info(current.number);
    
})(current, previous);

3) sys_attachment = remove duplicate attachments(before - insert, update) : 
script : 

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

    var attach = new GlideRecord('sys_attachment');
    attach.addQuery('table_sys_id', current.table_sys_id);
    attach.addQuery('table_name', current.table_name);
    attach.addQuery('file_name', current.file_name);
    attach.addQuery('content_type', current.content_type);
    attach.addQuery('size_bytes', current.size_bytes);
    attach.query();
    if (attach.next()) {
        current.setAbortAction(true);
    }

})(current, previous);



 Hope this will solve your copy attachment issue.
If this works for you please mark Helpful