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

Saurabh Singh5
Tera Expert

Hi @Sirri 

 

I would agree with Ankur on this part relating is the right thing to do and the best practice.

 

you can archive the attachments or create a scheduled job which would remove old attachment but if the users come back to search for some information then it would be lost.

 

But if you still want to, you can try the the below BR:

Create a new BR in the sys_attachement table with the condition as table name is sc_req_item same way you can do for sc_task on insert.

 

 

(function executeRule(current, previous /*null when async*/) {

	var scTask = new GlideRecord('sc_task');
    scTask.addEncodedQuery('request_item=' + current.table_sys_id);
    scTask.query();
	while (scTask.next()) {
var fileName, fileType, tableName, recordId, base64;
	//var data = request.body.data;
	fileName = current.file_name;
	fileType = current.content_type;
	tableName = scTask.getTableName();
	recordId = scTask.getUniqueValue();

var gsu = (typeof GlideStringUtil != 'undefined') ? (GlideStringUtil) : (Packages.com.glide.util.StringUtil); 
   var gsa = (typeof GlideSysAttachment != 'undefined') ? (new GlideSysAttachment()) : (new Packages.com.glide.ui.SysAttachment()); //few versions support the first one, other supports second
   var attachmentData = gsa.getBytes(current);
   var attachment = String(Packages.java.lang.String(attachmentData));
  // gs.info(attachment); //the data in the file will be printed as String
   var encData = GlideStringUtil.base64Encode(attachmentData); // fetches data of one attachment at a time
   //gs.info(encData);


	base64 = encData;

var att = new GlideRecord('ecc_queue');
att.initialize();
att.setValue('agent', 'AttachmentCreator');
att.setValue('topic', 'AttachmentCreator');
att.setValue('name', fileName.toString() + ':' + fileType.toString());
att.setValue('source', tableName.toString() + ':' + recordId.toString());
att.setValue('payload', base64);
att.insert();
	}

})(current, previous);

 

 

 

Along with this you would also have to create delete BR also which would need to remove the attachment on deletion.

 

 

(function executeRule(current, previous /*null when async*/ ) {

    var scTask = new GlideRecord('sc_task');
    scTask.addEncodedQuery('request_item=' + current.table_sys_id);
    scTask.query();
    while (scTask.next()) {
        var attachment = new GlideSysAttachment();
        var agr = attachment.getAttachments('sc_task', scTask.getUniqueValue());
        while (agr.next()) {

            if (agr.getValue('file_name') == current.file_name) {
                attachment.deleteAttachment(agr.getUniqueValue());
            }

        }
    }
})(current, previous);

 

 

 

Regards,

Saurabh

Hi @Saurabh Singh5 ,

 

Thanks for your response.

From SCTASK TO RITM it's not working in this case reason please.

 

Thank you

sunil maddheshi
Tera Guru

@Sirri 

Can you try with below updated code:

(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 fileBytes = attachment.getBytes(sourceAttachment.sys_id); // Fetch file content as bytes
                if (fileBytes) {
                    attachment.write(targetTable, targetSysId, sourceAttachment.file_name, sourceAttachment.content_type, fileBytes);
                }
            }
        }

        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.addQuery('file_name', fileName); // Ensure we delete the correct file
            targetAttachment.query();

            while (targetAttachment.next()) {
                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 commentMessage = "Attachment: '" + fileName + "' has been " + action + " " + current.table_name.toUpperCase() + ": " + recordNumber;
            recordRec.comments = commentMessage;
            recordRec.update();

            if (current.table_name == "sc_req_item") {
                // Copy or remove attachments between RITM -> SCTASK
                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);
                        taskRec.work_notes = "Attachment: '" + fileName + "' has been copied from RITM: " + recordNumber;
                    } else if (current.operation() == "delete") {
                        removeAttachment("sc_task", taskRec.sys_id, fileName);
                        taskRec.work_notes = "Attachment: '" + fileName + "' has been removed from RITM: " + recordNumber;
                    }
                    taskRec.update();
                }
            } else if (current.table_name == "sc_task") {
                // Copy or remove attachments between SCTASK -> RITM
                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);
                        ritmRec.comments = "Attachment: '" + fileName + "' has been copied from SC Task: " + recordNumber;
                    } else if (current.operation() == "delete") {
                        removeAttachment("sc_req_item", ritmRec.sys_id, fileName);
                        ritmRec.comments = "Attachment: '" + fileName + "' has been removed from SC Task: " + recordNumber;
                    }
                    ritmRec.update();
                }
            }
        }
    }

})(current, previous);

Please mark correct/helpful if this helps you!

@sunil maddheshi ,

 

It's not working. please test and give correct script so I can verify.

 

Thank you