How to Copy Attachments from Case to Incident avoiding duplicates ?

Jay75
Tera Contributor

Hi everyone,

Can anyone guide me to copy attachments from 'Case' to 'Incident' without any duplicates ?

I have already tried this code which is working fine but I am getting duplicates everytime a new attachment is added.

please correct me how to avoid Duplicate attachments?

updateINCAttachment: function(source, sourceRecord) {

        var getIncident = new GlideRecord("sn_customerservice_case");

        getIncident.addQuery('sys_id', sourceRecord); // if i have case attachement with case sys id

        getIncident.query();

        if (getIncident.next()) {

            var IncidentsysId = getIncident.incident;

            //Updating the Incident attachment with Case attachment  

            GlideSysAttachment.copy('sn_customerservice_case', sourceRecord, 'incident', IncidentsysId);

        }

    },

 @Allen Andreas @Ankur Bawiskar 

1 ACCEPTED SOLUTION

Hi,

this link has solution which avoids loop

Copy Attachment from ritm to task and vice versa without looping

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

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

6 REPLIES 6

Bert_c1
Kilo Patron

Add a check to query the sys_attachment table for table_name='incident' and table_sys_id= IncidentsysId before the line that copies the attachment. and don't copy if a record exists.

Tony Chatfield1
Kilo Patron

Hi, if you want to move the attachment from your case to your incident rather than coping?
Then you could update the sys_attachment record via script.

Query for case table and the case sys_id, then for the resulting records replace the table_name with 'incident' and the table_sys_id with the sys_id of your incident.

untested but something like this.

var myAttachment = new GlideRecord('sys_attachment');
myAttachment.addQuery('table_name', 'sn_customerservice_case');
myAttachment.addQuery('table_sys_id', caseRecordSysId);
myAttachment.query();

while(myAttachment.next()) {

myAttachment.table_name = 'incident';
myAttachment.table_sys_id = incidentrecordSys_id;
myAttachment.setWorkflow(false);
myAttachment.update();

}

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

I would suggest avoid copying the files as it will increase size of sys_attachment table

Instead check this

"Related Attachments" Related List

In case you still require then have after insert BR on sys_attachment table

Condition: current.table_name == 'sn_customerservice_case'

Script:

// delete first the files from incident and then copy from case to avoid duplicates

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

	// Add your code here
	var rec = new GlideRecord('sn_customerservice_case');
	rec.addQuery("sys_id", current.table_sys_id);
	rec.query();
	rec.next();
	var incSysId = rec.parent;

	var gr = new GlideRecord("sys_attachment");
	gr.addQuery("table_sys_id", incSysId);
	gr.query();
	while (gr.next()) {
		gr.deleteRecord();
	}

	GlideSysAttachment.copy('sn_customerservice_case', current.table_sys_id, 'incident', incSysId);


})(current, previous);

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Jay

Hope you are doing good.

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader