Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Salesforce REST API and the DELETE method

I'm using Adobe AIR and integrating with the force.com platform via the REST API, and so far it's been relatively smooth sailing, but I'm coming unstuck on using the DELETE method.

The documentation is simple enough:

Deleting an Account Record

Use the DELETE method to delete a record. In this example, an Account record is deleted.

Example usage for deleting fields in an Account object

curl https://instancename.salesforce.com/services/data/v20.0/sobjects/Account/001D000000INjVe -H "Authorization: Bearer token" -H "X-PrettyPrint:1" -X DELETE

Example request body for deleting an Account record

none required

Example response body for deleting an Account record

none returned

My code is below, note that the second parameter of HTTPConnection.send() is the method to call.

var headers:Object = new Object();
headers["Authorization"] = "Bearer "+ConnectionAccessToken;
var url:String = ConnectionInstanceURL + "/services/data/v"+_apiVersionNumber+"/sobjects/"+type+"/"+id;

var response:RESTResponse = new RESTResponse(callback);
var httpCallback:IResponder = new mx.rpc.Responder(response.resultHandler,response.faultHandler);

HTTPConnection.send(headers,"DELETE",url,httpCallback);

Similar code works perfectly for other operations, and the weird thing is that this doesn't fail per se, rather it receives a success response, but gets the record in question back with all of it's fields. It would appear that I'm seeing the results of [select * from Object where Id = <id>, and just to clarify the record is not deleted. The object doesn't have any master detail relationships, so I'm not sure what else might be stopping this delete from happening — has anyone run into this before or have suggestions on how to resolve it?

like image 305
Matt Lacey Avatar asked Jun 29 '26 02:06

Matt Lacey


1 Answers

With the setup I am using (where HTTPConnection is a custom class using HTTPService internally), the Adobe documentation states that the only HTTP methods available for me to use are just GET and POST:

"Optionally, you can pass parameters to the specified URL. When you do not go through the server-based proxy service, you can use only HTTP GET or POST methods. However, when you set the useProxy property to true and you use the server-based proxy service, you can also use the HTTP HEAD, OPTIONS, TRACE, and DELETE methods."

This is why the delete was failing as it must have been sending as a GET instead as per jkraybill's comment above. After experimenting I have found that POST can be used, with the actual method to call included as a parameter to the URL:

HTTPConnection.send(headers,"POST",url + "?_HttpMethod=DELETE",httpCallback);
like image 172
Matt Lacey Avatar answered Jun 30 '26 16:06

Matt Lacey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!