- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-07-2022 04:49 PM
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);
}
},
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-11-2022 07:51 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-07-2022 06:07 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-07-2022 06:15 PM
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-07-2022 08:16 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-10-2022 05:57 AM
@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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader