Sometimes throughout the course of your work you find something that just makes you wonder what the heck the developers were thinking. Recently that was me and working with the SalesForce Web Service API. So far there have been quite a few challenging things in working with this API that were new and strange to me… a big one being when you query for objects, you get an object back that has all the properties available, but if you didn’t explicitly put the field you need in the SELECT statement, the SalesForce Web Service would still return the field in the return result, but it just wouldn’t be filled in. Uh… if i don’t select it, then don’t give it to me… anyway… that was relatively minor until recently.
Then I ran into something that just drove me nuts. Let’s say you want to update a record, but you first want to give the user the ability to see what’s in it. So what’s the flow? In every API I’ve worked with the process is:
- Query & select item.
- Show item’s properties.
- User updates fields.
- You send item back to service for updating.
Pretty logical right? What if you wanted to clear out a field back to its default “empty” or null state? Well, just set it to string.empty or null… right? Nah… not if you’re the SalesForce API designers!
You have to explicitly tell the SalesForce Web Service what fields you want to null out like this:
Contact sfContact = // code to load a contact object from SalesForce
sfContact.FirstName = "John";
sfContact.LastName = "Doe";
sfContact.Phone = ""; // sure would make sense doing this right? nope!
sfContact.fieldsToNull = new string[] { "Phone" }; // this is how you clear the Phone property... ugh!
// call Web Service update method
Now that’s just strange to me. But it gets even better. Say you follow the process above where you get the object, update some fields and then want to clear out the phone number. Well, if you originally selected the Phone property & then you try to set it to null, SalesForce throws an error (in the new v8.0 Web Service API). Why? Because you sent back two instructions for the Phone property: (1) update it’s value and (2) set it to null.
So how to do you get around it? I give you my own person daily WTF: you have to send a separate object to tell the Phone to be set to null:
Contact sfContact = // code to load a contact object from SalesForce
sfContact.FirstName = "John";
sfContact.LastName = "Doe";// call Web Service update methodContact
sfContact_ResetFields = new Contact();
sfContact_ResetFields.Id = sfContact.Id;
sfContact_ResetFields.fieldsToNull = new string[] { "Phone" };
// call Web Service update method
And I give you dumb! Of course the update method can take an array of objects to update so you don’t make two round trips, but that’s just stupid if you ask me. Think I’m making this up or don’t understand the API? Guess again… straight from their forums and the mouth of a SF API moderator.