4

I am new to salesforce development. I wonder what is the correct rest api if I want to get a standard object by using a field instead of id? For example, if I want to get a contact record by id, I try below command:

curl https://cs10.salesforce.com/services/data/v33.0/sobjects/Contact/Id/003J000001CxKKsIAN -H "Authorization: Bearer myAccessToken" -H "Content-Type: application/json" -X GET

(I know Node "Id" could be omitted) it returns as expected:

{"attributes":{"type":"Contact","url":"/services/data/v33.0/sobjects/Contact/003J000001CxKKsIAN"},"Id":"003J000001CxKKsIAN","IsDeleted":false,"MasterRecordId":null,"AccountId":"001J000001bXQZHIA4",...}

However, if I want to retrieve this record by account id:

curl https://cs10.salesforce.com/services/data/v33.0/sobjects/Contact/AccountId/001J000001bXQZHIA4 -H "Authorization: Bearer myAccessToken" -H "Content-Type: application/json" -X GET

it returns error:

[{"errorCode":"NOT_FOUND","message":"Provided external ID field does not exist or is not accessible: AccountId"}]

What is the correct example for my case?

3

You can do that only if you are using a External Id, AccountId is not. What you can do it to make a simple query (doc):

Endpoint

/services/data/v32.0/query/?q=SELECT+Id%2C+FirstName%2C+LastName+FROM+Contact+WHERE+AccountId+%3D+%2700128000002lyIm%27
  • so, you mean, if the query element is not the primary key or external id, I should use query instead of a rest call like mine, right? – Phoenix Jun 2 '15 at 22:56
  • That's right, otherwise you can create your own apex rest api, but then it will requires more development on salesforce side. – Cloud Ninja Jun 2 '15 at 22:58
  • Same to do update action, right? I mean, I also need write my own sql to execute an update if I want to update the record by one of its field (instead of external id), right? – Phoenix Jun 2 '15 at 23:00
  • If external Id, you could update straight away your contact without its id. If you are using another field than an external id, you will need to make 2 calls, the first one to GET the record contact and its id, then another one to update(POST) the contact using the id. – Cloud Ninja Jun 2 '15 at 23:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.