How to call a DELETE method
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2020 06:34 AM
We have a delete method API which has parameters from variable.How to trigger it form script incude and get the status from workflow.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2020 08:22 AM
Hi,
Use the below code. This example shows the table API on change_request to delete a particular record based on a sys_id.
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://<instance_name>.service-now.com/api/now/table/change_request/c83c5e5347c12200e0ef563dbb9a7190');
request.setHttpMethod('DELETE');
//Eg. UserName="admin", Password="admin" for this code sample.
var user = 'admin';
var password = 'admin';
request.setBasicAuth(user,password);
request.setRequestHeader("Accept","application/json");
var response = request.execute();
gs.log(response.getBody());
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2020 11:12 PM
Tried but not working. Below is my code
var Delete = Class.create();
Delete.prototype = {
initialize: function() {},
Delete: function(current) {
var token = '';
var body;
var apiRes;
var Id = current.request.requested_for.user_name.toString();
var cId = current.request.requested_for.location.u_property_id.toString();
var Number = current.variables.number.toString();
gs.log("Body from REST" );
},
_TF: function() {
var resObj = {};
try {
//GETTING TOKEN
} catch (ex) {
}
try {
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://**Url**/' + Id + '?' + 'Number=%5C%2B' + Number);
request.setHttpMethod('DELETE');
request.setRequestHeader('Authorization', 'Bearer ' + token);
var responses = request.execute();
gs.log(responses.getBody());
} catch (ex) {
var set_message = ex.message;
}
return resObj;
},
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2020 11:28 PM
Hi,
which table you are trying to delete?
does the user has access to delete the record?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2020 11:41 PM
Hi,
Could you please let me know which part of the code is not working? Where are the Id and Number fields defined. It seems that they are in the Delete method. When are you calling the rest api given in the try-catch block?
Thanks