Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

@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

suresh1995
Tera Contributor

Thanks for your help @Ankur Bawiskar