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?
Best Answer
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).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 doRather than touching the relationship name, I simply supply the lookup field on the child object.
Or, to create a new child, I do
And then I get back a response that includes the Id of the new child.