Migrate Multiple Attachments via REST Message using the Attachment API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2024 12:13 AM
Hi All,
I've been trying to Migrate a large number of attachments (40k+) from one instance to another using the Attachment API. I have configured a REST Message in the source instance and triggering the REST Message from a while loop in an on-demand scheduled job, but the loop is not executing more that once. Even tried gs.sleep(1000) which is not looping more than 30 times.
Here is the script that I used
// Create a GlideRecord object "getAttachment" for the 'sys_attachment' table
var getAttachment = new GlideRecord('sys_attachment');
// Filter records in the 'getAttachment' GlideRecord based on the source table
getAttachment.addEncodedQuery('table_name=source_table');
// Execute the query to retrieve matching records
getAttachment.query();
// Create a RESTMessageV2 object "restRequest" to perform a REST API call
var restRequest = new sn_ws.RESTMessageV2('Attachment Migration', 'POST Attachment');
// Iterate through each record in the 'getAttachment' GlideRecord
while (getAttachment.next()) {
// Set query parameters for the REST API call
restRequest.setQueryParameter('table_name', 'target_table');
restRequest.setQueryParameter('table_sys_id', getAttachment.table_sys_id);
restRequest.setQueryParameter("file_name", getAttachment.file_name);
// Set request headers for the REST API call
restRequest.setRequestHeader("Content-Type", getAttachment.content_type.toString());
restRequest.setRequestHeader("Accept", "application/json");
// Set the request body using the attachment sys_id
restRequest.setRequestBodyFromAttachment(getAttachment.sys_id);
// Execute the REST API call
var attachmentResponse = restRequest.execute();
// Get the HTTP status code of the response
var attachmentStatus = attachmentResponse.getStatusCode();
gs.info('Attachment status: ' + attachmentStatus);
// Get the body of the response
var attachmentResult = attachmentResponse.getBody();
gs.info('Attachment response body: ' + attachmentResult);
gs.sleep(1000);
}