{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":175,"path":"api-reference/api-reference/r_RMV2-saveRespBodyAsAttach_S_S_S-1.md","title":"RESTMessageV2 - saveResponseBodyAsAttachment\\(String tableName, String recordSysId, String fileName\\)","bundle":null,"doc_type":null,"release":"australia","body":"\n# RESTMessageV2 - saveResponseBodyAsAttachment\\(String tableName, String recordSysId, String fileName\\)\n\nConfigures the REST message to save the returned response body as an attachment record.\n\nWhen you use this function with a REST message that is sent through a MID server, the MID server user must have any roles required to read and write attachment records, as well as any roles required to read and write records on the table specified in the **tableName** parameter.\n\nThe response body does not need to be a binary file to be saved as an attachment. Response bodies using text formats, such as JSON or XML can also be saved. If the instance fails to save the attachment, call getErrorMessage\\(\\) on the related RESTResponseV2 object for error details.\n\n|Name|Type|Description|\n|----|----|-----------|\n|tableName|String|Specify the table that contains the record you want to attach the saved file to.|\n|recordSysId|String|Specify the sys\\_id of the record you want to attach the saved file to.|\n|fileName|String|Specify the file name to give to the saved file.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\n(function sampleRESTMessageV2() {\n  try{\n    var request  = new sn_ws.RESTMessageV2();        \n    request.setHttpMethod('get');\n\n    var attachment_sys_id  = '<attachment_record_sys_id>', \n      tablename = 'incident',\n      recordSysId = '<incident_sys_id>',            \n      response,            \n      httpResponseStatus,             \n      filename ='<filename>';\n\n    //endpoint - ServiceNow REST Attachment API        \n    request.setEndpoint('https://<instance_name>.service-now.com/api/now/attachment/' + attachment_sys_id  +'/file');        \n    request.setBasicAuth('<username>', '<password>');\n\n    //RESTMessageV2 - saveResponseBodyAsAttachment(String tableName, String recordSysId, String fileName)        \n    request.saveResponseBodyAsAttachment(tablename, recordSysId, filename);        \n\n    response = request.execute();        \n    httpResponseStatus = response.getStatusCode();  \n      \n    gs.info(\" http response status_code:  \" + httpResponseStatus);    \n  }\n  catch(ex){\n    var message  = ex.getMessage();        \n    gs.info(message);    \n  }\n})();\n```\n\n"}}