Copying the Attachments from REQ to RITM to SC Task

Ganesh Manasali
Tera Contributor

Hi All,
I have a requirement to copy the attachments as below

 

1. IF attachment added to Req then it should copy to RITM and SC TASK

2. If attachment added to RITM then it should copy to SC TASK

 

To achieve this I have created a insert BR on sys_attachment table

Condition: Table name is sc_req_item or sc_request

 

Script:

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

    if (current.table_name == 'sc_request') {
        gs.log('REQ ganesh');
        var requestSysId = current.table_sys_id;
        var gr = new GlideRecord('sc_req_item');
        gr.addQuery('request', requestSysId);     
        gr.query();
        while (gr.next()) {
            var att = new GlideRecord('sys_attachment');
            att.initialize();
            att.file_name = current.file_name;
            att.size_bytes = current.size_bytes;
            att.table_name = 'sc_req_item';
            att.table_sys_id = gr.sys_id;
            att.image_height = current.image_height;
            att.image_width = current.image_width;
            att.content_type = current.content_type;
            att.compressed = current.compressed;
            att.average_image_color = current.average_image_color;
            gs.log('kishh' + current.average_image_color);
            att.size_compressed = current.size_compressed;
            att.chunk_size_bytes = current.chunk_size_bytes;
            att.hash = current.hash;
            var attRec = att.insert();
            var attDoc = new GlideRecord('sys_attachment_doc');
            attDoc.addQuery('sys_attachment', current.sys_id);
            //attDoc.setWorkflow(false);
            attDoc.query();
            while (attDoc.next()) {
                var attDocCopy = new GlideRecord('sys_attachment_doc');
                attDocCopy.initialize();
                attDocCopy.sys_attachment = attRec;
                attDocCopy.position = attDoc.position;
                attDocCopy.length = attDoc.length;
                attDocCopy.data = attDoc.data;
                attDocCopy.insert();
            }
        }
    }

    if (current.table_name == 'sc_req_item') {
        gs.log('RITM ganesh');
        var ritmSysId = current.table_sys_id; // RITM sys id
        var gr2 = new GlideRecord('sc_task');
        gr2.addQuery('request_item', ritmSysId);
        gr2.query();
        while (gr2.next()) {
            var att1 = new GlideRecord('sys_attachment');
            att1.initialize();
            att1.file_name = current.file_name;
            att1.content_type = current.content_type;
            att1.compressed = current.compressed;
            att1.table_name = 'sc_task';
            att1.size_bytes = current.size_bytes;
            att1.size_compressed = current.size_compressed;
            att1.table_sys_id = gr2.sys_id;
            att1.size_compressed = current.size_compressed;
            att1.chunk_size_bytes = current.chunk_size_bytes;
            att1.hash = current.hash;
            var attRec1 = att1.insert();
            //check whether current record present in sys_attachment_doc
            var attDoc1 = new GlideRecord('sys_attachment_doc');
            attDoc1.addQuery('sys_attachment', current.sys_id);
            attDoc1.query();
            //If current record present in sys_attachment_doc copy the data to sc_task attachment.
            while (attDoc1.next()) {
                var attDocCopy1 = new GlideRecord('sys_attachment_doc');
                attDocCopy1.initialize();
                attDocCopy1.sys_attachment = attRec1;
                attDocCopy1.position = attDoc1.position;
                attDocCopy1.length = attDoc1.length;
                attDocCopy1.data = attDoc1.data;
                attDocCopy1.insert();
            }
        }
    }


})(current, previous);

 

When I add attachment on RITM table then it will copy to SC Task as expected

but when I add attachment to REQ then attachment will copy to RITM and SC Task but while opening the attachment on sc task its thwoing error like file corrupted. 

Can anyone suggest me on this. Thanks in advance!

 

Regards,

Ganesh

5 REPLIES 5

Vishal Birajdar
Giga Sage

Hi @Ganesh Manasali 

 

You can use OOTB function from GlideSysAttachment() API:

 

copy(String sourceTable, String sourceID, String targetTable, String targetID)

 

e.g., 

 

var attachment = new GlideSysAttachment();
var incidentSysID = 'ab1b30031b04ec101363ff37dc4bcbfc';
var incGR = new GlideRecord('incident');
incGR.get(incidentSysID);

var copiedAttachments = attachment.copy('incident', incidentSysID, 'problem', incGR.getValue('problem_id'));
gs.info('Copied attachments: ' + copiedAttachments);

 

below link might help :

 

https://developer.servicenow.com/dev.do#!/reference/api/utah/server/no-namespace/c_GlideSysAttachmen...

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Cobee
Tera Expert

another option is to add a related list to the sc_task that will show the attachments on both REQ and RITM. 

Hi Cobee,

 

Do you have an example on how to achieve this please?

Also will this work with attachments added via the Attachment Variable as well as the Add Attachment option?

 

Regards

Mike

Hey @Feasood 
Under relationships you can create a new relationship and name it "Attachment"
Name:Attachment
Application:Global
Applies to table: Global[global]
Queries from table: Attachment[sys_attachment]

Query with:

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

Now depending on what table you add the Attachment related list to, it should display all the attachments associated with those records. So you can see REQ attachments on RITM for example.