0

I have developed vf page which have page block table displaying 3 fields.

I tried to modify it with use of apex :repeat

so that i have created one inner class as

public class outer{
    public class filterField {
            public String label {set;get;}
            public String searchFieldName {set;get;}
            public String searchValue {set;get;}
        }
}

I want create an array of filterFields. Populate this with the search field labels and their corresponding search field name, as per the list below. The searchValue will be initialized to null.

Accname : acc_name
Accname1 : acc_name1
Accname2 : acc_name2
<apex:repeat> which repeats for the filterField array

The outputLabel value will be the value of the “label” field of the filterField class; the inputtext value will be the value of the “searchValue” field of the filterField class.

I have tried like :

Outerclass.FilterField inc = new Outerclass.FilterField();
FilterField.label = 'ugh';
FilterField.searchFieldName  = 'ram';
FilterField.searchValue  = 'fdg';

FilterField fr=new FilterField{'a','d','s'};
  • Have you tried anything? – Eric Apr 25 '17 at 12:42
  • I tried like public FilterField filter; filter.add('a'); something like that and FilterField filter=new FilterField {'a','aq','as'}; but it showing error @Eric – sonu Apr 25 '17 at 12:45
  • Do you know how to declare a List? If not you should head over to trailhead. Filterlist is not a valid type which it why it did not work – Eric Apr 25 '17 at 12:47
  • Outerclass.FilterField inc = new Outerclass.FilterField(); FilterField.label = 'ugh'; FilterField.searchFieldName = 'ram'; FilterField.searchValue = 'fdg'; – sonu Apr 25 '17 at 12:48
  • Don't post code in comments. Update your question. Review How to Ask. – Eric Apr 25 '17 at 12:49
2

You're not too far off, actually. The simple answer to your question is that you initialize a List of your inner class the same as you would with any other List.

That is, the end result will look similar to this

List<Integer> myInts = new List<Integer>{1,2,3};

Integers are a bit of a trivial case though. An integer is simply itself, there are no extra fields to be set. Perhaps a better example would be initializing a List<Account>...

List<Account> myAccounts = new List<Account>{
    new Account(Name = Acct1, BillingStreet = '123 b Baker St'),
    new Account(Name = Acct2, BillingStreet = '4 Privet Drive')
};

Here we set a few fields on Account using the SObject constructor (which allows us to provide name = value pairs). We can't use this exact same pattern in your case (because your inner class is not an SObject), but it'll look similar.

// General convention is to have class names start with a capital letter
List<Outer.FilterField> myFilterFields = new List<Outer.FilterField>{
    new Outer.FilterField(),
    new Outer.FilterField(),
    new Outer.FilterField()
};

Close, but this doesn't allow you to set class variables like you could with the Account example. After initializing your list, you could grab an individual instance from it, and set values like this

myFilterFields[0].label = 'abc';
myFilterFields[0].searchFieldName = 'def';
myFilterFields[0].searchValue = 'ghi';

But that's rather cumbersome. You can make your life a bit easier by specifying a constructor for the FilterField class.

public class FilterField {
    public String label {set;get;}
    public String searchFieldName {set;get;}
    public String searchValue {set;get;}

    // public <class name> is the signature for a constructor.
    // You can specify any number of parameters for it
    public FilterField(String label, String searchFieldName, String searchValue){
        // In this case, since our parameter names are the same as our class
        //   property names (also known as variable shadowing), we need to use
        //   this.<class property name> to access the class property
        this.label = label;
        this.searchFieldName = searchFieldName;
        this.searchValue = searchValue;
    }
}

Doing that allows you to initialize your list almost the same way as you initialize a List<Account>

List<Outer.FilterField> myFilterFields = new List<Outer.FilterField>{
    new Outer.FilterField('abc', 'def', 'ghi'),
    new Outer.FilterField('123', '456', '789'),
    new Outer.FilterField(null, null, null)
};

The difference here is that you can't pass name=value pairs. Your constructor dictates the order that parameters are passed in, so abc in the example above will always be placed into the label property.

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.