I have a code snippet that runs like so:

Id accountId;

public SObject getContact(){
  Contact toReturn = new Contact();
  toReturn = fillInRequiredValues(toReturn);
  toReturn.Account = accountId;
  insert toReturn;
  return toReturn;

But whenever it's ran, I get the error: System.SObjectException: Invalid field Account for Contact, when account is clearly the first field in contact. It seems like this is highly an error on the back end, but I cannot be sure...


The field you want to set is AccountId. You can set a lookup via name pointing reference (Account in this case), but you just have the Id here.

toReturn.AccountId = accountId;
  • why does apex do this? would it not be simpler to just name the field 'Account' ? – tuskiomi Sep 20 '16 at 16:44
  • I find their naming conventions quite sensible. You can either do Account record = toReturn.Account or Id accountId = toReturn.AccountId. It makes sense to me. – Adrian Larson Sep 20 '16 at 16:47

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.