How to delete multiple records in servicenow table using REST API

Gokulendra Pand
Giga Contributor

How to delete multiple records in servicenow table using REST API

6 REPLIES 6

Hemant Goldar
Mega Sage
Mega Sage

Hi Gokulendra,

please find the link of the article https://community.servicenow.com/community?id=community_article&sys_id=66c5d6c61b03b748ada243f6fe4bc... it will help you.

 

Please mark correct if it solves your problem.

 

Thanks 

Hemant 

Gokulendra Pand
Giga Contributor

Ok Thanks

please mark my answer correct and helpful.

Service_RNow
Mega Sage

Hello,

When we integrate ServiceNow with other 3rd party systems, below are some of the common functionality that you do it

  • Fetch (List)
  • Create/Add
  • Update/Modify
  • Delete

In this article, I will show you the code on how you can do bulk delete through API. 

As you aware, ServiceNow REST API allows you to delete 1 record at a time. So in order to delete bulk records, one way is to fetch all the records that you want to delete, loop through it and call delete API one by one. Below is the sample code snippet that allows you to do this.

var request = new sn_ws.RESTMessageV2();
//modify the end point as per your requirement. i just queried incidents whose assignment group is servicedesk. you can change as per your requirement.
request.setEndpoint('https://your-instance.service-now.com/api/now/table/incident?sysparm_query=assignment_group%3Dd625dccec0a8016700a222a0f7900d06&sysparm_fields=number%2Cshort_description%2Csys_id');
request.setHttpMethod('GET');

//UPDATE AS PER YOUR INSTANCE CREDENTIALS BELOW.
var user = 'admin';
var password = 'admin';

request.setBasicAuth(user,password);
request.setRequestHeader("Accept","application/json");

var response = request.execute();
var result = response.getBody();

var result_json = JSON.parse(result);
//now loop through the results and call delete API
for(i=0;i<result_json.result.length;i++) {
  //call delete API
  request.setEndpoint('https://your-instance.service-now.com/api/now/table/incident/'+result_json.result[i].sys_id); //change end point as per your table
  request.setHttpMethod('DELETE');
  request.setRequestHeader("Accept","application/json");
  response = request.execute();
  gs.info("Record is deleted successfully"+result_json.result[i].number);
}

If you have any questions, let me know in the comments section.

Mark the article as helpful and bookmark if it finds useful.