Copy attachment from Change Task to Change Request via sys_attachment table

Rahil Mirza
Tera Contributor

Hi,

I am trying to copy attachments from ctask to change, I am able to do it but I am getting duplicates of previously attached attachments on change request level as I add any new attachment,

Here is the BR script:

(function executeRule(current, previous /*null when async*/) {
// Get the Change Task record associated with the attachment
var changeTaskGr = new GlideRecord('change_task');
if (changeTaskGr.get(current.table_sys_id)) {
// Get the parent Change Request Sys ID from the Change Task record
var changeRequestSysId = changeTaskGr.getValue('parent');

// Check if an attachment with the same name and file size already exists in the Change Request
var attachmentName = current.file_name.toString();
var attachmentSize = current.size_bytes.toString();
var targetAttachments = new GlideSysAttachment().getAttachments('change_request', changeRequestSysId);
var exists = false;

while (targetAttachments.next()) {
if (targetAttachments.file_name.toString() === attachmentName &&
targetAttachments.size_bytes.toString() === attachmentSize) {
exists = true;
break;
}
}

// Copy the attachment if it does not already exist
if (!exists) {
GlideSysAttachment.copy(current.table_name, current.table_sys_id, 'change_request', changeRequestSysId);
// Log the action for debugging purposes
gs.log('Copied attachment from change_task: ' + current.sys_id.toString() + ' to change_request: ' + changeRequestSysId);
} else {
gs.log('Attachment already exists in change_request: ' + changeRequestSysId);
}
}
})(current, previous);

9 REPLIES 9

Runjay Patel
Giga Sage

Hi @Rahil Mirza ,

 

I have created one blog for same. You just need to change the table name. It is tested solution and it should work for you as well.

https://servicenowwithrunjay.com/copy-attachment-from-one-table-to-another/

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

Hi @Runjay Patel Thanks for the solution, although I believe my problem statement does not totally align with the proposed solution apart from changing tables, but thank you!

Ankur Bawiskar
Tera Patron
Tera Patron

@Rahil Mirza 

that's the OOB behavior and GlideSysAttachment will copy all the files.

What I will recommend is delete all the previous ones and copy all the files fresh

OR

refer below link where I shared solution on how to copy single file to target record

Is there any way to copy single attachment from multiple attachments ?? 

Also check this link

Select specific attachments to copy 

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

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

@Rahil Mirza 

Thank you for marking my response as helpful.

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

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