Attachment added from RITM to SCTASK and SCTASK to RITM Vice Versa Incase of deletion

Sirri
Tera Guru

Hi All,

 

I have requirement like in the below scenarios:

1) If any attachment is added to RITM I need to copy that attachment to SC task.

In this case i need to update Additional comments in RITM attachment added with file name and in sctask I need to update work notes Attachment "file name" copied from RITMNumber.

2)If any attachment is added to sctask I need to copy that attachment to RITM.

In this case I need to update work notes in sctask like this a Attachment file name added and in RITM comments like Attachment file name copied from sctasknumber.

3)If Any attachment is deleted in RITM and same attachment need to be deleted from sctask.

In this case I need to update comments in RITM Attachment file names is removed and sctask Attachment file name is removed from RITM and same in sctask like this.

4)If Any attachment is deleted in Sctask and same attachment need to be deleted from RITM.

In this case work notes in sc task Needs to update to attachment file name removed and RITM comments Attachment file name has been removed from sctask and RITM.

 

For all these requirements I have written one business rules as per the below:

Table: sys_attachment

When to run: After - Insert & Delete

 

In Advance condition Iike below:

current.table_name=="sc_req_item" || current.table_name=="sc_task"

 

Script is like below:

(function executeRule(current, previous /*null when async*/ ) {
 
 if (current.table_name == "sc_req_item" || current.table_name == "sc_task") {
        var action = (current.operation() == "insert") ? "added to" : "removed from";
        var fileName = current.file_name; // Get the attachment file name

        function copyAttachment(sourceTable, sourceSysId, targetTable, targetSysId) {
            var attachment = new GlideSysAttachment();
            var sourceAttachment = new GlideRecord('sys_attachment');
            sourceAttachment.addQuery('table_name', sourceTable);
            sourceAttachment.addQuery('table_sys_id', sourceSysId);
            sourceAttachment.query();
            while (sourceAttachment.next()) {
                var inputStream = attachment.getContentStream(sourceAttachment.sys_id);
                attachment.write(targetTable, targetSysId, sourceAttachment.file_name, sourceAttachment.content_type, inputStream);
            }
        }

        function removeAttachment(targetTable, targetSysId, fileName) {
            var attachment = new GlideSysAttachment();
            var targetAttachment = new GlideRecord('sys_attachment');
            targetAttachment.addQuery('table_name', targetTable);
            targetAttachment.addQuery('table_sys_id', targetSysId);
            targetAttachment.query();
            while (targetAttachment.next()) {
                if (targetAttachment.file_name == fileName) {
                    attachment.deleteAttachment(targetAttachment.sys_id);
                }
            }
        }

        var recordSysId = current.table_sys_id;
        var recordRec = new GlideRecord(current.table_name);
        if (recordRec.get(recordSysId)) {
            var recordNumber = recordRec.number;
            var workNoteMessage = "Attachment: '" + fileName + "' has been " + action + " " + current.table_name.toUpperCase() + ": " + recordNumber;
            recordRec.comments = workNoteMessage;
            recordRec.update();

            if (current.table_name == "sc_req_item") {
                // Handle SC Task records
                var taskRec = new GlideRecord("sc_task");
                taskRec.addQuery("request_item", recordSysId);
                taskRec.query();
                while (taskRec.next()) {
                    if (current.operation() == "insert") {
                        copyAttachment("sc_req_item", recordSysId, "sc_task", taskRec.sys_id);
                        var taskWorkNoteMessage = "Attachment: '" + fileName + "' has been copied from RITM: " + recordNumber;
                        taskRec.comments = taskWorkNoteMessage;
                    } else if (current.operation() == "delete") {
                        removeAttachment("sc_task", taskRec.sys_id, fileName);
                        var taskWorkNoteMessage = "Attachment: '" + fileName + "' has been removed from RITM: " + recordNumber;
                        taskRec.comments = taskWorkNoteMessage;
                    }
                    taskRec.update();
                }
            } else if (current.table_name == "sc_task") {
                // Handle RITM record
                var ritmRec = new GlideRecord("sc_req_item");
                if (ritmRec.get(recordRec.request_item)) {
                    if (current.operation() == "insert") {
                        copyAttachment("sc_task", recordSysId, "sc_req_item", ritmRec.sys_id);
                        var ritmWorkNoteMessage = "Attachment: '" + fileName + "' has been copied from SC Task: " + recordNumber;
                        ritmRec.comments = ritmWorkNoteMessage;
                    } else if (current.operation() == "delete") {
                        removeAttachment("sc_req_item", ritmRec.sys_id, fileName);
                        var ritmWorkNoteMessage = "Attachment: '" + fileName + "' has been removed from SC Task: " + recordNumber;
                        ritmRec.comments = ritmWorkNoteMessage;
                    }
                    ritmRec.update();
                }
            }
        }
    }
 
})(current, previous);
 
