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
hemmhemm 

Setting a lookup field using an External ID in UPSERT

For the life of me, I am unable to perform an upsert and get a lookup field to get populated.  I am getting the error "id value of incorrect type: 3".  Here's how I have things configured.

Custom object 1
External_Id__c - this is a number field and has the attributes External ID and Unique checked to true. (example value is 3)

Custom object 2
Has a lookup field to Custom Object 1.  When upserting, my PHP array sets this values to a value that exists in CustomObject1.External_Id__c (example value is 3)
Upserting a record here gets the error "id value of incorrect type: 3"

If I comment out the lookup field when upserting to Custom Object 2, a record is upserted with no errors.

Any ideas? Do I have to do anything special in the PHP code to get this foreign key relationship to work?
msimondsmsimonds
Check this out > http://www.mikesimonds.com/setting-lookup-field-using-external-id-upsert-t114.html#post422


~Mike
hemmhemm
I was under the impression that there were two ways to populate a lookup field on an object.  The common way is to populate it with the ID of the record being looked up.  Another way (I think) is to populate the field with the value of the External ID from the record being looked up and Salesforce will automatically resolve that to the correct record.

Reading the section "upsert() and Foreign Keys" in the API guide leads me to think this.  What I don't know is how to apply this in the PHP toolkit.  I was hoping I could just set the field value, but maybe it's not that easy.

Ideas?
SuperfellSuperfell
I wrote up some notes on this, see http://www.pocketsoap.com/weblog/2008/09/1824.html
I'm not sure how this maps into the PHP toolkit, but hopefully between the c# example, and the actual SOAP message needed, it should get you there.
hemmhemm
Thanks, Simon.  That's great!  I don't have a confirmed answer, but I am suspecting that the PHP toolkit is not setup to do this.  I'd need to extend it.

I am actually trying to associate Custom Object 2 to Custom Object 1, Accounts and Contacts.  I was planning on doing some Apex Code anyway to resolve a couple of FKs, so I decided to just implement the logic in Apex Code to resolve all foreign keys.  What is actually happening behind the scenes is that I am pushing a set of related MySQL table in Salesforce.  I am now able to load each table individually with no dependencies between them. Once it's loaded Apex Code goes and uses the data on each table to resolve all the Foreign Keys I have.

It's similar to using the External ID to resolve it, but just doing it in a different way.

I am still looking for an answer whether the PHP toolkit supports this capability, but my specific needs in this use case are going to be met via Apex Code.


Message Edited by hemm on 09-08-2008 02:32 PM
SuperfellSuperfell
Incidently, the nested object approach for FK resolution works exactly the same in apex code.
hemmhemm
Would you be so kind as to write an example of this for Apex Code?  Maybe an example that sets the AccountId on a Contact using the External ID value from the Account.  I just need to understand the syntax and procedure for doing this.  It's hard to apply this to Apex by looking at other languages since Apex is so particular.

Thanks in advance!

SuperfellSuperfell
Sure, here's a block you can run with execAnonymous

Case c = new Case(subject='Apex FKs');
Account a = new Account(extId__c='00001');
c.account = a;
insert c;
hemmhemm
Wow, 2 lines of code.  Embarrassed I had to ask the question. :smileytongue:

Thanks, Simon.
hemmhemm

SimonF wrote:
Sure, here's a block you can run with execAnonymous

Case c = new Case(subject='Apex FKs');
Account a = new Account(extId__c='00001');
c.account = a;
insert c;

Simon, suppose that an Account didn't exist with that External ID.  Would this code create the Account or would it just not be able to populate the lookup field and leave c.account blank on the case record? 
SuperfellSuperfell
It would return an error saying it can't find an account with that extId value.
SuperfellSuperfell
Exception System.DmlException: Insert failed. First exception on row 0; first error: INVALID_FIELD, Foreign key external ID:00011 not found for field extId__c in entity Account AnonymousBlock: line 4, column 1
banders5144banders5144

This does not work for me.

 

Have a custom object called test_obj__c with a lookup to Account which has an external Id called ext_id__c.

 

When i try this code i get an error:

 

 

Account testa = new Account(ext_id__c = '11');
test_obj__c testing = new test_obj__c(Name = 'Test');
testing.acct_lkup__c = testa;
insert testing;

 Compile Error: line 3, column 1: Illegal assignment from SOBJECT:Account to Id

 

Why does this happen?

 

SuperfellSuperfell

You have to set the relationship, not the foreign key field itself. (note the __r rather than __c)

 

Account testa = new Account(ext_id__c = '11');
test_obj__c testing = new test_obj__c(Name = 'Test');
testing.acct_lkup__r = testa;
insert testing;
IvanStIvanSt

I ran into this issue, but I'm using this REST API. How can this be done using the REST API?