Dinesh Kumar11
Kilo Sage
Kilo Sage

Process 1 :

Step 1: 

Create below Script Include :

var C_AttachmentUtils = Class.create();
C_AttachmentUtils.prototype = {
    initialize: function() {
    },
	
	copyAttachment: function(attachmentId, receiverTable, receiverId) {
		var grAtt = new GlideRecord('sys_attachment');
		if (!grAtt.get(attachmentId)) {
			return -1;
		}
		var grTab = new GlideRecord(receiverTable);
		if (!grTab.get(receiverId)) {
			return -1;
		}
		
		grAtt.table_name = receiverTable;
		grAtt.table_sys_id = receiverId;
		grAtt.setWorkflow(false);
		grAtt.autoSysFields(false);
		var newAtt = grAtt.insert();
		
		var grAttDoc = new GlideRecord('sys_attachment_doc');
		grAttDoc.addQuery('sys_attachment', attachmentId);
		grAttDoc.query();
		while(grAttDoc.next()) {
			var grNewAttDoc = new GlideRecord('sys_attachment_doc');
			grNewAttDoc.initialize();
			grNewAttDoc.data = grAttDoc.data;
			grNewAttDoc.length = grAttDoc.length;
			grNewAttDoc.position = grAttDoc.position;
			grNewAttDoc.sys_attachment = newAtt;
			grNewAttDoc.setWorkflow(false);
			grNewAttDoc.autoSysFields(false);
			grNewAttDoc.insert();
		}
		
	},

    type: 'C_AttachmentUtils'
};

 

Step 2: 

Configure Below Async Business Rule and call the above script include in the script section :

Table  : sys_attachment 

When : Async and Insert 

Filter condition : 

Table Name is sc_req_item

Script

(function executeRule(current, previous /*null when async*/) {
	var attUtil = new C_AttachmentUtils;
	
	var gr = GlideRecord('sc_task');
	gr.addQuery('active', true);
	gr.addQuery('request_item', current.table_sys_id);
	gr.query();
	while(gr.next()) {
		attUtil.copyAttachment(current.sys_id, 'sc_task', gr.sys_id);
	}

})(current, previous);

 

Process 2 :

Step 1: 

Configure Below After Business Rule :

Table  : sys_attachment 

When : After and Insert 

Filter condition : 

Table Name is sc_req_item

Script : 

var id = current.table_sys_id;
var gr = new GlideRecord('sc_req_item');
gr.addQuery('sys_id', id);
gr.query();
var ritm_num = '';
if (gr.next()) {
ritm_num = gr.number;
}



var gr_task = new GlideRecord('sc_task');
gr_task.addQuery('request_item.number', ritm_num);
gr_task.query();
if (gr_task.next()) {
var ritm = gr_task.sys_id;
var att = new GlideRecord('sys_attachment');
att.initialize();
att.file_name = current.file_name;
att.content_type = current.content_type;
att.compressed = current.compressed;
att.table_name = 'sc_task';
att.size_bytes = current.size_bytes;
att.size_compressed = current.size_compressed;
att.table_sys_id = ritm;
var attRec = att.insert();
var attDoc = new GlideRecord('sys_attachment_doc');
attDoc.addQuery('sys_attachment', current.sys_id);
attDoc.query();
if (attDoc.next()) {
var attDocCopy = new GlideRecord('sys_attachment_doc');
attDocCopy.initialize();
attDocCopy.sys_attachment = attRec;
attDocCopy.position = attDoc.position;
attDocCopy.length = attDoc.length;
attDocCopy.data = attDoc.data;
attDocCopy.insert();
}
}

 

Hope you will find it as helpful. Don’t forget to Mark it Helpful and Bookmark article so you can easily find on your profile.

Thank you,
Dinesh Kumar Raghu,
Capgemini India Pvt Ltd.

Version history
Last update:
‎03-10-2022 06:54 AM
Updated by: