[SalesForce] Upsert into Contact without knowing AccountId but relate to existing Account

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?

Best Answer

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.