How to create Contact under the Account As a Related list ?

Friday, July 24, 2015
Hi All,

This post is regarding insert ACCOUNT as well as its CONTACT.

for example see below screeshot :



Create one Visualforce Page.
Create one Class related to that visualforce Page.


Code of Visualforce Page :

<apex:page Controller="AccountDetail">

<apex:form >
    <apex:pageblock title="Account Detail with contact">
        <apex:pageblockButtons location="bottom">
            <apex:commandButton value="Save" action="{!Save}"/>
        </apex:pageblockButtons>
        <apex:outputPanel >
            <apex:pageblockSection title="Account Detail">
                <apex:inputField value="{!account.Name}" />
                <apex:inputField value="{!account.AccountNumber}"/>
                <apex:inputField value="{!account.date__c}"/>
             </apex:pageblockSection>
        </apex:outputPanel>    
        <apex:outputPanel >
              <apex:pageblockSection title="Contact Detail">
                <apex:inputField value="{!contact.FirstName}" />
                <apex:inputField value="{!contact.LastName}"/>
             </apex:pageblockSection>
        </apex:outputPanel>
    </apex:pageblock>
</apex:form>

</apex:page>

Code of Class related to above visualforce Page :

Public class AccountDetail{

    public Account account{get;set;}
    public Contact contact{get;set;}
   
    public AccountDetail(){
       account = new Account();
       contact = new Contact();
    }

   
    public void Save(){
    insert account;
    contact.AccountId = account.Id;
    insert contact;
    update account;
    }

}

No comments: