+ Start a Discussion

Problem associating contact with an account in controller

Hi. Long time reader, first time poster. I am having a problem associating a contact object with an account object in my custom controller extension. I have two objects, a contact and an account. I am creating a new contact and want it associated with the active account. When the contact gets inserted, it has all of the correct information (name, phone, ect.), but the account field is empty. I've posted the insert function here. Any help is appreciated.



public pagereference savecontact(){ my_obj.c.account = my_obj.a; insert my_obj.c; return page.MyPagePG2; }


Best Answer chosen by Admin (Salesforce Developers) 

Contact obj = new Contact();

obj.AccountId = accId;//your account id here

//all mandatory fields

insert obj;