1

I am a newbie to salesforce and hence unable to understand this code block. Can anyone help me to understand controller code?

Controller Code :

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.Allaccountssaved;    // I am returning another vf page here.
    }
} 

Visualforce Code:

<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 one more account" action="{!addAccount}"/>
                <apex:commandButton value="Save Accounts" action="{!saveAccount}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Edit

Why have we created two objects of Account type namely 'account' and 'acc', though we are adding both of them to the same list 'listAccount'. And also why do we write page reference ahead of a name of a method?

2
  • Why have we created two objects of Account type namely 'account' and 'acc', though we are adding both of them to the same list 'listAccount'. And also why do we write page reference ahead of a name of a method?
    – happy
    Commented May 15, 2015 at 11:45
  • Welcome to SF.SE happy. Instead of adding comments, you can edit your original question to update it by clicking on Edit at the bottom left of your post. I've gone ahead and done that for you. When you have sufficient reputation, you'll be able to comment on other people's posts in addition to your own. Visit the help center to learn more.
    – crmprogdev
    Commented May 15, 2015 at 13:35

3 Answers 3

0

When the Visualforce page is initially loaded, the page shows a row for one Account Object. This comes from account which is added in your constructor for controller class. The other acc object is used when user clicks on 'Add one or more account' button. This basically creates another acc object and adds to list object (listAccount)

If you click this button 'Add one more account' multiple times, same number of new acc object will get added to listAccount.

Once all information is provided on page and 'Save Accounts' button clicked, all the accounts in list will get saved in database.

However, saveAccount() method is incorrectly written.
for(Integer i=0; i


Since we have all accounts already in listAccount, we need simply use insert listAccount without the for loop.
Hope it is clear.

If this explanation helps you, please choose this as answer to your question.

Rgds, Dave

1

The controller code creates a new Account object when the page is accessed. It also creates a list of Account objects.

In the page you have two commandButtons, on the lines 19 and 20. The one at line 19 calls the method 'addAccount' of the controller, which creates a new Account, and add it to the list created in the beginning.

The button on line 20 calls the method 'saveAccount' in the controller, and it saves all accounts on the list to the database using the 'insert' command. When it does this operation, it returns a page reference to a page that (probably) shows a list of accounts saved in the database.

0

This code has a major problem in the saveAccount() method

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

 public PageReference saveAccount() {
    for(Integer i=0; i<listAccount.size(); i++)
    {
        insert listAccount;  // You will insert the list of Accounts over and over again!
    }
    return Page.Allaccountssaved;    // I am returning another vf page here.
 }
} 

Instead, it should be

public PageReference saveAccount() {
    insert listAccount;
    return Page.Allaccountssaved;    // I am returning another vf page here.
 }
}

The code would greatly benefit from better names - method saveAccount should be saveAccounts; listAccount should be accountList

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .