Pradyumna Das
Tera Expert

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.

Comments
Ash41
Kilo Sage

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)

sailesh1
Tera Contributor

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?

 

Pradyumna Das
Tera Expert

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.

Pradyumna Das
Tera Expert

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.

Purushotham Ga2
Tera Contributor

Hi Das- Thanks a lot. Could you please add BR conditions as well

Purushotham Ga2
Tera Contributor

please provide the table and conditions

bhupendrapareek
Tera Contributor

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.

Pradyumna Das
Tera Expert

@bhupendrapareek  Please use After BR and also add condition table name is sc_task or sc_req_item

JP6
Tera Contributor

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

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.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 copied. New Attachment 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();
    }
}

// Get SC_Task linked to a Request
function getTasks() {
    var task = new GlideRecord('sc_task');
    task.addQuery('request_item', current.table_sys_id);
    task.addQuery('active', true);
    task.query();
    return task;
}

// Get Request Details
function getRequestDetails() {
    var req = new GlideRecord('sc_request');
    req.addQuery('sys_id', current.table_sys_id);
    req.query();
    return req;
}

// Check if attachment is already linked
function checkAttachmentPresent(id) {
    var attach = new GlideRecord('sys_attachment');
    attach.addQuery('hash', current.hash);
    attach.addQuery('table_sys_id', id);
    attach.query();
    return attach.next();
}

// Copy attachment from REQ to SC_Task
function copyREQtoTask() {
    var request = getRequestDetails();
    if (request.next()) {
        var tasks = getTasks();
        while (tasks.next()) {
            if (!checkAttachmentPresent(tasks.sys_id)) {
                copyAttachment("sc_task", tasks.sys_id);
                gs.info('Attachment copied from REQ to SC_Task successfully.');
            } else {
                gs.info('Attachment already present in SC_Task.');
            }
        }
    } else {
        gs.info('Request not found.');
    }
}

// Copy attachment from SC_Task to REQ
function copyTaskToREQ() {
    var task = new GlideRecord('sc_task');
    task.addQuery('sys_id', current.table_sys_id);
    task.query();
    if (task.next()) {
        if (!checkAttachmentPresent(task.request)) {
            copyAttachment("sc_request", task.request);
            gs.info('Attachment copied from SC_Task to REQ successfully.');
        } else {
            gs.info('Attachment already present in REQ.');
        }
    } else {
        gs.info('SC_Task not found.');
    }
}

// Determine table type and execute correct function
if (current.table_name == 'sc_request') {
    copyREQtoTask();
}

if (current.table_name == 'sc_task') {
    copyTaskToREQ();
}
Version history
Last update:
‎07-30-2021 06:43 AM
Updated by: