Can you tell me how to add attachment in incident when we create incident in one instance.

niveditakumari
Mega Sage

Hello Team,

 

Can you tell me how to add attachment in incident when we create incident in one instance and send it to other instance with rest api.

Please help me with this.

 

Regards,

Nivedita

 

 

1 ACCEPTED SOLUTION

Hi Nivedita,

here is the sample code that you can put in your BR of incident which will help you to push the attachment along with the incident to another instance.

(function executeRule(current, previous /*null when async*/ ) {
    var body = {
        "short_description": current.getValue("short_description"),
        "description": current.getValue("description"),
        "urgency": current.getValue("urgency"),
        "impact": current.getValue("impact"),
        "state": current.getValue("state")
    };
    try {
        var r = new sn_ws.RESTMessageV2('Create_Incident', 'Create Incident');
        r.setRequestBody(JSON.stringify(body));
        var response = r.execute();
        var responseBody = response.getBody();
        var json = JSON.parse(responseBody);
	var remoteInc_sys_id=json.result.sys_id;
        var httpStatus = response.getStatusCode();
        gs.log("Response status code is " + httpStatus);
        if (httpStatus == "201") {
            gs.log("Entered into the if conditon");
            //Fetch attachment associated with the incident
            var att = new GlideRecord("sys_attachment");
            att.addQuery("table_sys_id", current.sys_id);
            att.query();
            //if you want to push all attachmetns, use while loop here
            if (att.next()) {
                var attachmentMessage = new sn_ws.RESTMessageV2();
                attachmentMessage.setHttpMethod("post");
                attachmentMessage.setBasicAuth("username", "password");
                attachmentMessage.setEndpoint("https://devxxxxx.service-now.com/api/now/attachment/file");
                attachmentMessage.setQueryParameter("table_name", att.table_name);
                attachmentMessage.setQueryParameter("table_sys_id",remoteInc_sys_id);
                attachmentMessage.setQueryParameter("file_name", att.file_name);
                attachmentMessage.setRequestHeader("Content-Type", att.content_type);
                attachmentMessage.setRequestHeader("Accept", "application/json");
                attachmentMessage.setRequestBodyFromAttachment(att.sys_id);
                response = attachmentMessage.execute();
                responseBody = response.getBody();
                httpStatus = response.getStatusCode();
            }
        }
    } catch (ex) {
        var message = ex.message;
    }
})(current, previous);

Mark the comment as a correct answer and also helpful once worked.

View solution in original post

7 REPLIES 7

Maik Skoddow
Tera Patron
Tera Patron

niveditakumari
Mega Sage

Hi Maik,

 

I have used rest api to configure my integration between two service now instances.

Can you please tell me steps how can i implement attachment functionality in rest api to send attachment also while sending incident details.

 

Regards,

Nivedita

 

 

Hi Nivedita,

in case you don't want to or cannot use OOTB solutions you will have to implement a lot.

Plaese see similar questions for example: https://community.servicenow.com/community?id=community_question&sys_id=abfa0baddb5cdbc01dcaf3231f96...

Kind regards
Maik

If my answer replied your question please mark appropriate response as correct so that the question will appear as resolved for other users who may have a similar question in the future.

use the attachmenn API. fastest and Easy way. Only thing neeed is target system Sys ID which I am sure you are anyway storing when bondnig Incidents.

 

Once the correlation ID(target sys id) is available use BR to triger rest message using Attachment API

 

Let me know if you need a sample Attachment Rest Message