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

How to link Contact with Account

I don't know what I am doing wrong.  I am try to create a Contact, then link it with an Account.  Here is the gist of it.



Account a = new Account();
a.Name = 'account1';
insert a;

Contact c = new Contact();
c.Account__c = a;         // is this the right way to link them together?
insert c;


Contact cc = [ select name, Account.Name from Contact ];
System.debug(cc.Account);    // Account is null here.  Why?


Much appreciated if someone figures this out.  I am stumped.  

Best Answer chosen by Admin (Salesforce Developers) 



Hope the below code helps!!

//insert an account
account a = new account(name='newAcc');
insert a;

//insert a contact with the above account
contact c = new contact(lastname='newCon',;
insert c;