Copying Attachments from Parent to Child
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2019 07:55 AM
Hello all,
I have a question on how to get all of the parent attachments onto the child automatically. I currently have made a Business rule on the Parent table.
I feel like there is something obvious that I am missing. Any help is appreciated. Thanks in advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2019 08:53 AM
it might be an order thing.
GlideSysAttachment.copy(String sourceTable, String sourceID, String targetTable, String targetID)
are you trying to copy attachment from the change request to the change task, or from the change task to the change request?
change task to change request :
GlideSysAttachment.copy('change_tack',current.sys_id,'change_request', current.change_request);
change request to change task :
GlideSysAttachment.copy('change_request', current.change_request, 'change_tack',current.sys_id);
personally I would use a custom relationship so you arent copying large attachments and duplicating them on the attachment table.
to do that you will need to go to the System Definition > Relationships
it applies to the table change_task
queries from table sys_attachment
the script would be
current.addQuery("table_name", "change_request");
current.addQuery("table_sys_id", current.change_request);
then you just add this relationship as a related list.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2019 09:16 AM
change request to change task :
GlideSysAttachment.copy('change_request', current.change_request, 'change_task',current.sys_id);
WORKED!!!
But the only probably that I have run into is now every time the change task is updated, the attachments duplicate. I thought about making the business rule only run on Insert, but the problem with that is that is users add more attachments to the change request later on, the attachments won't copy over.
Could I use the below script? Not sure exactly how it fits though...
Found at https://docs.servicenow.com/bundle/madrid-servicenow-platform/page/script/useful-scripts/reference/r_UsefulAttachmentScripts.html
function fixDuplicateImages(){var gr =new GlideRecord('sys_attachment'); gr.addQuery('table_name','LIKE','ZZ_YY%'); gr.orderBy('table_sys_id'); gr.orderByDesc('sys_created_on'); gr.query();var lastID ='not_a_match';var lastFile ='not_a_match';while(gr.next()){var isDuplicate =(lastID == gr.table_sys_id)&&(lastFile == gr.file_name); lastID = gr.table_sys_id; lastFile = gr.file_name; gs.print(gr.table_sys_id+' '+ gr.table_name+' '+ gr.file_name+' '+ gr.sys_created_on+' '+ isDuplicate);if(isDuplicate) gr.deleteRecord();}}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2019 10:19 AM
that might be best if you are copying the attachments on update.
If you were to use it as is, it would be like this.
GlideSysAttachment.copy('change_request', current.change_request, 'change_task',current.sys_id);
fixDuplicateImages();
function fixDuplicateImages(){var gr =new GlideRecord('sys_attachment');
gr.addQuery('table_name','LIKE','ZZ_YY%');
gr.orderBy('table_sys_id');
gr.orderByDesc('sys_created_on');
gr.query();
var lastID ='not_a_match';
var lastFile ='not_a_match';
while(gr.next()){
var isDuplicate =(lastID == gr.table_sys_id)&&(lastFile == gr.file_name);
lastID = gr.table_sys_id;
lastFile = gr.file_name;
gs.print(gr.table_sys_id+' '+ gr.table_name+' '+ gr.file_name+' '+ gr.sys_created_on+' '+ isDuplicate);
if(isDuplicate)
gr.deleteRecord();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2019 10:35 AM
Hmmm it doesn't seem to kick in properly because the attachments still get duplicated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2019 06:52 AM
Hi htank6,
By chance did you check my answer to the below thread? The same duplicate attachment issue was handled over there. Attachment sync REQ-TASK
Hope this helps. Please mark Helpful/Correct based on the impact.