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.

Please help with copy attachments script.

Lisa Goldman
Kilo Sage

Hello,

I would like to create a UI Action that copies an existing ritm along with all the attachments to a new ritm.  However, I got stuck on copying attachments over to the new ritm. Could somebody please review the following code and provide suggestions on how I can accomplish this task?  Thank you 

 

**This part works**

var getSysId = current.getUniqueValue();
current.setValue('number','');

CreateSysID = current.insert()

 

**This part of the code did not work**
var attach = new GlideSysAttachment();
attach.copy('sc_req_item', current.sys_id, 'sc_req_item', current.sys_id);

2 REPLIES 2

Lisa Goldman
Kilo Sage

Please discard my posted, because I figured it out where I screwed up.

I need to pass in "getSysID" and "CreateSysID"

attach.copy('sc_req_item', getSysId, 'sc_req_item', CreateSysID);

Thank you.

Amit Pandey
Kilo Sage

Hi @Lisa Goldman 

 

Can you try this-

// Create a new RITM
var newRITM = new GlideRecord('sc_req_item');
newRITM.initialize();
newRITM.setValue('number', '');
var newRITMsys_id = newRITM.insert();

// Copy attachments from the original RITM to the new RITM
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_name', 'sc_req_item');
attachmentGR.addQuery('table_sys_id', current.sys_id);
attachmentGR.query();

while (attachmentGR.next()) {
    var attachment = new GlideSysAttachment();
    attachment.copy('sc_req_item', current.sys_id, 'sc_req_item', newRITMsys_id, attachmentGR.getUniqueValue());
}

gs.addInfoMessage('RITM copied successfully along with attachments.');

Please mark my answer helpful and correct.

 

Regards,

Amit