1

I've got an Account object on which I've created a multiple relationship Online_quotes1__r that accepts OnlineQuote__c objects. Now I'd like to relate my OnlineQuote__c objects with Account via REST API.

Trying to do it via:

PATCH:/services/data/v42.0/sobjects/Account/[id]/Online_quotes1__r endpoint gives me HTTP Method 'PATCH' not allowed. Allowed are HEAD,GET error

And via: PATCH:'/services/data/v42.0/sobjects/Account/[id]/' endpoint gives me QueryResult must start with '{' error for payload:

'Online_quotes1__r' => [

 ]

and Invalid field in QueryResult: 'Id' for payload:

'Online_quotes1__r' => [
    'Id' => '[id]'
]

What is the syntax to do what I'm after?

1

The custom relationship name on a parent object, here Online_quotes1__r, is used in SOQL to query from the parent down the child and in Apex to access queried child objects. You cannot, however, write to the custom relationship as a field in order to add new child objects.

Instead, you can manipulate the children directly. Here's an example with Contact and Account (where the relationship name is Contacts, but the mechanism works the same way).

GET /services/data/v43.0/sobjects/Account/0013600001sxEUmAAM/Contacts

{ "totalSize" : 1, "done" : true, "records" : [ { "attributes" : { "type" : "Contact", "url" : "/services/data/v43.0/sobjects/Contact/0033600001qwBXEAA2" }, "Id" : "0033600001qwBXEAA2", "IsDeleted" : false, "MasterRecordId" : null, "AccountId" : "0013600001sxEUmAAM", "LastName" : "Testerson", "FirstName" : "Test", ... }

So that gets me a list of currently associated child objects, including their Ids. Suppose though that I have a Contact (this one, 0033600001qwBXEAA2, that doesn't have a parent Account or that needs to be reparented. In that situation, I do

PATCH /services/data/v43.0/sobjects/Contact/0033600001qwBXEAA2
Body: { "AccountId": "YOUR_ACCOUNT_ID_HERE" }

Rather than touching the relationship name, I simply supply the lookup field on the child object.

Or, to create a new child, I do

POST /services/data/v43.0/sobjects/Contact/
Body: { "AccountId": "0013600001sxEUmAAM", "FirstName": "Tom", "LastName": "Test" }

And then I get back a response that includes the Id of the new child.

  • Unfortunately this doesn't work for custom objects (or my setup, idk tbh) - if I'll try to update my OnlineQuote__c object with PATCH: {"AccountID": [id]} I get "No such column 'AccountId' on sobject of type OnlineQuote__c". Inside SF CRM I do however have a Online Quote tab for Account object – eithed Oct 8 '18 at 15:15
  • The relationship field on OnlineQuote__c will not be AccountId; since it's a custom field, it will certainly end in __c. You need to examine your data model to determine what the API name of that custom lookup field is. – David Reed Oct 8 '18 at 15:18
  • Yup - that's it; I've RelatedAccount__c but either I was looking at wrong object or stopping my population of relationships midprocess as it didn't feed to CRM in the Online Quote tab. After your enquiry I've retested it and indeed it works as expected. – eithed Oct 8 '18 at 16:48

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.