Need to create attachment with same sys_id in target instance

amit71
Tera Contributor

I am trying to create attachment with same sys_id in target instance as of source instance using Attachment API but getting error, Please suggest any method how we can achieve this requirement.

 

BR on Kb_knowledge table

After Update

 

(function executeRule(current, previous /*null when async*/ ) {

    try {
        gs.info("kb_attachment_ebond");
        var attach = new GlideRecord("sys_attachment");
        attach.addQuery("table_sys_id", current.sys_id);
        attach.addQuery("table_name", "kb_knowledge");
        attach.query();

        while (attach.next()) {
            var fileName = attach.file_name.getValue();
            var tablesysid = current.u_correlation_display.getValue();
            var attachmentsysid = attach.sys_id.getValue();

            var attachment = new sn_ws.RESTMessageV2();
            attachment.setHttpMethod('POST');
            var username = "XXXXX";
            var password = "XXXXX";
            attachment.setBasicAuth(username, password);
            attachment.setEndpoint("https://devXXXXX.service-now.com/api/now/attachment/file");
            attachment.setRequestHeader("Accept", "application/json, text/plain, */*");
            attachment.setRequestHeader('Content-Type', 'image/png');
            attachment.setQueryParameter("file_name", fileName);
            attachment.setQueryParameter("table_name", "kb_knowledge");
            attachment.setQueryParameter("table_sys_id", tablesysid);
            attachment.setRequestBodyFromAttachment(attachmentsysid);

            var response = attachment.execute();
            var requestBody = response.getRequestBody();
            var responseBody = response.getBody();
            var httpStatus = response.getStatusCode();

            gs.info("check_status: " + httpStatus);
            gs.log("kb_e-bondingattachment_request" + requestBody);
            gs.log("kb_e-bondingattachment_response" + responseBody);
        }
    } catch (ex) {
        var message = ex.message;
    }
})(current, previous);

 

 

1 REPLY 1

Sumanth16
Kilo Patron

Hi @amit71 ,

 

Please try below approach:

 

https://www.servicenow.com/community/developer-forum/copying-attachment-from-one-instance-to-another...

 

https://www.servicenow.com/community/developer-forum/copying-attachment-from-one-instance-to-another...

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda