How to delete a record from a ServiceNow table through scripted REST API?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2022 09:39 AM
Scenario: There is a table name "x_test", in this table i need to delete a record through JSON request in "Request body"
Payload:
{"name": "xyz"}
is there any way to achieve this solution?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2022 09:57 AM
Hi,
On Scripted REST API use DELETE HTTP method.
Make the script based on your requirement that is when you need to perform delete operation.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2022 10:04 AM
Hello,
I am adding little elaborative answer, I have pasted below code you need to write down code on similar lines.
Note: Instead of passing name in payload you can pass it as path parameter.
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var name = request.pathParams.name || null;
if (!name)
return new sn_ws_err.BadRequestError("Change Request id not provided");
var result = {};
var record = new GlideRecord('x_test');
if(record.get('name', name)){
if (!record.deleteRecord())
return new sn_ws_err.BadRequestError("Change Request not deleted");
else{
result.status = 'success';
response.setStatus(200);
response.setBody(result);
}
}else
return new sn_ws_err.NotFoundError("Change Request not found");
})(request, response);
Step1: Create a scripted REST API with proper name (e.g. Test Table APIs )
Step 2: Create a DELETE scripted REST Resource as shown in below screenshot