Moving Attachments to a different ServiceNow instance

TomWright_Dos
Tera Contributor

I had a case were I needed to move Case back to OOB.  I exported all of the case records, made my changes and imported the case records into their respective case type table.  So now I need re-attach the attachments.  As part of the import, I moved the old case number into the correlation id field on the imported case record. 

In our test instance (a clone of PRD prior to moving case to OOB), I created a field on the sys_attachment table called u_correlation_id and ran a script that would do a lookup against the case table and record (based on the table_name and table_sys_id) and move the case number into the u_correlation_id field.  I then wrote a script that, using REST post call, would copy over the attachments to other instance. The problem I am seeing is that, while the attachment is getting created on the other instance, it is not bringing over the value in the u_correlation_id field.

 

function sendAttachment (sourceTable, targetInstanceURL){
    var answer = [0,0];
    var attachRec = new GlideRecord('sys_attachment');
    attachRec.setLimit(10);
    attachRec.addQuery('table_name', sourceTable);
    attachRec.query();
    if(attachRec.hasNext()){
        while (attachRec.next()){
            var attachMessage = new sn_ws.RESTMessageV2();
            attachMessage.setLogLevel('all');
            attachMessage.setHttpMethod("post");
            attachMessage.setEndpoint(targetInstanceURL+"/api/now/attachment/file");
            attachMessage.setQueryParameter("table_name", attachRec.table_name);
            attachMessage.setQueryParameter("file_name", attachRec.file_name);
            attachMessage.setQueryParameter('table_sys_id', attachRec.table_sys_id);
            attachMessage.setQueryParameter('u_correlation_id', attachRec.u_correlation_id);
            attachMessage.setRequestHeader("Content-Type", attachRec.content_type);
            attachMessage.setRequestHeader("Accept", "application/json");
            attachMessage.setRequestBodyFromAttachment(attachRec.sys_id);

            var response = attachMessage.execute();

            var responseBody = response.getBody();
            gs.info('Response Body: ' + responseBody);
            var httpStatus = response.getStatusCode();

            if (httpStatus.toString() == "201"){
                answer[0] += 1;
            }else{
                answer[1] += 1;
            }
        }
    } else {
        answer = "none";
    }
    gs.info("Number of attachments sent over to target instance: " + answer[0] + " number of attachments not sent over: " + answer[1]);

}

  

6 REPLIES 6

Hi @TomWright_Dos 

Great, happy to hear your issue is resolved. Could you please mark my answer as correct and helpful then. 

 

Thank you

 

 

@Rajesh Chopade1 

 

Hi Rajesh, i am sending attachment to 3rd party application through Outbound REST API. 3rd Party app endpoint is expecting 'input_file' with attachment but I am not sure where to put 'input_file' in REST Message. Below is my BR. Could you please take a look ?

Appreciate your response.

 

 

var resultGr = new GlideRecord("sn_customerservice_case");
resultGr.addQuery("sys_id", current.table_sys_id);
resultGr.addQuery("account", '<account sys id>'); // running for specific account
resultGr.query();
if (resultGr.next()) {
var caseid = resultGr.sys_id.toString();
gs.info("Case sys id : " + caseid);
try {
var r = new sn_ws.RESTMessageV2('New Integration', 'UploadAttachment');
r.setRequestBodyFromAttachment(current.sys_id);
r.setRequestHeader("Content-Type", resultGr.content_type);
r.setStringParameterNoEscape('request_id', resultGr.correlation_id);

 

var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.info("The value of the POST Response is " + httpStatus + "Body : " + responseBody);
} catch (ex) {
var message = ex.message;
}
}

 

Screenshot 2025-03-05 220953.png