0

I am working with visualforce and trying to set up SelectOption over the records fetched through a SOQL query. I want to populate the records in a field to a DropDown List in my VF page.

While using System.debug, results are as desired but vf page is not showing the result. Unable to understand the problem with the code

<apex:page Controller="PermissionSetCtrl">
    <apex:form >
        <apex:pageBlock >

            <apex:pageBlockSection >
                <apex:pageBlockTable var="st" value="{!qry}">         
                    <apex:column headervalue="Profile" >        
                        <apex:outputText value="{!st.Profile.Name}"/>        
                    </apex:column>         
                    <apex:column > 
                        <apex:selectList value="{!objSelected}" multiselect="false" size="1">
                            <apex:selectOptions value="{!Objectlist}" />
                        </apex:selectList>
                    </apex:column>
                </apex:pageBlockTable>

                <apex:pageBlockTable value="{!listobj}" var="a1">
                    <apex:column headerValue="sObject" value="{!a1.SobjectType}"></apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>    

        </apex:pageBlock>    
    </apex:form> 
</apex:page>

And the Controller to the vf Page:

public class PermissionSetCtrl {
    public List<PermissionSet> qry {get; set;}
    Public List<SelectOption> Objectlist {get; set;}
    public List<ObjectPermissions> listobj {get; set;}
    public String objSelected {get; set;}

    Public List<SelectOption> getObjectlist(){
        system.debug('getObjectlist called!');
        List<SelectOption> options = new List<SelectOption>();
        for(ObjectPermissions lst : listobj){
            options.add(new SelectOption(lst.SobjectType, lst.SobjectType));
        }
        System.debug(options);
        return options;
    } 

    public PermissionSetCtrl(){
        system.debug('constructor called!');
        List<string> objlist = new List<String>();
        qry = [SELECT Id, Profile.Name,(Select SObjectType from ObjectPerms) from PermissionSet WHERE Id IN (SELECT ParentId FROM ObjectPermissions WHERE SObjectType = 'Account' AND PermissionsRead = true) limit 5];                            
        for(PermissionSet s : qry){
            listobj = s.ObjectPerms;          
        }
        getObjectlist();
    }    
}

2 Answers 2

1

I see a couple of issues with your code. I doubt if it compiles at all. Here are some guidelines about how you should use {get;set;}.

  1. {get;set;} are invoked automatically when VF page is loaded. It means you don't need to call getObjectList() from constructor.

  2. If you want to assign any value to any variable either initialize it in constructor otherwise use {get;set;}. Use both of them should be avoided.

Refer below sample for using selectOptions

VF Code:

<apex:selectCheckboxes value="{!objSelected}" title="Choose a object">
    <apex:selectOptions value="{!Objectlist}"/>
</apex:selectCheckboxes>

Apex Controller

public class ControllerConstructor {
    String[] objSelected = new objSelected[]{};
    public ControllerConstructor() {

    }

    public List<SelectOption> getObjectlist() {
        List<PermissionSet> lstPer = [SELECT Id, Profile.Name,(Select SObjectType from ObjectPerms) from PermissionSet WHERE Id IN (SELECT ParentId FROM ObjectPermissions WHERE SObjectType = 'Account' AND PermissionsRead = true) limit 5];
        List<SelectOption> options = new List<SelectOption>();
        for(PermissionSet ps : lstPer) {
            options.add(new SelectOption(ps.Id,ps.Profile.Name));
        }
        return options;
    }

    public String[] getobjSelected() {
        return objSelected;
    }

    public void setobjSelected(String[] objSelected) {
        this.objSelected = objSelected;
    }
}

Above code is not tested but I guess it will give some idea about how it should be.
For detailed documentation refer this selectOptions provided by salesforce.

0
0

Very true.. As Rohit said you have to avoid one of this -

 Public List<SelectOption> Objectlist {get; set;}

And

Public List<SelectOption> getObjectlist(){}

You must log in to answer this question.

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