Sync Attachment between sc_request to sc_task and visa versa

Not applicable

I have a requirement to sync attachment between sc_request and sc_task and vis versa 

I tried the below script using business rule while  it is working for sc_task to sc_request  .  I tried using similar logic to copy from sc_request to sc_task it is not working. Seeking our assistance.

sc_task to sc_request

 

Table:sys_attachment

when to run: after insert

condition: Table name is sc_task 

 

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

    // Add your code here
    var taskRec = new GlideRecord(current.table_name.toString());
taskRec.addQuery("sys_id", current.table_sys_id.toString());
taskRec.query();

if(taskRec.next()){
    GlideSysAttachment.copy(current.table_name.toString(), current.table_sys_id.toString(), "sc_request", taskRec.request.toString());
   
}
 
6 REPLIES 6

mahesh009
Tera Expert

Hi, I can see the GlideSysAttachment syntax from the script is incorrect and we should give the records sys id not table sys id.
Replace the  GlideSysAttachment.copy(current.table_name.toString(), current.table_sys_id.toString(), "sc_request", taskRec.request.toString());  line with

var attchment=new GlideSysAttachment();

attchment.copy("source_table_name", "source_table_record_sysId", "target_table_name", "target_table_record_sysId");
OR
new GlideSysAttachment().copy("source_table_name", "source_table_record_sysId", "target_table_name", "target_table_record_sysId");

Thanks, and Regards

Mahesh

Please mark this response as correct or helpful if it assisted you with your question.



Ankur Bawiskar
Tera Patron

@Community Alums 

this will work to copy from sc_request to sc_task

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

    // Add your code here
        // query to find sc_task record for this REQ sysId
        var gr = new GlideRecord("sc_task");
        gr.addQuery("request", current.table_sys_id);
        gr.query();
        if (gr.next()) {
            var attachment = new GlideSysAttachment();
            attachment.copy(current.table_name.toString(), current.table_sys_id.toString(), "sc_task", gr.sys_id.toString());
        }
})(current, previous);

I will suggest not to copy attachments rather use related list feature and show the files in related list

This will help in keeping attachment table size as lower

TNT: "Related Attachments" Related List 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Not applicable

Thanks @Ankur Bawiskar , the script is working. But when I add an attachment via a portal/Native UI, it is duplicating the number of attachments with query continuously executing.

@Community Alums 

Glad to know that my script is working.

Please mark my response as correct and close the thread.

The discussion can continue on answered thread as well.