Comments are updating fine as per this script but copy is not happening.
Please let me know why this not working fine in all scenarios as per my requirement. Please help me with exact code which works in these all scenarios.
 
Thank you
13 REPLIES 13

Pooja58
Kilo Sage

Hi @Sirri ,

Try below line of code to copy attachment from RITM to Sc_task and vice versa.
Syntax to copy attachment:

 

 

var attachment = new GlideSysAttachment();

attachment.copy(String sourceTable, String sourceID, String targetTable, String targetID)

 

Example to copy from Incident table to Problem

 

var attachment = new GlideSysAttachment();

var copiedAttachments = attachment.copy('incident', incidentSysID, 'problem', problemSysID);

 

To break the occurrences of infinite loop add the filter condition as below.

Pooja58_0-1740069711970.png

 

 

Mark it as correct/helpful if this answers your query.

 

Best Regards,

Pooja

 

@Pooja58 ,

 

It will copy duplicate attachment when we added second attachment. Please let me know apart from this how we can achieve

 

Thank you

Pooja2998
Mega Sage

@Sirri 

1) Business Rule 1:

(After insert) table:sys_attachment

(function executeRule(current, previous /*null when async*/ ) {
    if (current.table_name != 'sc_req_item' && current.table_name != 'sc_task') {
        return;
    }
    var sysid = '';
    var hasAttachmentCopied = false;
    if (current.table_name == 'sc_req_item') {
        sysid = current.table_sys_id;
        var scTaskGR = new GlideRecord('sc_task');
        scTaskGR.addQuery('request_item', sysid);
        scTaskGR.query();
        while (scTaskGR.next()) {
            var attachmentCheckGR = new GlideRecord('sys_attachment');
            attachmentCheckGR.addQuery('table_name', 'sc_task');
            attachmentCheckGR.addQuery('table_sys_id', scTaskGR.sys_id);
            attachmentCheckGR.addQuery('file_name', current.file_name);
            attachmentCheckGR.query();
            if (attachmentCheckGR.next()) {
                hasAttachmentCopied = true;
                break;
            }
        }
        if (!hasAttachmentCopied) {
            var attachment = new GlideSysAttachment();
            scTaskGR.query();
            if (scTaskGR.next()) {
                attachment.copy('sc_req_item', current.table_sys_id, 'sc_task', scTaskGR.sys_id);
                scTaskGR.work_notes = scTaskGR.work_notes + "\nAttachment '" + current.file_name + "' copied from RITM " + current.table_sys_id;
                scTaskGR.update();
            }
        }
    } else if (current.table_name == 'sc_task') {
        var sc = new GlideRecord('sc_task');
        sc.addQuery('sys_id', current.table_sys_id);
        sc.query();
        if (sc.next()) {
            var req = sc.request_item;
        }

        var ritmGR = new GlideRecord('sc_req_item');
        ritmGR.addQuery('sys_id', req);
        ritmGR.query();
        if (ritmGR.next()) {
            var attachmentCheckGR1 = new GlideRecord('sys_attachment');
            attachmentCheckGR1.addQuery('table_name', 'sc_req_item');
            attachmentCheckGR1.addQuery('table_sys_id', ritmGR.sys_id);
            attachmentCheckGR1.addQuery('file_name', current.file_name);
            attachmentCheckGR1.query();

            while (attachmentCheckGR1.next()) {
                hasAttachmentCopied = true;
                break;
            }
        }
        if (!hasAttachmentCopied) {
            var attachment1 = new GlideSysAttachment();
            ritmGR.query();
            if (ritmGR.next()) {
                attachment1.copy('sc_task', current.table_sys_id, 'sc_req_item', ritmGR.sys_id);
                ritmGR.comments = ritmGR.comments + "\nAttachment '" + current.file_name + "' copied from SCTask " + current.table_sys_id;
                ritmGR.update();
            }
        }
    }
})(current, previous);

