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.

how to copy attachment from catalog task to RITM

Prashanth Rejin
Kilo Guru

i was trying to copy the attachement from task to Ritm it was working fine for 1 task and for 2 nd task the system getting hanged

10 REPLIES 10

Bhojraj Dhakate
Tera Expert

HI,

create business rule for copying attachment from one table to another.

table name: sc_req_item

when: after insert

Code:

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


var att = new GlideSysAttachment();
var src_table = "sc_task";  //source table
var tar_table = "sc_req_item"; //target table


return att.copy(src_table, current.sc_task, tar_table, current.sys_id);


})(current, previous);

 

Thanks,

Bhojraj Dhakate

This is Helpful For me Thank you So much dear Bhojraj Dhakate

 

 

Regards,

Dinesh Kumar

Prashanth Rejin
Kilo Guru

Hi have tried by doing the below code:

After insert

Table: sys_attachment

condition: table_name is sc_task.

 

it is working fine when we are adding an attachment to first task but it is not working when we try on second task of same RITM

 

(function executeRule(current, previous /*null when async*/) {
	var gr = new GlideRecord('sc_task');
	gr.addQuery('sys_id',current.table_sys_id);
	gr.query();
	var ritm = gr.request_item;
       if(gr.next()){
		if(gr.request_item.cat_item == 'sys_id of catalog item'  ){
		GlideSysAttachment.copy('sc_task', gr.sys_id, 'sc_req_item', ritm);
		}
	}
}	

Hi Prashant,

Please try following code:

table name: sys_attachment

Operation: insert/update

Script:

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

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_req_item';
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);

 

Above code is working for me.

Thanks,

Bhojraj