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 RITM to SC Task on request submit and updates from portal.

Kiran Patil3
Giga Expert

Hi

I want to copy attachment from RITM to SC Task. There are two scenario

  • Attachment attached while submitting the request
  • Attachment attached by end user from portal (Updates to RITM).

I have below AFTER BR on insert and it is working fine but not in all scenario:

  1. Below BR is not working if attachment is attached while submitting the request.
  2. If User attached 2nd attachment, it copy all attachment to SC Task (On updates). As of now, I am cleaning existing attachment and copy all once again to SC Task but it changes timestamp of the Attachments

Any suggestion, specially on point 1.

-----------------------------------------------------------------------------------------------------------------------------------------------

 

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

{
var gr = new GlideRecord('sc_task');
gr.addQuery('request_item' , current.table_sys_id);
gr.query();
//gs.log("row count: "+ gr.getRowCount(),"test");
if(gr.next())
{
var at = new GlideRecord('sys_attachment');
at.addQuery('table_sys_id', gr.sys_id);
at.deleteMultiple();
GlideSysAttachment.copy('sc_req_item', gr.request_item, 'sc_task', gr.sys_id);
}
}
})(current, previous);

1 ACCEPTED SOLUTION

Hi Vishrut

Thanks for response.

I did the same way, wrote BR on SC Task on Insert to copy attachment from RITM To SC Task.

We had 3 scenario:

  • Scenario 1: Copy Attachment from RITM to SC Task when Request is submitted from Portal. 
  • After BR on Insert on sc_task
(function executeRule(current, previous /*null when async*/) {

	GlideSysAttachment.copy('sc_req_item', current.request_item, 'sc_task', current.sys_id);
	gs.info(current.number);

})(current, previous);

 

  • Scenario 2: Copy attachment, when requester update from Portal.
  • After BR on Insert on sys_attachment table. Below code does not work on Submit.

 

(function executeRule(current, previous /*null when async*/) {
	
	updateTasks();
	
	function updateTasks() {
		
		// Add your code here
		var gr = new GlideRecord('sc_task');
		gr.addQuery('request_item' , current.table_sys_id);
		gr.addQuery('x_fru_dynamic_wf_workflow_step', 'b6fb44e9db9ed7001588753a8c9619a2');
		gr.query();
	//	gs.log(current.table_sys_id);
		while(gr.next())
			{
		//		gs.log(gr.number);
			var at = new GlideRecord('sys_attachment');
			at.addQuery('table_sys_id', gr.sys_id);
			at.query();
			if(at.next())
				{
				at.deleteMultiple();
			}
			GlideSysAttachment.copy('sc_req_item', gr.request_item, 'sc_task', gr.sys_id);
		}
		
	}
	
})(current, previous);

 

  • Scenario 3: When eBonded system upload attachment to SC_Task, it should be visible to end user.
  • After BR on Insert on - sys_attachment table.
  • Here need to be careful. Scenario 2 & scenario 3 may go into infinite loop. So I we used integration user in condition to make sure below BR run only if user is integration user.

 

(function executeRule(current, previous /*null when async*/) {
	
	updateTasks();
	
	function updateTasks() {
		
		//var sctaskSysId = current.table_sys_id;
		var gr = new GlideRecord('sc_task');
		gr.addQuery('sys_id', current.table_sys_id);
		gr.addQuery('x_fru_dynamic_wf_workflow_step', 'b6fb44e9db9ed7001588753a8c9619a2');
		gr.query();
		//	gs.log(current.table_sys_id);
		while(gr.next())
			{
			//var itemSysId = gr.request_item;
			var at = new GlideRecord('sys_attachment');
			at.addQuery('table_sys_id', gr.request_item);
			at.query();
			if(at.next())
				{
					at.deleteMultiple();
				}
			//GlideSysAttachment.copy('sc_req_item', , 'sc_task', gr.sys_id);
			GlideSysAttachment.copy('sc_task', gr.sys_id, 'sc_req_item', gr.request_item);
		}
		
	}
	
})(current, previous);

 

Still, I am not happy with GlideSysAttachment.copy method, it copy all attachment from source table to destination. Trying to use attachment API if it is useful.

 

View solution in original post

15 REPLIES 15

Hi Kiran Patil,

 

Can you please tell me what is x_fru_dynamic_wf_workflow_step that you have used in your code for query. Mine is working from your code but I m not getting this query??

 

gr.addQuery('x_fru_dynamic_wf_workflow_step', 'b6fb44e9db9ed7001588753a8c9619a2');

Can I get information about this line that from which table it is taken I'm new to servicenow

Hi @Shambhu   

for me when item submitted from portal, attachment not showing. but when im submitting from native view there it showing attachemnt in sc task what might be the reason, i tried log also when i submitted from portal BR not triggering ie log msg not displaying,

help in this please

Meshia
Kilo Guru

Why write a script or BR when the action is already in flow designer?   This is how mine is set up: 

 

 

Hi Maria,

Looks interesting, could you give a better example on how it relates to creating a catalog task?