- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 07-30-2021 06:43 AM
Create a Business Rule on the sys_attachmnet table and add the below code in the business rule.
function copyAttachment(table_name, table_sys_id) {
var att = new GlideRecord('sys_attachment');
att.initialize();
att.file_name = current.file_name;
att.content_type = current.content_type;
att.table_name = table_name;
att.table_sys_id = table_sys_id;
att.hash = current.hash;
//att.state = current.state;
att.average_image_color = current.average_image_color;
att.chunk_size_bytes = current.chunk_size_bytes;
att.compressed = current.compressed;
att.image_height = current.image_height;
att.image_width = current.image_width;
att.size_bytes = current.size_bytes;
att.size_compressed = current.size_compressed;
var attch = att.insert();
gs.info('Attachment coppied and Attachmnet ID: ' + attch);
var attDoc = new GlideRecord('sys_attachment_doc');
attDoc.addQuery('sys_attachment', current.sys_id);
attDoc.query();
while (attDoc.next()) {
var attDocCopy = new GlideRecord('sys_attachment_doc');
attDocCopy.initialize();
attDocCopy.sys_attachment = attch;
attDocCopy.position = attDoc.position;
attDocCopy.length = attDoc.length;
attDocCopy.data = attDoc.data;
attDocCopy.insert();
}
}
function getTasks() {
var task = new GlideRecord('sc_task');
task.addQuery('request_item', current.table_sys_id);
task.addQuery('active', true);
task.query();
return task;
}
function getRequestDetails() {
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('sys_id', current.table_sys_id);
//ritm.addQuery('cat_item', '060f3afa3731300054b6a3549dbe5d3e');
ritm.query();
return ritm;
}
function checkAttachmentPresent(id) {
var attach = new GlideRecord('sys_attachment');
attach.addQuery('hash', current.hash);
attach.addQuery('table_sys_id', id);
attach.query();
if (attach.next()) {
return true;
}
return false;
}
function copyTaskToTask(taskReq) {
var task = new GlideRecord('sc_task');
task.addQuery('request_item', taskReq);
task.addQuery('active', true);
task.query();
while (task.next()) {
if (checkAttachmentPresent(task.sys_id) != true) {
// GlideSysAttachment.copy("sc_task", sctask.sys_id, "sc_task", task.sys_id);
copyAttachment("sc_task", task.sys_id);
gs.info('Attachment coppied from TASK to TASK successfully');
} else {
gs.info('Attachmnet already present in the TASK');
}
}
}
if (current.table_name == 'sc_req_item') {
var request = getRequestDetails();
if (request.next()) {
var scTask = getTasks();
while (scTask.next()) {
if (checkAttachmentPresent(scTask.sys_id) != true) {
//GlideSysAttachment.copy("sc_req_item", request.sys_id, "sc_task", scTask.sys_id);
copyAttachment("sc_task", scTask.sys_id);
gs.info('Attachment coppied from RITM to TASK successfully');
} else {
gs.info('Attachmnet already present in the TASK');
}
}
} else {
gs.info('RITM not found');
}
}
if (current.table_name == 'sc_task') {
var sctask = new GlideRecord('sc_task');
sctask.addQuery('sys_id', current.table_sys_id);
sctask.query();
if (sctask.next()) {
if (sctask.request_item.cat_item == '060f3afa3731300054b6a3549dbe5d3e' && checkAttachmentPresent(sctask.request_item) != true) {
// GlideSysAttachment.copy("sc_task", sctask.sys_id, "sc_req_item", sctask.request_item);
copyAttachment("sc_req_item", sctask.request_item);
gs.info('Attachment coppied from TASK to RITM successfully');
copyTaskToTask(sctask.request_item);
} else {
gs.info('Attachmnet already present in RITM');
copyTaskToTask(sctask.request_item);
}
} else {
gs.info('TASK not found');
}
}
Please mark it helpful If it helps you.
- 2,558 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi,
I see you used sys_id, may I know what that sys_id for? also, may I know BR condition?
I need to implement samething
if (sctask.request_item.cat_item == '060f3afa3731300054b6a3549dbe5d3e' && checkAttachmentPresent(sctask.request_item) != true)

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi,
This works well
However, I have a criteria like below, could you please suggest how to handle it
RITM has an attachment and the BR copies it to TASK
now that another attachment is added to the RITM, but with the BR, system is re-adding the first attachment again when I use GlideAttachmentCopy method.
How to prevent the duplicate attachments from being added?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hello Dear,
That sys_id is for a particular Catalog Item if you want to implement for all then you skip that sys_id check condition.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hello Dear,
You have to use above script. This script will never re-add any attachment if it has already been present for the particular record.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi Das- Thanks a lot. Could you please add BR conditions as well
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
please provide the table and conditions
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
this script works almost fine, but for me , its keeping only one attachment in RITM , if i attache more than one , it deletes previous file, Any suggestions to fixt it ?i am using before BR @Pradyumna Das Advance thanks for your support.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@bhupendrapareek Please use After BR and also add condition table name is sc_task or sc_req_item
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi @Pradyumna Das , I want to sync attachments between Request and SC_task. I had updated your code , it is working when the attachment is added to sc_task it gets copied to Request . But when the attachment is added to Request, it is not getting copied to sc_task, Please help