[SalesForce] Update a relationship via REST API

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).

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.