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
ericmonteericmonte 

Insert Record with a lookup field using externalId

So previously, I was able to associate an externalId to a Lookup Field in my Contacts. I've used this in the past in Dev501 course and now it is not working and getting error.

 

Here is the code:

Account refAcc = new Account(ClientExternalId__c = 'test1234');
Contact c = new Contact (AccountId = refAcc, FirstName = 'test', LastName = 'testMe');
upsert c;

system.debug('what is my new contact id +++++++' +c.id);

 and here is the error i am receiving:

Invalid initial expression type for field Contact.AccountId, expecting: Id

 

I am so lost on what happen and curious if anyone else see this issue?

Val ValinoVal Valino

Try saving refAcc first before you make the new Contact. You haven't saved the record yet, so technically there is nothing to relate to.

ericmonteericmonte

Tried that as well. No go. Also, I tried creating the Account with the external ID and still not working. :(

 

Few months ago I was able to use this code. And in the training premier training catalog for APEX, this is the code they use and it used to work.

Val ValinoVal Valino

Instead of AccountID use Account when you create the Contact.

ericmonteericmonte

Tried that as well.. Account get's created and Contact gets created but not being referenced.

 

Before i was able to only use 1 DML statement rather than having 2 DML statements :(