0

I need to add a record to my account object by using lists in my apex class i wrote the below code and when i execute the vf page it is showing that Unknown property 'Listofaccounts.a' Can anyone please help me to solve out the issue?

Class:

public class Listofaccounts
{
  public static  void accounts()
   {
    List<Account> myList = new List<Account>(); 
    Account a = new Account(Name='ramana'); 
    myList.add(a);                    
    }
}

Vf-page:

<apex:page Controller="Listofaccounts">
<apex:form>
      <apex:inputField value="{!a.Name}" required="false">
      </apex:inputField>
</apex:form>
</apex:page>
  • I've rolled this back to a previous edit, because the edit only changed the formatting (and in doing so, made it much easier to read). The stackexchange network uses a dialect of "Markdown" to format text inside of posts. You can read more about it here. – Derek F Sep 27 '20 at 12:37
2

Your list in Apex doesn't contain the literal variable a. It contains the contains the same Account that is stored in a.

Just like in Apex, when you have a list, you need to loop over the elements one at a time. In Apex, you'd use a for or while loop. With Visualforce, you use <apex:repeat> (or some other component that can handle multiple items, like <apex:pageBlockTable>). The <apex:repeat> documentation is what you should use for guidance here.

The example given is pretty clear and straightforward, but in the interest of not relying on links, I'll reproduce the example here (with added comments)

<!-- Page: -->

<apex:page controller="repeatCon" id="thePage">
    <!-- The "value" takes a collection (list, string, or set), and the merge
        notation ({!thing}) either references a property (a class variable
        that has something like {get; set;} after it) or a getter.
        In this example, they're using an explicit getter (getStrings()).
        Getters start with "get", but we don't include that part in the Visualforce.-->
    <apex:repeat value="{!strings}" var="string" id="theRepeat">

        <!-- Inside the repeat, we can use the "var" of the repeat like it
          were a property.
          If we were iterating over accounts, and you used var="acc", then you
          could access account fields using normal dot notation (e.g.
          {!acc.Name} -->
        <apex:outputText value="{!string}" id="theValue"/><br/>

    </apex:repeat>

</apex:page>
/*** Controller: ***/

public class repeatCon {
    // This is how a property would be defined (but it's not present
    //   in this particular example
    //public String myString {get; set;}
    
    // This is an explicitly defined getter
    public String[] getStrings() {
        return new String[]{'ONE','TWO','THREE'};
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.