Copying attachment from Request to ritm and sc_task and ritm to sc_task.

Kishor O
Tera Sage

1.I have a requirement where I need to copy an attachment from a Request to ritm and associated sc-task.

2. When an attachment is attached to ritm alone it should copy to the associated sc_task.

 

Please help to achieve this requirement. 

1 REPLY 1

IAmAbhigyaan12
Giga Guru

Hi @Kishor O ,

1. Can you try below code on sys_attachment table with after insert BR, 

(function executeRule(current, previous /*null when async*/) {
	var sctask = new GlideRecord('sc_task');
	sctask.addQuery('request_item', current.table_sys_id);
	sctask.addQuery('request_item.cat_item', 'PASS THE SYS_ID OF CATALOG ITEM HERE');
	sctask.query();
	if(sctask.next()){
		var deleteTaskAttachment = new GlideRecord('sys_attachment');
		deleteTaskAttachment.addQuery('table_sys_id', sctask.sys_id.toString());
		deleteTaskAttachment.addQuery('table_name', 'sc_task');
		deleteTaskAttachment.deleteMultiple();
		
		var ritmAttachment = new GlideRecord('sys_attachment');
		ritmAttachment.addQuery('table_sys_id', sctask.request_item);
		ritmAttachment.query();
		if(ritmAttachment.next())
			GlideSysAttachment.copy('sc_req_item', ritmAttachment.table_sys_id, 'sc_task', sctask.sys_id); 
	}
})(current, previous);

 
2. 

Create After business rule on "sys_attachment" table, operation would be "INSERT", use the business rule condition Table Name | IS | sc_req_item table.

 

copyAttachments(current);

function copyAttachments(attachment) {
	var tasks = new GlideRecord("sc_task");
	tasks.addActiveQuery();
	tasks.addQuery('request_item', current.table_sys_id);
	tasks.query();
	while(tasks.next()) {
		gs.log('Copying attachment for task ' + tasks.number, 'ServiceNow Community');
		var gsa = new GlideSysAttachment();
		gsa.copy(current.table_name, current.table_sys_id, tasks.getTableName(), tasks.sys_id);
	}
}

 

If my answer solved your issue, please mark my answer as Correct & hit like Helpful

Thanks
Abi