anusha(salesforce developer)

Friday, 8 July 2016

Dynamically add rows by click on add row button

Dynamically add rows by click on add row button

To add rows dynamically without using wrapper class and by clicking the add rows button.
<apex:page Controller="AddmultipleAccountsController">
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!listAccount}" var="acc">
<apex:column headerValue="Account Name">
<apex:inputField value="{!acc.Name}"/>
</apex:column>
<apex:column headerValue="Account Number">
<apex:inputField value="{!acc.AccountNumber}"/>
</apex:column>
<apex:column headerValue="Account Type">
<apex:inputField value="{!acc.Type}"/>
</apex:column>
<apex:column headerValue="Industry">
<apex:inputField value="{!acc.Industry}"/>
</apex:column>
</apex:pageBlockTable>
<apex:pageBlockButtons >
<apex:commandButton value="Add Accounts Row" action="{!addAccount}"/>
<apex:commandButton value="Save Accounts" action="{!saveAccount}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>


Find the below controller of above page editor

public class AddmultipleAccountsController
{
Account account = new Account();
public list<Account> listAccount{ get; set; }

public AddmultipleAccountsController()
{
listAccount=new list<Account>();
listAccount.add(account);
}

Public void addAccount()
{
Account acc = new Account();
listAccount.add(acc);
}
public PageReference saveAccount() {
for(Integer i=0; i<listAccount.size(); i++)
{
insert listAccount;
}
return Page.accountssaved;
}
}

Once user will pouplate the value and save the account ,page will be redirect to "AccountSaved" page.
<apex:page showheader="false">
Account Saved Succesfully
</apex:page>

No comments:

Post a Comment