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

Neeraj Sharma10
Tera Guru

Hello Experts,

How can we configure Outbound Rest message to send attachment to 3rd party(cloudplus) which later can be used in business rule?

Or is there other way of writing business rule for the same.

Do we need to encode it into Base64 before sending?

I am bit confused.
Any Help would be appreciated.

Thanks

Neeraj Sharma

1 ACCEPTED SOLUTION

Neeraj Sharma10
Tera Guru

finally i am able   to successfully send   attachment to 3rd party and viceversa via rest.



Points to Remember.



Sending attachments   (serviceNow to 3rd party)


1. Outbound Rest message- Need to ask for a endpoint of the 3rd party. Configure HTTP headers If any.


2. Use this Rest message in the business rule to send attachments


                  a. Call the rest message and simply send the binary content of the attachment to the request body using function request.setRequestBodyFromAttachment('<attachment sys_id>');


(RESTMessageV2 - setRequestBodyFromAttachment(String attachmentSysId)   )


  Screenshot of the business rule



S1.png



And you will see Magic happens.



Receiving attachments (3rd party to serviceNow)


1. Go Rest API explorer > select Attachment API in API name and explore all the APIs available (Get, Post, Delete)


For are two ways of making Post request :-


a) https://yourinstance.service-now.com/api/now/attachment/file


b) https://dev33253.service-now.com/api/now/attachment/upload



table_name = Table name ( incident for above case)


table_sys_id = Unique Id of the record to which attachment needs to be inserted.


file_name = name of the attachment.


In HTTP header you need to send Content-Type Also (Not mandatory but always good to give).



Postman Screenshots


acattach.png


S1.png




Hope this will be helpful.



Thanks


Neeraj sharma


View solution in original post

32 REPLIES 32

Hi Neeraj,

 

I've setup the ServiceNow to JIRA integration successfully using the John Andersen ServiceNow-> JIRA integration documentation 

http://www.john-james-andersen.com/blog/service-now/rest-based-servicenow-jira-integration-poc.html

But only thing is I wasn't able to is transfer attachments over to JIRA from ServiceNow, I've tried to use your business rule but I am not getting anything in response body log, Here is my Business Rule

Business Rule:

 

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

attach = current.sys_id;
name = current.file_name;
var r = new sn_ws.RESTMessageV2('REST API Name', 'Attchments');
r.setStringParameterNoEscape('tokenid','JIRA Token');
r.setStringParameterNoEscape('fileName',name);
r.setRequestBodyFromAttachment(attach);
var response= r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log('Check attachment:'+responseBody);

})(current, previous);

 

I'm not doing something right here but I couldn't able to what I'm missing here. Could you please help me in figure out that?

Thanks,

Chaitanya

How can I send and attachment If I need to specify in a form of Key value pair, like example bellow:

var payload = { "attachment[]" : file }

 

In Postman I can set the file from my machine, but, How can I have the same treatment on Servicenow?

MohamedAliE
Kilo Expert

thank u All i have been resolved it, by using this code bellow : 

 

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();
        if (!contentType) {
            contentType = "application/octet-stream"; // fallback si vide
        }

        // Création du RESTMessage
        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");

        // ✅ Ici on envoie directement depuis l'attachement source
        rm.setRequestBodyFromAttachment(grAttach.sys_id);

        // Execution
        var resp = rm.execute();
        gs.log("Attachment sent via setRequestBodyFromAttachment: "
            + fileName + " | MIME type: " + contentType
            + " | Status: " + resp.getStatusCode()
            + " | Response: " + resp.getBody());
    }
}
 
 

nad this URI like EndPoint for my  rest api 

https://lavieenrosedev.service-now.com/api/now/attachment/file?table_name=${table_name}&table_sys_id...}