The requirements for Contacts to have an Account associated seem to be a little loose so I was able to get a successful compile and test run, but I wanted to better understand why this code returned an

'Illegal Assignment' error:

public class TestDeDuper {
    static testMethod void testDupes() {
        //Principle #ls: Create recordsd from Scratch

        //creating new Account from Scratch
        Account testAccount = new Account();
        testAccount.Name = 'Tryler McCoder Inc';
        insert testAccount;

        //Now trying to create a new Contact
        Contact dupeContact = new Contact();
        dupeContact.LastName = 'Spiderman';
        dupeContact.Email = 'spiderman@gmail.com';
        dupeContact.Account = testAccount.Id;
        insert dupeContact;

That's the main chunk of code that I am concerned with, since the it appears that this snippet:

dupeContact.Account = testAccount.Id;

Is throwing this error:

Illegal assignment from Id to Account at line 15 column 9

I double checked that the field label on Contact for the Account lookup is correct, so what about the way this is written would be returning the 'Illegal Assignment' error?

  • 1
    GPP.... still facing issue? – Ratan Paul Mar 28 '16 at 9:05
dupeContact.Account = testAccount.Id;

this should be AccountId field on contact

dupeContact.AccountId = testAccount.Id;
  • 1
    thank you, that resolved the issue. I will keep in mind these fields have subtle sub fields that contain the Id string. – GPP Mar 28 '16 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.