[SFDC] Handling External ID's properly

I just wanted to a jot a quick post on something I found out while catching up on work over this Labor Day. For an upcoming project (teaser here) a case is being related to a custom object. That custom object is being tracked via an external ID, the unique ID of the device itself.

Now there shouldn't be any reason that doesn't work. In several places Salesforce documentation discusses the interchangeable nature of an external ID with our own Salesforce ID. And yet when sending POST data which looked like this:

{"Related_Device__c" : deviceID}

I would get an "invalid ID" error. Likewise if in Apex I tried:

Case c = new Case(Subject='Test',Related_Device__c='EXTERNALID'); insert c;

I would likewise get an invalid ID error. It didn't occur to me what the problem might be until I noticed a similarity across code samples. You don't hand the ID, you hand the object:

Device__c d = new Device__c(DeviceID__c='EXTERNALID');

Case c = new Case(Subject='Test',Related_Device__r=d); insert c;

And in the JSON body for a POST call you would likewise objectify it:

{"Related_Device__r" : {"Device__c": deviceID}}

Note the use of the underscore "r" which denotes using the full related object and not just a single field.

Why? I don't have a solid answer, my assumption is that something on our platform relies on the object in order to translate the ID. Why can't it do that on the fly in the background for functionality over half a decade old?

Dunno. But yes, sports fans, you can use an external ID instead of a Salesforce ID. You just have to ask the right way.

Happy Labor Day!

comments powered by Disqus