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

Ankur Bawiskar
Tera Patron
Tera Patron

@niveditakumari 

ServiceNow has attachment API which would help you.

Refer below links for help

Copying attachment from one instance to another

Copying attachment from one instance to another+

Regards
Ankur

 

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

asifnoor
Kilo Patron

Hello Nivedita,

In the left navigation, look for Rest API Explorer and in that you can check out attachment API.

Use that and it will also provide your script which you can use it as per your requirement.

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.