copy attachments ,getting duplicates in ever new attachment added?

srinivaskan
Tera Contributor

Hi folks, i have a urgent Requirment for copy attachment s ,we have make and checker to kind of tasks, once the maker close the task the checker task will be open both the tasks are same parent case in the task table. now the requirment is once we add a attachment to maker it need to be copy in the checker if add a attachment in maker its need to be copy in the checker .for that I have created a BR for that .that BR is working for copying the attachment s but every time its copying all the attachment s from maker to checker and checker to maker i need only copy the latest attachment which add in the tasks.how to fix this issue.  my BR Is :

 

(function executeRule(current, previous) var parentCase = current.parent_case.sys_id;
var number = current.number;
var makerShortDesc = current.short_description.split('-');

// Get the related task (checker if current is maker, or maker if current is checker)
var relatedTaskGr = new GlideRecord('task');
relatedTaskGr.addQuery('parent_case', parentCase);
relatedTaskGr.addQuery('number', '!=', number);
relatedTaskGr.addQuery('sys_id', '!=', current.sys_id);
relatedTaskGr.addEncodedQuery('short_descriptionLIKE' + makerShortDesc[0]);
relatedTaskGr.query();

while (relatedTaskGr.next()) {
// Get the latest attachment from the current task
var latestAttachment = new GlideRecord('sys_attachment');
latestAttachment.addQuery('table_name', 'task');
latestAttachment.addQuery('table_sys_id', current.sys_id);
latestAttachment.orderByDesc('sys_created_on'); // get the latest one
latestAttachment.setLimit(1); // just the latest
latestAttachment.query();

if (latestAttachment.next()) {
var fileName = latestAttachment.file_name + '';
var fileSize = latestAttachment.size_bytes + '';

// Check if the same file already exists in the related task
var duplicateCheck = new GlideRecord('sys_attachment');
duplicateCheck.addQuery('table_name', 'task');
duplicateCheck.addQuery('table_sys_id', relatedTaskGr.sys_id);
duplicateCheck.addQuery('file_name', fileName);
duplicateCheck.addQuery('size_bytes', fileSize);
duplicateCheck.query();

if (!duplicateCheck.next()) {
// Only copy the latest attachment if not a duplicate
var gsa = new GlideSysAttachment();
gsa.copy('task', current.sys_id, 'task', relatedTaskGr.sys_id);
}else {

var fileName2 duplicateCheck.file_name + '';

gs.info('duplicate found + fileName2);

} } } })(current, previous);

 

@ Ankur Bawiskar,

0 REPLIES 0