An effort to bring a revolution in Salesforce.

Saturday 25 July 2015

How to create Contact in the Account As a Related list

 

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

 

 e.g. 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:

Post a Comment

Get Gmail, Docs, Drive, and Calendar for business