[SalesForce] Populate Owner in REST API Using external Id

I'm upserting a custom object via the REST api.
How can I use the REST api to populate the record owner using an external id?
See details below:

Endpoint:

/services/data/v42.0/sobjects/finserv__Alert__c/FinServ__SourceSystemId__c/test-external_id

Method: PATCH

Body:

{  
    "FinServ__Account__r":{  
        "External_Id__c":"account-external-id-1"
    },
    "FinServ__Message__c":"Alert-Subject-Finserv-CREATE-1",
    "FinServ__MessageDescription__c":"Alert-Description-Finserv-CREATE-1",
    "FinServ__Active__c":false,
    "Owner":{  
        "External_Id__c":"user-external-id-1"
    }
}

I get the following error when I run this in workbench:

message: Unexpected character ('F' (code 70)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [line:1, column:2]
errorCode: JSON_PARSER_ERROR

This seems to be because Owner is a UserOrGroup lookup, and not a lookup to a single object. The moment I remove "Owner": {…} from the payload, it succeeds.

How can I use the REST api to populate the record owner using an external id?

Best Answer

Even though this question is one year old I had the same issue today and found a solution to that problem.

With standard Rest API, your request only needs a slight modification:

{  
    "FinServ__Account__r":{  
        "External_Id__c":"account-external-id-1"
    },
    "FinServ__Message__c":"Alert-Subject-Finserv-CREATE-1",
    "FinServ__MessageDescription__c":"Alert-Description-Finserv-CREATE-1",
    "FinServ__Active__c":false,
    "Owner": {
        "attributes": {
            "type": "User"  
        },
        "External_Id__c":"user-external-id-1"
    }
}

Note the additional hint to what will actually be transferred within Owner structure via the attributes sub type.

I did not find official documentation on this, however, it just works. :)

Related Topic