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
Prallavi DuaPrallavi Dua 

Insert Account through VF page

Hi,
I want to work on below task, Can anyone please help me in that task

Create a visual force page with two page blocks, one showing the existing Accounts and another for inserting new accounts. The inserted account also needs to be dynamically displayed in the existing Account list.

Thanks in Advance.
karthikeyan perumalkarthikeyan perumal
Hello 

Here is the code for Insert account. for display List Existing record make another pageblock just display account, also dont forget to referesh the list block after insert. 

Page: 
<apex:page controller="multiAccountInsert">
    <apex:form >
        <apex:pageBlock >
        
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}" rerender="error"/>
            </apex:pageBlockButtons>
        
            <apex:pageBlockTable value="{!accts}" var="a" id="table">
                <apex:facet name="footer">
                    <apex:commandLink value="Add Row" action="{!addRow}" rerender="table,error"/>
                </apex:facet>
                <apex:column headerValue="Name">
                    <apex:inputField value="{!a.Name}"/>
                </apex:column>
                <apex:column headerValue="Billing City">
                    <apex:inputField value="{!a.BillingCity}"/>
                </apex:column>                        
            </apex:pageBlockTable>
            
        </apex:pageBlock>
    </apex:form>
</apex:page>


Controller: 
 
public class multiAccountInsert{

    public List<Account> accts {get; set;}
    
    public multiAccountInsert(){
        accts = new List<Account>();
        accts.add(new Account());
    }
    
    public void addrow(){
        accts.add(new Account());
    }
    
    public PageReference save(){
        insert accts;
        PageReference home = new PageReference('/home/home.jsp');
        home.setRedirect(true);
        return home;
    }
}

Hope this will help you, 

Mark Best ANSWER if its works for you. 

Thanks
karthik
 
Sonam PatilSonam Patil
Hello Karthik,
This is not my requirement.
My requirement is, in one pageblock all the account should be displayed and other pageblock is to insert account. And inserted account should be dynamically added to the displayed list.
DeveloperDeveloper
Hi sonam patill,
i have requriment could you please share u r code.
DeveloperDeveloper
same requriment
 
Prallavi DuaPrallavi Dua
Hi Gopal,
Below is the code for your requirement.


<apex:page controller="multiAccountInsert">
    <apex:form >
        <apex:pageBlock >
         
            <apex:pageBlockTable value="{!accts}" var="a">
                  <apex:column value="{!a.Name}"/>  
                  <apex:column value="{!a.Industry}"/>
                  <apex:column value="{!a.phone}"/>             
            </apex:pageBlockTable>
     </apex:pageBlock>
         <apex:pageBlock >
              <apex:pageBlockButtons location="Bottom" >
                <apex:commandButton value="Save" action="{!Save}" id="display" />
            </apex:pageBlockButtons>
                <apex:pageblocksection >
                    Enter Name: <apex:inputText value="{!AccountName}"/>
                </apex:pageblocksection>
         </apex:pageBlock>
    </apex:form>
</apex:page>
==========================================================
public class multiAccountInsert{

    public Account at { get; set; }
    public String AccountName{get;set;}

    public PageReference Save() {
    at = new Account();
    at.Name=AccountName;
    
    insert at;
    
        return null;
    }


    public List<Account> accts {get; set;}
        
        public multiAccountInsert(){
        accts = new List<Account>();
        accts = [Select id, Name, Industry, Phone from Account];
       }
}
DeveloperDeveloper
Sorry for late reply .
Thank you sending code.
sam khan 13sam khan 13
hey 
i go through the code 
i wanna ask that if i wanna show the newly inserted record on top of the table.then what will i do?