Copy attachment from Request form to SC task form

suresh1995
Tera Contributor

Hi all, i have the task that if i attached one file in request form in catalog item , that same attachment will automatically reflected in SC task from & i have wrote the after business rule  in request from  but it's not work . kindly guide me 

Business rule:

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

//GlideSysAttachment.copy('sc_task',current.sys_id , 'sc_request', current.request_item);
// Add your code here
var id = current.table_sys_id;
var gr = new GlideRecord('sc_task');
gr.addQuery('sys_id', id);
gr.query();
if(gr.next()){
var ritm = gr.request_item;
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_request';
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();
while(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();
}
}

})(current, previous);
1 ACCEPTED SOLUTION

@suresh1995 

why to copy and increase the attachment table size?

why not just show the files in related list? check below link

TNT: "Related Attachments" Related List 

If you still want to copy then use this script

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

	var taskRec = new GlideRecord('sc_task');
	taskRec.addQuery("request_item.request", current.table_sys_id.toString());
	taskRec.query();
	while(taskRec.next()){
		GlideSysAttachment.copy(current.table_name.toString(), current.table_sys_id.toString(), "sc_task", taskRec.sys_id.toString());
	}

})(current, previous);

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

View solution in original post

6 REPLIES 6

Kristen Ankeny
Kilo Sage

Rather than copying the files to the tasks, which can exponentially increase the size of you attachment table, I would suggest creating a defined related list on sc_task that has a filter to show the attachments from the task and its parent requested item. 

suresh1995
Tera Contributor

Hi Thanks for your reply but my task is same attachment will show in request item form & SCtask form using business rule .I want to know how to match the attachment kindly assist me.

Kristen Ankeny
Kilo Sage

If they must have it copied, then you should use the GlideSysAttachment API: https://developer.servicenow.com/dev.do#!/reference/api/tokyo/server_legacy/GlideSysAttachmentGlobal...

suresh1995
Tera Contributor

Thank you for your reply , but still not working

Task : when i upload any attachment in sc_request form ,it should also been seen in sc_task form . i tried below script but its not working ,kindly guide me

After business rule & condition: table name -is- sc_task

script:

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

var taskRec = new GlideRecord(current.table_name.toString());
taskRec.addQuery("sys_id", current.table_sys_id.toString());
taskRec.query();

if(taskRec.next()){
GlideSysAttachment.copy(current.table_name.toString(), current.table_sys_id.toString(), "sc_task", taskRec.request.toString());
}
})(current, previous);