attachment pdf

Avinash Kumar K
Tera Expert

Hi,

I have to move some attachments which is in PDF format, from one instance to another instance. when i tried to move as XML its not working. 

4 REPLIES 4

Riya Verma
Kilo Sage

HI @Avinash Kumar K ,

 

Hope you are doing great.

 

Create a custom script to perform the attachment migration. Reference code using ServiceNow's GlideSysAttachment API:

// 1. Query the attachments from the source instance
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_name', '<source_table_name>');
attachmentGR.addQuery('table_sys_id', '<source_record_sys_id>');
attachmentGR.query();

// 2. Loop through the attachments and copy them to the destination instance
while (attachmentGR.next()) {
  var sourceAttachment = new GlideSysAttachment();
  sourceAttachment.setTableName('<source_table_name>');
  sourceAttachment.setTableSysId('<source_record_sys_id>');
  sourceAttachment.setFile(attachmentGR.getValue('file'));

  // 3. Create a new attachment record in the destination instance
  var destinationAttachment = new GlideSysAttachment();
  destinationAttachment.setTableName('<destination_table_name>');
  destinationAttachment.setTableSysId('<destination_record_sys_id>');
  destinationAttachment.setFileName(attachmentGR.getValue('file_name'));
  destinationAttachment.setContentType(attachmentGR.getValue('content_type'));

  // 4. Copy the attachment file from the source to the destination
  destinationAttachment.write(sourceAttachment.getContentStream());

  gs.info('Attachment ' + attachmentGR.getValue('file_name') + ' moved successfully.');
}

 

Exactly where i have to use this script, please will you assist me

@Avinash Kumar K , you can create a on demand scheduled script and use this script there. You can click on execute now to execute the script.

Hi, 

How this script is working without a connection between two instance, will you expalin me please