Saturday, January 29, 2011

Salesforce REST API Update and Delete Record

So far we have looked at how to create and read records from Salesforce using the REST API, so the final two basic operations we need are Update and Delete.

Update

The code for doing an update is almost identical to the code for doing an insert with just two small differences.
First we need to specify the ID of the record we want to update in the URI just like we did for a read:

var uri = instanceURL + "/services/data/v20.0/sobjects/Account/0017000000hX4Iz";

Second we will use PATCH for the HTTP method instead of POST. Not that PATCH isn’t a standard HTTP method, but fortunately the .NET WebRequest class allows for arbitrary methods.
Just like with an insert you put the JSON or XML encoded object into the body of the request. You can put all the fields in the body or just the ones you want to update.  If you decide to use XML for the body don’t forget to change the ContentType from JSON to XML:

req.ContentType = "application/xml";

The only other difference with an update is the response. Unlike the other function we have looked at so far, the content of the response will be empty if the update succeeds.


Delete

Delete is probably the simplest operation. We use the same URI as an update, one that contains the id of the record we want to delete, and set the HTTP method to DELETE. Nothing needs to be put in the body of the request for a delete. Just like an update, if the delete is successful the response will be empty.

3 comments:

Unknown said...
This comment has been removed by the author.
Unknown said...

Great Post! I think your posts are best introduction of SF Restful Api for .net developer.

Would you please provide some sample about how to use "PATCH" to update Record.

I got an error about http 405 (The method specified in the Request-Line isn't allowed for the resource specified in the URL).

Here is my code:
public class sfContact
{
public string InstallApp__c { get; set; }

}

public string sfContactUpdate(string Id, string installapp)
{
var sfcontactupdate = new SalesForceREST();
sfcontactupdate.GetTokenUsernamePassword();

string updateID = Id;
var uri = sfcontactupdate.token.instance_url + "/services/data/v24.0/sobjects/Contact/"+ updateID;
var cont = new sfContact();
cont.InstallApp__c = installapp;
var ser = new JavaScriptSerializer();
var body = ser.Serialize(cont);
//var accesstoken = sfcontactupdate.token;


System.Net.WebRequest req = System.Net.WebRequest.Create(uri);
req.Headers.Add("Authorization: Bearer " + sfcontactupdate.token.access_token);
req.ContentType = "application/json";
req.Method = "POST";

// Add parameters to post
byte[] data = System.Text.Encoding.ASCII.GetBytes(body);
req.ContentLength = data.Length;
System.IO.Stream os = req.GetRequestStream();
os.Write(data, 0, data.Length);
os.Close();

// Do the post and get the response.
WebResponse resp;

try
{
resp = req.GetResponse();
}
catch(WebException ex)
{
resp = ex.Response;
}

if (resp == null) return null;
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
return sr.ReadToEnd().Trim();
}

}

Unknown said...

Ok, I got it.

Changing req.Method ="POST" to req.Method = "PATCH"