send attachment from an instance to another

MohamedAliE
Tera Contributor

in my BR : i wrote this : 

function sendAttachment(targetSysId) {
    var grAttach = new GlideRecord('sys_attachment');
    grAttach.addQuery('table_sys_id', current.sys_id);
    grAttach.query();

    while (grAttach.next()) {
        var fileName = grAttach.file_name.toString();
        var contentType = grAttach.content_type.toString() || 'application/octet-stream';

        var binData = new GlideSysAttachment().getBytes(grAttach);

        try {
            var rm = new sn_ws.RESTMessageV2('Upload Attachment to LVER DEV', 'POST');

            // Endpoint avec sys_id cible et nom de fichier
            var endpoint = rm.getEndpoint()
                .replace('${table_name}', 'incident')
                .replace('${table_sys_id}', targetSysId)
                .replace('${file_name}', encodeURIComponent(fileName));
           
            rm.setEndpoint(endpoint);
            rm.setHttpTimeout(60000);

            // Le binaire directement dans le body
            rm.setRequestHeader("Content-Type", contentType);
            rm.setRequestHeader("Accept", "application/json");
            rm.setRequestBody(binData);

            var resp = rm.execute();
            var body = resp.getBody();
            var status = resp.getStatusCode();

            gs.log("Attachment sent: " + fileName + " | MIME type: " + contentType + " | Status: " + status + " | Response: " + body);
        } catch (e) {
            gs.log("Error sending attachment " + fileName + ": " + e.message);
        }
    }
}
 
but in my instance cible the file are truncated 😞 
1 ACCEPTED SOLUTION

MohamedAliE
Tera Contributor

i found a solution it seems work : 

 

function sendAttachment(targetSysId) {
    var sa = new GlideSysAttachment();

    // 1️⃣ Supprimer les anciens attachments sur l'incident cible (to update docs)
    var grTargetAttach = new GlideRecord('sys_attachment');
    grTargetAttach.addQuery('table_sys_id', targetSysId);
    grTargetAttach.query();
    while (grTargetAttach.next()) {
        sa.deleteAttachment(grTargetAttach.sys_id);
        gs.log("Deleted old attachment on target: " + grTargetAttach.file_name);
    }

    // 2️⃣ Parcours des attachments source et envoi
    var grSourceAttach = new GlideRecord('sys_attachment');
    grSourceAttach.addQuery('table_sys_id', current.sys_id);
    grSourceAttach.query();
    while (grSourceAttach.next()) {
        var fileName = grSourceAttach.file_name.toString();
        var contentType = grSourceAttach.content_type.toString() || "application/octet-stream";

        var rm = new sn_ws.RESTMessageV2('Upload Attachment to LVER DEV', 'POST');
        rm.setEndpoint(rm.getEndpoint()
            .replace('${table_name}', 'incident')
            .replace('${table_sys_id}', targetSysId)
            .replace('${file_name}', encodeURIComponent(fileName))
        );
        rm.setRequestHeader("Content-Type", contentType);
        rm.setRequestHeader("Accept", "application/json");
        rm.setRequestBodyFromAttachment(grSourceAttach.sys_id);

        var resp = rm.execute();
        gs.log("Attachment sent: " + fileName + " | Status: " + resp.getStatusCode() + " | Response: " + resp.getBody());
    }
}
 
thank you

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@MohamedAliE 

you want to transfer files between instances right?

if yes then check these links

Unable to send attachment via REST 

Business rule to send attachment to 3rd Party Tool via REST 

Send attachment via rest message 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

MohamedAliE
Tera Contributor

i found a solution it seems work : 

 

function sendAttachment(targetSysId) {
    var sa = new GlideSysAttachment();

    // 1️⃣ Supprimer les anciens attachments sur l'incident cible (to update docs)
    var grTargetAttach = new GlideRecord('sys_attachment');
    grTargetAttach.addQuery('table_sys_id', targetSysId);
    grTargetAttach.query();
    while (grTargetAttach.next()) {
        sa.deleteAttachment(grTargetAttach.sys_id);
        gs.log("Deleted old attachment on target: " + grTargetAttach.file_name);
    }

    // 2️⃣ Parcours des attachments source et envoi
    var grSourceAttach = new GlideRecord('sys_attachment');
    grSourceAttach.addQuery('table_sys_id', current.sys_id);
    grSourceAttach.query();
    while (grSourceAttach.next()) {
        var fileName = grSourceAttach.file_name.toString();
        var contentType = grSourceAttach.content_type.toString() || "application/octet-stream";

        var rm = new sn_ws.RESTMessageV2('Upload Attachment to LVER DEV', 'POST');
        rm.setEndpoint(rm.getEndpoint()
            .replace('${table_name}', 'incident')
            .replace('${table_sys_id}', targetSysId)
            .replace('${file_name}', encodeURIComponent(fileName))
        );
        rm.setRequestHeader("Content-Type", contentType);
        rm.setRequestHeader("Accept", "application/json");
        rm.setRequestBodyFromAttachment(grSourceAttach.sys_id);

        var resp = rm.execute();
        gs.log("Attachment sent: " + fileName + " | Status: " + resp.getStatusCode() + " | Response: " + resp.getBody());
    }
}
 
thank you