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
sumanth kumar 26sumanth kumar 26 

Contact insert along with account

Hello Developers...
I need a visualforce where Account and contact fields will be displayed on different pageblock sections and when the details are entered, contact should be associated with that account. 
Thanks in advance
Sumanth kumar
Best Answer chosen by sumanth kumar 26
Karan Shekhar KaulKaran Shekhar Kaul
Try below code. Modify as per your requirment.
You can also use below resource to do dynamic DML. 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_foreign_keys.htm
Hope this helps


VF page:

<apex:page showHeader="true"  controller="AccountContactController">
    <apex:form >
    <apex:pageBlock title="Account & Contact Insert">
        <apex:pageBlockSection title="Account Details">
            <apex:inputField value="{!acc.Name}"/>
           
        </apex:pageBlockSection>
        <apex:pageBlockSection title="Contact Details">
         <apex:inputField value="{!con.FirstName}"/>
         <apex:inputField value="{!con.LastName}"/>
        </apex:pageBlockSection>
        <apex:commandButton value="Save" action="{!saveDetails}"/>
    </apex:pageBlock>
    
    
    </apex:form>
</apex:page>


Controller:



public with sharing class AccountContactController {
    
    public Account acc{get;set;}
    public Contact con{get;set;}

    public AccountContactController(){
        acc = new Account();
        con = new Contact();
    }
    
    public PageReference saveDetails(){
        
        List<SObject> sObjList = new List<SObject>();
        sObjList.add(acc);
        sObjList.add(con);
        
        insert acc;
        con.AccountId = acc.id;
        insert con;
        return new pagereference('/'+acc.Id);
    }

}
 

All Answers

Karan Shekhar KaulKaran Shekhar Kaul
Try below code. Modify as per your requirment.
You can also use below resource to do dynamic DML. 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_foreign_keys.htm
Hope this helps


VF page:

<apex:page showHeader="true"  controller="AccountContactController">
    <apex:form >
    <apex:pageBlock title="Account & Contact Insert">
        <apex:pageBlockSection title="Account Details">
            <apex:inputField value="{!acc.Name}"/>
           
        </apex:pageBlockSection>
        <apex:pageBlockSection title="Contact Details">
         <apex:inputField value="{!con.FirstName}"/>
         <apex:inputField value="{!con.LastName}"/>
        </apex:pageBlockSection>
        <apex:commandButton value="Save" action="{!saveDetails}"/>
    </apex:pageBlock>
    
    
    </apex:form>
</apex:page>


Controller:



public with sharing class AccountContactController {
    
    public Account acc{get;set;}
    public Contact con{get;set;}

    public AccountContactController(){
        acc = new Account();
        con = new Contact();
    }
    
    public PageReference saveDetails(){
        
        List<SObject> sObjList = new List<SObject>();
        sObjList.add(acc);
        sObjList.add(con);
        
        insert acc;
        con.AccountId = acc.id;
        insert con;
        return new pagereference('/'+acc.Id);
    }

}
 
This was selected as the best answer
sumanth kumar 26sumanth kumar 26
It worked....Thanks a lot Karan :)