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
MJRDeveloperMJRDeveloper 

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

Contact obj = new Contact();

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

//all mandatory fields

insert obj;

 

ccheers