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

Hi randrews,

 

I have already created a related list on the REQ form which shows the attachment on the RITM, the problem with this is that it is not shown in the portal.  I need to be able to have end-users who do not have roles view the attachments on approvals in the portal as they do not use the console.

Any assistance would be greatly appreciated 

benn23
ServiceNow Employee
ServiceNow Employee

You can use a display Business Rule as well.  This will add links to the attachments to the header of the form:

 

Condition:  

!RP.isPopup() && current.request.getRefRecord().hasAttachments()

 

Script:

(function executeRule(current, previous /*null when async*/) {
var link = '';
var fileName = '';
var count = 0;
var fullResult = '';

var att = new GlideRecord('sys_attachment');
att.addQuery('table_name','sc_request');
att.addQuery('table_sys_id',current.request);
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's Attachments:</b><br/>" + fullResult);
}
})(current, previous);

Hi Benn23,

Thanks for your response.  Could you please break down the condition for me? I understand the second half, checking to see whether the request has attachments or not.  But what does 'RP.isPopup()' check?

Thanks

benn23
ServiceNow Employee
ServiceNow Employee

!RP.isPopup() prevents the script from running when a record is viewed via the hover over pop-up.

I have tried adding the business rule but I am not seeing the desired result of copying the attachment over from the RITM to the REQ.  

I have it as a Display rule running on the 'Request [sc_request]' table, as this is the ticket without the attachment.

and I have amended the line 'att.addQuery('table_name',sc_request');  to 'att.addQuery('table_name',sc_req_item'); as this is where the attachment is added to after submitting the request.

Is this correct?