1

I am trying to show a list of 5 of Accounts and along with it is an inputText. Now when I try to use the apex:repeat to display the 5 Accounts, along with the inputText (accPrice) and tried to loop the Account to see if each accPrice I put tied with each Account, it is not. The only value it is reading is the last field in the apex:repeat. Can you help me with this please?

Here is my VF Page:

<apex:page controller="ListsMapsController">
    <apex:outputPanel id="box" layout="block">
        <apex:pageMessages/>

        <apex:form >
            <apex:commandButton action="{!ShowAccounts}" value="Show" id="firstbutton" rerender="box"/>

            <apex:repeat value="{!accountList}" var="acc">
                <apex:outputField value="{!acc.Name}"/>
                <apex:inputText value="{!accPrice}"/><br/>
            </apex:repeat>

            <apex:commandButton action="{!submitFieldData}" value="Submit" id="button" rerender="box"/>
        </apex:form>
    </apex:outputPanel>
</apex:page>

Here is my Controller:

public class ListsMapsController {

    public Decimal accPrice {get; set;}
    public List<Account> accountList {get; set;}

    public void ShowAccounts()
    {
        accountList = [SELECT ID, NAME FROM Account LIMIT 5];
    }

    public void submitFieldData()
    {
        for(Account acc : accountList)
        {
            system.debug('hb.Name: ' + acc.Name);
            system.debug('hdbPrice: ' + accPrice);
        }
    }
}

Answers would much appreciated! Thank you in advance!

2
  • Simple description of what's happening: your implementation has a list of accounts and a single price... if you're looking for one price for each account, you need a list of prices, not just one.
    – Mark Pond
    Commented May 15, 2020 at 0:20
  • Thank you so much Mark for taking time answering my question. I am just a beginner in Developing, i am still learning. But how would I call the list in inputText? Or is there a way to implement your suggestion?
    – Noobie Dev
    Commented May 15, 2020 at 0:24

1 Answer 1

2

What you have are a number of input fields all tied to the same variable. When you submit the form, only one of the input values can actually be stored, as there is only one variable to store the info into.

One way would be to add a class holding an account and your accPrice. Then you can iterate over a list of those classes:

public class ListsMapsController {
    public List<MyHelperClass> myList {get; set;}
    public ListsMapsController () {
        List<Account> accs = [SELECT Id, Name FROM Account];
        myList = new List<MyHelperClass> ();
        for (Account acc : accs) {
            MyHelperClass helper = new MyHelperClass ();
            helper.acc = acc;
            myList.add (helper);
        }
    }

    public void submitFieldData ()
    {
        for(MyHelperClass mhc : myList)
        {
            system.debug('hb.Name: ' + mhc.acc.Name);
            system.debug('hdbPrice: ' + mhc.accPrice);
        }
    }

    class MyHelperClass {
        public Account acc {get; set;}
        public Decimal accPrice {get; set;}
    }
}

And the page needs to reference those fields then:

<apex:outputPanel id="box" layout="block">
    <apex:form >
        <apex:repeat value="{!myList}" var="ml">
            <apex:outputField value="{!ml.acc.Name}"/>
            <apex:inputText value="{!ml.accPrice}"/><br/>
        </apex:repeat>

        <apex:commandButton action="{!submitFieldData}" value="Submit" id="button" rerender="box"/>
    </apex:form>
1
  • Thank you so much! I just read the Wrapper Class article then went to see this and it works perfectly. I was able to implement it in our Org.
    – Noobie Dev
    Commented May 18, 2020 at 20:58

You must log in to answer this question.

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