How to delete a record from a ServiceNow table through scripted REST API?

Not applicable

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?

6 REPLIES 6

Pavankumar_1
Mega Patron

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

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Sameer18
Tera Contributor

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 )

find_real_file.png

Step 2: Create a DELETE scripted REST Resource  as shown in below screenshot

find_real_file.png