copy attachments from request to RITM

kunal16
Tera Expert

Hi,

I have a requirement to copy all the attachments from Request to the corresponding RITMs.

I have written an After-Insert Business rule on the Request table to achieve the same. But it seems the code is not working as expected.

Name: Copy Attachments to RITM

Table: Request (sc_request)

When to run: After - Insert

Script:

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

  // Add your code here

  var ritm = new GlideRecord('sc_req_item');

  ritm.addQuery('request', current.sys_id);

  ritm.query();

  while(ritm.next())

  {

  GlideSysAttachment.copy('sc_request', current.sys_id, 'sc_req_item', ritm.sys_id);

  }

})(current, previous);

Any lead will be appreciated. Thanks in advance!!

22 REPLIES 22

benn23
ServiceNow Employee
ServiceNow Employee

Oh I thought you were trying to pull the attachments from the Request to the Item.

You'll have to get rid of the 2nd part of the condition (current.request.getRefRecord().hasAttachments()).

(function executeRule(current, previous /*null when async*/) {
var link = '';
var fileName = '';
var count = 0;
var fullResult = '';
var gr = new GlideRecord('sc_req_item');
gr.addEncodedQuery('request=' + current.sys_id);
gr.query();
while(gr.next()){
var att = new GlideRecord('sys_attachment');
att.addQuery('table_name','sc_req_item');
att.addQuery('table_sys_id',gr.sys_id.toString());
att.query();
while(att.next()){
if(count > 0){
fullResult += '<br/>';
}
link = 'sys_attachment.do?sys_id=' + att.sys_id;
fileName = att.file_name;
fullResult += '<a href="' + link + '">' + fileName + '</a>';
count++;
}
}

if(link != ''){
gs.addInfoMessage("<b>Request Item's Attachments:</b><br/>" + fullResult);
}
})(current, previous);

Thank you so much for your help, that worked perfectly.

I have tried to amend the code so that it runs on the catalog task as well and displays the attachment in the header but it just seems to hang the system when I access a catalog item.  I had updated the EncodedQuery, but I'm guessing that isn't right.  Could you please let me know what to amend?

 

Display BR on sc_task

(function executeRule(current, previous /*null when async*/) {
	
	var link = "";
	var fileName = "";
	var count = 0;
	var fullResult = "";
	
	var gr = new GlideRecord('sc_req_item');
	gr.addEncodedQuery('request_item=' +current.sys_id);
	gr.query();
	while(gr.next()){
		
		var att = new GlideRecord('sys_attachment');
		att.addQuery('table_name','sc_req_item');
		att.addQuery('table_sys_id',gr.sys_id.toString());
		att.query();
		while(att.next()){
			if(count>0){
				fullResult += '<br/>';
			}
			link = 'sys_attachment.do?sys_id='+att.sys_id;
			fileName = att.file_name;
			fullResult += '<a href="' + link + '">'+ fileName + '</a>';
			count++;
		}
	}
	if(link!=""){
		gs.addInfoMessage("<b>Attachments on RITM:</b><br/>" +fullResult);
	}
	
})(current, previous);

benn23
ServiceNow Employee
ServiceNow Employee

From sc_task:

Condition: !RP.isPopup() && current.request_item.getRefRecord().hasAttachments()

Script:

(function executeRule(current, previous /*null when async*/) {
//Display a note on the catalog task with links to attachments in parent request item
var link = '';
var fileName = '';
var count = 0;
var fullResult = '';

var att = new GlideRecord('sys_attachment');
att.addQuery('table_name','sc_req_item');
att.addQuery('table_sys_id',current.request_item);
att.query();
while(att.next()){
if(count > 0){
fullResult += '<br/>';
}
link = 'sys_attachment.do?sys_id=' + att.sys_id;
fileName = att.file_name;
fullResult += '<a href="' + link + '">' + fileName + '</a>';
count++;
}

if(link != ''){
gs.addInfoMessage("<b>Item's Attachments:</b><br/>" + fullResult);
}
})(current, previous);