Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

attachment getting duplicated when parent to child record

vardini
Tera Contributor

HI All,

 

i'm trying to attach attachment to parent --> its attached to child record

but when i attach 2nd attachment --> 1st one duplicated and 2nd attached
3rd time its increased more ....i want to fix this issue in businessrule

 

 

(function executeRule(current, previous /null when async/ ) {
// Add your code here
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_name', 'incident');
attachment.query();
if (attachment.next()) {
var gr = new GlideRecord('u_azure_devops');
gr.addQuery('parent', current.getUniqueValue());
gr.query();
if (gr.next()) {
// Check if the attachment already exists on the destination record
var existingAttachment = new GlideRecord('sys_attachment');
existingAttachment.addQuery('table_name', 'u_azure_devops');
existingAttachment.addQuery('table_sys_id', gr.getUniqueValue());
existingAttachment.addQuery('file_name', attachment.file_name);
existingAttachment.query();
if (!existingAttachment.next()) {
GlideSysAttachment.copy('incident', current.getUniqueValue(), 'u_azure_devops', gr.getUniqueValue());
}
}
}
})(current, previous);

 

 

please correct my code

 

1 REPLY 1

Namrata Ghorpad
Mega Sage

Hi @vardini ,

Please update your script like below and try.

var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_sys_id",current.sys_id);
gr.addQuery("table_name","u_azure_devops");
gr.query();
if(gr.getRowCount()==0) {
//add your copy script here
GlideSysAttachment.copy('incident', current.parent, 'u_azure_devops', current.sys_id);
}

 

Please refer below link and try the same.

https://www.servicenow.com/community/itsm-articles/avoid-duplicating-attachment-glidesysattachment-c... 

https://www.servicenow.com/community/itsm-articles/copying-attachment-from-one-record-to-another-rec... 

 

Please mark my answer as helpful and correct if it helps to resolve your issue.

Regards,

Namrata