4

I upsert into Contact with external id using REST API like this :

https://myinstance.salesforce.com/services/data/v32.0/sobjects/Contact/clinician_id__c/1000

I send PATCH request along with Contact json data. It works just fine, and creates an orphaned Contact record since I don't include AccountId. I don't send AccountId because I do not have AccountId in my database.

But I do have external id field in Account that I can lookup in Account to find AccountId that I want to relate the Contact being upserted.

But I believe it'll require me to send two API requests, one to lookup AccountId in Account using external id, then upsert into Contact passing retrieved AccountId.

What's the best way for me to upsert into Contact without looking up AccountId, therefore, saving me a API call?

2 Answers 2

3

Thanks TC Sutton.

For all those keeping track of Salesforce generated ID, this should help.

There is no need to create another relationship to Account. It won't work anyways. Contact already has relationship to Account. It's not Account__r. It's just Account.

So I have custom external id field in Account called Org_ID__c. It can be referenced by "Account":{"Org_ID__c":1234} in your json data that's PATCHed to REST API point.

So if I go back to my question...

  • Send PATCH request to along with your "Authorization" header to

https://myinstance.salesforce.com/services/data/v32.0/sobjects/Contact/clinician_id__c/1000

  • along with JSON data of
{
    "FirstName":"Michael",
    "LastName":"Jordan", 
    "Account": {
         "Org_ID__c":3973
    }
}

And see magic happen.

1

I believe you can do that using the REST API. See the Section Upserting Records and Associating with an External ID:

http://www.salesforce.com/us/developer/docs/api_rest/Content/dome_upsert.htm

4
  • That looks to refer to using external ids on the record being upserted, not a related record... Commented Feb 7, 2015 at 0:02
  • 1
    The example shows updating object Line_Item__c with an external id of 123 and then referencing a related object Merchandise__r with an external id of 456. The same could be done with Contact and Account.
    – TC Sutton
    Commented Feb 7, 2015 at 0:07
  • My mistake, missed that little clarification. Thanks! Commented Feb 7, 2015 at 0:33
  • I saw this before and couldn't figure it out. But with your suggestion, I've looked over again and found the solution. I would love to upvote it thousand times because I think it'll save lots of headaches for those keeping track of AccountId(aka Salesforce.com ID). I will answer my own question if that's quite alright with detailed answer.
    – WJR
    Commented Feb 7, 2015 at 1:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .