1

In our company's Salesforce we have a list of accounts with an External ID (External_ID__c). And we have a custom object Event (i.e. Event__c), also with an External ID (Event_ID__c) used and managed in our backend. In our backend we hold a list of events for every user, indicating which events this user has booked.

Now, we want to represent this relation Salesforce as well. From a traditional relational-database-point-of-view I would create a new relation/table/object Bookings__c with fields (id, External_ID_Booking, External_ID_Account__c, External_ID_Event__c, ... (other stuff like timestamps etc.)).

Now I want to populate that table from our backend when a user books an event. To do this, we use the REST API, specifically by using the Force.com-.NET-Client. As a starting point, you can assume that the user has already been synced with salesforce as an Account-Object, and the event he books has been synced as an Event__c-Object in advance (e.g. the user XY has created his account last week, the event AB was created by our company two days ago and TODAY the user XY books a ticket for event AB).

I have been reading through this article here, section "create() and Foreign Keys". There it is stated that one has to send a foreign-key-referenced-object with just the external-id set. As far as I understand it, that means to create a DTO for our Booking with a nested Account- and Event-Object, both with just the External-ID set.

The JSON that I send via the REST-API looks like this:

{
  "Name": "51aff1cd-146f-4fef-aa5d-c260ed0955d4 | 6423e899-abdb-4660-9eea-9eef5e5799f1",
  "Account__c": {
    "Account_ID__c": "51aff1cd-146f-4fef-aa5d-c260ed0955d4"
  },
  "Event__c": {
    "Event_ID__c": "6423e899-abdb-4660-9eea-9eef5e5799f1"
  },
  "Order_Time__c": "2016-12-13T12:40:21",
  "Price__c": 0
}

The Account__c-field of our Booking__c-Object is a lookup field for the standard Salesforce-Object Account, and the Event__c field is a lookup field for our Custom Object Event__c.

Taking the above mentioned article into account, I would assume that passing objects to those fields that contain nothing but the external id, I was expecting Salesforce to lookup these objects and automatically create the relationship. However, I keep getting the following error:

Cannot deserialize instance of reference from START_OBJECT value { 
or request may be missing a required field at [line:1, column:91]

Does anyone of you SF-pros out there have an idea what I am doing wrong?

1 Answer 1

1

Holy moly. I figured it out. It turns out that the solution was quite simple. Even though our custom fields that reference Account and Event have API names Account__c and Event__c they have to be referenced as Account__r and Event__r for salesforce to understand that you want to create that relationship.

Makes sense, but should be documented better!!!

The below version works for us now!

 {
  "Name": "51aff1cd-146f-4fef-aa5d-c260ed0955d4 | 6423e899-abdb-4660-9eea-9eef5e5799f1",
  "Account__r": {
    "Account_ID__c": "51aff1cd-146f-4fef-aa5d-c260ed0955d4"
  },
  "Event__r": {
    "Event_ID__c": "6423e899-abdb-4660-9eea-9eef5e5799f1"
  },
  "Order_Time__c": "2016-12-13T12:40:21",
  "Price__c": 0
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .