function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
tekrytekry 

Populate case lookup field using CaseNumber using REST API

I have an custom object MyObject__c with a custom lookup field to Case, Case__c.
I used Postman to create a new record through REST API that populates Case__c field from an existing Case record.
I used the following:
Resource: /services/data/v40.0/sobjects/MyObject__c
Method: POST
Body: 
{
    "Case__r": {
        "CaseNumber": "00001014"
    }
}
Result: The MyObject__c record was created and Case__c field is populated with correct Case record Id.

I tried similar approach in Apex:
insert new MyObject__c (
	Case__r = new Case (CaseNumber='00001014')
);
Result: 
ERROR running force:apex:execute:  Line: 2, Column: 12
Field is not writeable: Case.CaseNumber

Questions:
1) I understand that Apex code does not work because CaseNumber is not an external Id. However, why is the REST API working?
2) Is there Salesforce documentation for this behaviour in REST API?