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
vishal yadav 86vishal yadav 86 

My requirement is to show account block first and after account record inserted show contact block

VF PAGE CODE :
<apex:page controller="InsertAcc_Con" sidebar="False" tabstyle="Campaign">
<apex:sectionHeader title="Main Section" subtitle="Recrd Insertion"/>
<apex:form >
<apex:PageBlock title="Record Insert Block">
<apex:PageblockSection title="New Account Section" id="Accblock">
<apex:inputText label="Name" value="{!Nval}"/>
<apex:inputText label="Phone" value="{!Pval}"/>
<apex:inputText label="Website" value="{!Wval}"/>
<apex:commandButton value="Insert" action="{!InsertAccount}" rerender="Accblock"/>
</apex:PageblockSection>
<apex:PageblockSection title="New Contact Section" id="ConBlock">
<apex:inputText label="First Name" value="{!Fval}"/>
<apex:inputText label="Last Name" value="{!Lval}"/>
<apex:inputText label="Phone" value="{!CPval}"/>
<apex:inputText label="Fax" value="{!CFval}"/>
<apex:commandButton value="Insert" action="{!InsertContact}" rerender="ConBlock"/>
</apex:PageblockSection>
</apex:PageBlock>
</apex:form>
</apex:page>

VF CONTROLLER CLASS :
public class InsertAcc_Con
{
    public string Nval{get;set;}
    public string Pval{get;set;}
    public string Wval{get;set;}
    public void InsertAccount()
    {
        account acc = new account();
        acc.name = Nval;
        acc.phone = Pval;
        acc.website = Wval;
        insert acc;
    }
    public string Fval{get;set;}
    public string Lval{get;set;}
    public string CPval{get;set;}
    public string CFval{get;set;}
    Public void InsertContact()
    {
        contact con = new contact();
        con.firstname = Fval;
        con.lastname = Lval;
        con.phone = CPval;
        con.fax= CFval;
        insert con;
    }
Khan AnasKhan Anas (Salesforce Developers) 
Hi Vishal,

Greetings to you!

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="InsertAccAndConC">
    <apex:form >
        <apex:pageMessages />
        <apex:pageBlock >    
            <apex:pageBlockSection title="Insert Account" rendered="{!show_acc}">
                <apex:inputField value="{!acc.Name}"/> 
                <apex:inputField value="{!acc.Phone}"/>  
                <apex:commandButton value="Save" action="{!saveAcc}"/>
            </apex:pageBlockSection>
            
            <apex:pageBlockSection title="Insert Contact" rendered="{!show_con}">
                <apex:inputField value="{!con.FirstName }"/>
                <apex:inputField value="{!Con.LastName }"/>
                <apex:inputField value="{!Con.Phone }"/>
                <apex:commandButton value="Save" action="{!saveCon}" />
            </apex:pageBlockSection>
            
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class InsertAccAndConC {

    public boolean show_con {get;set;}
    public boolean show_acc {get;set;}
    public Account acc {get;set;}
    public Contact con {get;set;}
    
    public InsertAccAndConC(){
        acc = new Account();
        con = new Contact();
        show_con = false;
        show_acc = true;
    }
    
    public void saveAcc(){
        INSERT acc;
        acc = new Account();
        show_con = true;
        show_acc = false;
    }
    
    public void saveCon(){
        INSERT con;
        con = new Contact();
        show_acc = true;
        show_con = false;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi vishal,
I have made changes in your code.Try this one.
vf page:

<apex:page controller="InsertAcc_Con" sidebar="False" tabstyle="Campaign">
<apex:sectionHeader title="Main Section" subtitle="Recrd Insertion"/>
<apex:form >
<apex:PageBlock title="Record Insert Block">
<apex:PageblockSection title="New Account Section" id="Accblock">
<apex:inputText label="Name" value="{!Nval}"/>
<apex:inputText label="Phone" value="{!Pval}"/>
<apex:inputText label="Website" value="{!Wval}"/>
<apex:commandButton value="Insert" action="{!InsertAccount}"/>
</apex:PageblockSection>
<apex:PageblockSection title="New Contact Section" id="ConBlock" rendered = "{!showContact}">
<apex:inputText label="First Name" value="{!Fval}"/>
<apex:inputText label="Last Name" value="{!Lval}"/>
<apex:inputText label="Phone" value="{!CPval}"/>
<apex:inputText label="Fax" value="{!CFval}"/>
<apex:commandButton value="Insert" action="{!InsertContact}" rerender="ConBlock"/>
</apex:PageblockSection>
</apex:PageBlock>
</apex:form>
</apex:page>

controller:

public class InsertAcc_Con
{
    public string Nval{get;set;}
    public string Pval{get;set;}
    public string Wval{get;set;}
    public boolean showContact{get;set;}
    public void InsertAccount()
    {
        account acc = new account();
        acc.name = Nval;
        acc.phone = Pval;
        acc.website = Wval;
        insert acc;
        showContact = true;
    }
    public string Fval{get;set;}
    public string Lval{get;set;}
    public string CPval{get;set;}
    public string CFval{get;set;}
    Public void InsertContact()
    {
        contact con = new contact();
        con.firstname = Fval;
        con.lastname = Lval;
        con.phone = CPval;
        con.fax= CFval;
        insert con;
    }
}

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards