2

I'm looking to generalize some of my VF code by creating a component. I need to pass a picklist to my component from the VF page, i.e. <c.CustomComponent picklist="{!Opportunity.Picklist__c}"></c.CustomComponent> then through getter/setter methods access the picklist in the component's controller. Within the controller, I want to access the fields getDescribe() method and return a List<SelectOption> for the VF page. I am able to do this when I hardcode the field, but I think the problem I am having is coming up with the correct datatype in the generalized component's controller. I've tried Picklist, String, and ValueSet with no luck. Has anyone done this? Is my approach wrong?

If in my VF page, I use this markup:

<c:customPicklist picklist="{!Opportunity.Current_Solution__c}"></c:customPicklist>

Then in my component's controller, I've attempted these:

A:

public class PicklistWithOtherController {
        private static final String startingChoice = 'Make Selection';
        public Picklist picklist;
        public void setPicklist(String s){

        }
        public List<SelectOption> getPicklist(){
            List<SelectOption> options = new List<SelectOption>();
            Schema.DescribeFieldResult fieldResult = picklist.getDescribe();
            List<Schema.PicklistEntry> picklistList = fieldResult.getPicklistValues();
            for (Schema.PicklistEntry pke : picklistList) {
                options.add(new SelectOption(pke.getLabel(), pke.getValue()));
            }
            options.add(0, new SelectOption(startingChoice, startingChoice, true));
            return options;
        }
    }

Invalid type: Picklist at line 3 column 12

B:

public class PicklistWithOtherController {
    private static final String startingChoice = 'Make Selection';
    public String picklist;
    public void setPicklist(String s){

    }
    public List<SelectOption> getPicklist(){
        List<SelectOption> options = new List<SelectOption>();
        Schema.DescribeFieldResult fieldResult = picklist.getDescribe();
        List<Schema.PicklistEntry> picklistList = fieldResult.getPicklistValues();
        for (Schema.PicklistEntry pke : picklistList) {
            options.add(new SelectOption(pke.getLabel(), pke.getValue()));
        }
        options.add(0, new SelectOption(startingChoice, startingChoice, true));
        return options;
    }
}

Method does not exist or incorrect signature: [String].getDescribe() at line 9 column 51

C:

public class PicklistWithOtherController {
    private static final String startingChoice = 'Make Selection';
    public ValueSet picklist;
    public void setPicklist(String s){

    }
    public List<SelectOption> getPicklist(){
        List<SelectOption> options = new List<SelectOption>();
        Schema.DescribeFieldResult fieldResult = picklist.getDescribe();
        List<Schema.PicklistEntry> picklistList = fieldResult.getPicklistValues();
        for (Schema.PicklistEntry pke : picklistList) {
            options.add(new SelectOption(pke.getLabel(), pke.getValue()));
        }
        options.add(0, new SelectOption(startingChoice, startingChoice, true));
        return options;
    }
}

Invalid type: ValueSet at line 3 column 12

If I were to hardcode the picklist, I can access the getDescribe() method just fine.

public class PicklistWithOtherController {
    private static final String startingChoice = 'Make Selection';
    public void setPicklist(String s){

    }
    public List<SelectOption> getPicklist(){
        List<SelectOption> options = new List<SelectOption>();
        Schema.DescribeFieldResult fieldResult = Opportunity.Current_Solution__c.getDescribe();
        List<Schema.PicklistEntry> picklistList = fieldResult.getPicklistValues();
        for (Schema.PicklistEntry pke : picklistList) {
            options.add(new SelectOption(pke.getLabel(), pke.getValue()));
        }
        options.add(0, new SelectOption(startingChoice, startingChoice, true));
        return options;
    }
}

So my question is, how can I generalize this code to accept any picklist?

  • It would help if you share what you've tried so far. – Adrian Larson Jul 5 '17 at 19:02
  • Please edit your post with any clarifications. – Adrian Larson Jul 10 '17 at 15:49

Your Answer

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

Browse other questions tagged or ask your own question.