how can give attachment at time of incident creation in REST API.

Kishore8
Kilo Guru

I want to give attachment to new incident creation from REST API.

1 ACCEPTED SOLUTION

JennyHu
Tera Guru
Tera Guru

Please see Attachment API - POST /now/attachment/file.   It has a good example on how to use the Attachment API to post an attachment to an incident record.



You do need to know the sysID of the record that you want insert the attachment to.   So if I would to code this, I would first use the Table API POST /now/table/incident to insert the Incident record.   On successful Incident record creation, get the sysID from the JSON response body.   Then, use the Attachment API POST /now/attachment/file.



Thanks,


Jenny


View solution in original post

4 REPLIES 4

Alikutty A
Tera Sage

Attachment is a different table, so you need to send a second request once your incident is created.



You can upload the required attachment using the attachment upload Rest API



Thank You


Please Hit Like, Helpful or Correct depending on the impact of response


JennyHu
Tera Guru
Tera Guru

Please see Attachment API - POST /now/attachment/file.   It has a good example on how to use the Attachment API to post an attachment to an incident record.



You do need to know the sysID of the record that you want insert the attachment to.   So if I would to code this, I would first use the Table API POST /now/table/incident to insert the Incident record.   On successful Incident record creation, get the sysID from the JSON response body.   Then, use the Attachment API POST /now/attachment/file.



Thanks,


Jenny


i have done  same thing here, Can you suggets me where i am wrong in this procedure.

i have created POST /now/table/incident, successfully incident created in Target Instance from Source Instance..

 

i have done below things, For  POST /now/attachment/file.

Business Rule:

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

    repeatAttach();
    function repeatAttach(){
        var grAttach= new GlideRecord('sys_attachment');
        grAttach.addQuery('table_name','IN','incident');
        grAttach.orderByDesc('file_name');
        grAttach.setLimit(1);
        grAttach.query();
        while(grAttach.next())
            {
                var attachmentID=grAttach.sys_id;
                var table_name=grAttach.table_name;
                var table_sys_id=grAttach.table_sys_id;
                var file_name=grAttach.file_name;
                var content_type=grAttach.content_type;
                
                sendAttachment(attachmentID,table_name,table_sys_id,file_name,content_type);
                
            }
    }
    
function sendAttachment(attachmentID,table_name,table_sys_id,file_name,content_type)
    {
try { 
 var r = new sn_ws.RESTMessageV2('Add Attachment', 'POST attachment');
 r.setStringParameterNoEscape('table_name', table_name);
 r.setStringParameterNoEscape('file_name', file_name);
 r.setStringParameterNoEscape('content_type', content_type);
 r.setStringParameterNoEscape('table_sys_id', table_sys_id);
 r.setRequestBodyFromAttachment(attachmentID);
r.setRequestHeader("Content_Type",content_type);
    

 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
    gs.info("Status:" +httpStatus+ ' - '+ responseBody);
}
catch(ex) {
 var message = ex.getMessage();
}

}
})(current, previous);