[SalesForce] Give lookup field External Id in JSON request

I am using standard create Rest resource to create an contact record in sales force. In json , I am matching the Label name in request with SFDC custom field name , so SFDC do the mapping for me automatically.

/services/data/v20.0/sobjects/contact/

Now , there is custom lookup for Region__c object. So contact have 2 two Country__c and State__c in it. json request doesn't have SFDD ID to put in lookup field. I have created a external ID in Region__c. but, I don't know how to put values in json request , so based on external ID SFDC can populate the records id. Please help thhanks.

{    
    "Salutation":"Mr",
    "FirstName" : "Rest",
    "LastName" : "Explorer",
    "Job_Title_HDS__c" : "Account Manager",
    "Department" : "Sales",
    "Email" : "test@test.com",
    "HomePhone":"12312123",
    "Fax":"123456789",
    "MobilePhone" : "12312312",
    "Street__c" : "teststreet",
    "Street_2__c" : "testStreet2",
    "Street_3__c" : "testStreet2",
    "City__c" : "Bangalore",
    "Preferred_State_HDS__c" :  **How should I refer to lookup object external Id , so we don't need to put records id.**
}

Best Answer

It works the same way as in SOAP & Apex, you pass a nested object of the related type, specifying its external Id, e.g. here's a payload to create a contact, and specify the account by an account external Id.

{
    "firstName" : "Simon",
    "lastName" : "Fell",
    "Account" : { "extId__c" : "SFDC" }
}

so in your case you'd have something like

{ 
"Salutation":"Mr", 
"FirstName" : "Rest", 
"LastName" : "Explorer",
"region__r" : { "externalId_field__c" : "externalId_value" }
}
Related Topic