2)Business Rule 2:
(After Delete) table :sys_attachment
(function executeRule(current, previous /*null when async*/ ) {

    if (current.table_name != 'sc_req_item' && current.table_name != 'sc_task') {
        return;
    }

    if (current.table_name == 'sc_req_item') {

        var scTaskGR = new GlideRecord('sc_task');
        scTaskGR.addQuery('request_item', current.table_sys_id);
        scTaskGR.query();

        while (scTaskGR.next()) {

            var attachmentDeleteGR = new GlideRecord('sys_attachment');
            attachmentDeleteGR.addQuery('table_name', 'sc_task');
            attachmentDeleteGR.addQuery('table_sys_id', scTaskGR.sys_id);
            attachmentDeleteGR.addQuery('file_name', current.file_name);
            attachmentDeleteGR.query();

            if (attachmentDeleteGR.next()) {
                attachmentDeleteGR.deleteRecord();
                scTaskGR.work_notes = scTaskGR.work_notes + "\nAttachment '" + current.file_name + "' deleted from RITM " + current.table_sys_id;
                scTaskGR.update();
            }
        }


    } else if (current.table_name == 'sc_task') {

        var sc = new GlideRecord('sc_task');
        sc.addQuery('sys_id', current.table_sys_id);
        sc.query();
        if (sc.next()) {
            var req = sc.request_item;
        }

        var ritmGR = new GlideRecord('sc_req_item');
        ritmGR.addQuery('sys_id', req);
        ritmGR.query();

        while (ritmGR.next()) {
            var attachmentDeleteGR = new GlideRecord('sys_attachment');
            attachmentDeleteGR.addQuery('table_name', 'sc_req_item');
            attachmentDeleteGR.addQuery('table_sys_id', ritmGR.sys_id);
            attachmentDeleteGR.addQuery('file_name', current.file_name);
            attachmentDeleteGR.query();
            if (attachmentDeleteGR.next()) {
                attachmentDeleteGR.deleteRecord();
                ritmGR.comments = ritmGR.comments + "\nAttachment '" + current.file_name + "' deleted from SCTask " + current.table_sys_id;
                ritmGR.update();
            }
        }

    }
})(current, previous);
If the above information helps you, Kindly mark it as Helpful and Accept the solution.
Regards,
Pooja.

 

@Pooja,

Thank you for your response.

I have observed below points in testing:

1)If we add second attachment it is copying first and second after copying first one duplicated is copying once again as per the script.

2) When we add attachment in RITM or sctask comments not updating like this attachment added. But copied comments are updated correctly but it's showing sys id in comments (Attachment 'At or after filter Last 6 months.PNG' copied from SCTask 91d1b86a83331210b104c670ceaad354)

 

Please can your correct your script and give exact one after testing.

 

Thank you

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Sirri 

Doesn't sound a valid use-case to copy same file on another record.

It will increase the table size for attachment.

use the related list feature

TNT: "Related Attachments" Related List 

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