+ Start a Discussion
ptran123ptran123 

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) 
jungleeejungleee

Hi,

 

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', accountid=a.id);
insert c;

 

 

Regards

Sameer