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

I would assume you are using workflow to create all of these sc_tasks.  Its important to know the order of execution, sc_task gets inserted into database prior to sc_req_item.  The sc_req_item gets inserted prior to the sc_request.  What I suspect is happening is this script runs but doesn't have any sc_req_items get to copy attachments from.

 

I would recommend you put this script into a run script in the workflow.  But make sure you put a 5 or so second timer in between so the sc_req_item gets a chance to be committed and it should work.

Hope this makes sense.

Shambhu5
Kilo Guru

Hi Kiran,

Now since you want this to perform (because of no option available) I can help you with a script. I do have a script that worked well on my PDI. It goes as below:

You can write above BR on sc_task table after Insert/Update.

(function executeRule(current, previous /*null when async*/) {
	
	var gr = new GlideRecord("sys_attachment");
	gr.addQuery("table_sys_id",current.request_item);
//	gr.addQuery("file_name", 'CONTAINS', ".txt");
	gr.query();
	if (gr.next()) {
		GlideSysAttachment.copy('sc_req_item', current.request_item, 'sc_task', current.sys_id);
	}
	
	
	
	
})(current, previous);

Please let me know if it worked for you.

Also, mark my answer Correct/helpful in case that worked.

Regards,
Vishrut
find_real_file.png 

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.

 

Hi Kiran,

 

I tried steps in scenario 1, its not working for me. Please guide me.

 

(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);

Hi

Instead of copying the Attachment in sc_task, how can I copy the Hyperlink of attachment in the work note of sc_task??