2

I am displaying Profile's object access in vfPage but when I click the button to display, it did not even call my action method in my Class. Can somebody explain why this might be happening?

Apex Class

public with sharing class ProfileObjectAccess {

    string[] Profiles = new string[]{};
    public limitWrapper[] WrapperBlocks{get;set;}

    public ProfileObjectAccess(){
        WrapperBlocks = new limitWrapper[]{};              
    }

    public list<SelectOption> getItems() {

        map<Id, Profile> mProf = new map<Id, Profile>([SELECT Id, Name FROM Profile]);
        List<SelectOption> options = new List<SelectOption>();
        for (Id idKey : mProf.keyset()){
            string sName = mProf.get(idKey).name;
            options.add(new SelectOption(idKey,sName));
        }
        return options;
    }

    public string[] getProfiles(){
        return Profiles;
    }

    public void setProfiles(string[] Profiles){
        this.Profiles = Profiles;
    }

    public void RetrieveProfileObjectPermission(){

        system.assertequals(3,2);
        system.assertequals(1,2);

        wrapper[] lWrap = new wrapper[]{};


        list<ObjectPermissions> op = [SELECT Parent.ProfileId, sObjectType, PermissionsCreate, PermissionsRead, PermissionsEdit, 
                                             PermissionsDelete, PermissionsViewAllRecords, PermissionsModifyAllRecords 
                                      FROM ObjectPermissions];
                                      //WHERE Parent.ProfileId =: ProfileId];

        for(ObjectPermissions o: op){

            wrapper wProf = new wrapper();                  

            wProf.ProfileId = o.Parent.ProfileId;
            //wProf.ProfileName = mProf.get(o.Parent.ProfileId).name;
            wProf.ObjectName = o.sObjectType;
            wProf.hasCreate = o.PermissionsCreate;
            wProf.hasRead = o.PermissionsRead;
            wProf.hasEdit = o.PermissionsEdit;
            wProf.hasDelete = o.PermissionsDelete;
            wProf.hasViewAll = o.PermissionsViewAllRecords;
            wProf.hasModifyAll = o.PermissionsModifyAllRecords;

        }
        WrapperBlocks.add(new limitWrapper(lWrap));


    }

    public class limitWrapper{
        public wrapper [] wrappers {get;set;}

        public limitWrapper(wrapper[] ws)
        {
            wrappers = ws;
        }

    }

    public class wrapper{   
        public string ProfileId {get;set;}
        public string ProfileName {get;set;}
        public string ObjectName {get;set;}
        public boolean hasCreate {get;set;}
        public boolean hasRead {get;set;}
        public boolean hasEdit {get;set;}
        public boolean hasDelete {get;set;}
        public boolean hasViewAll {get;set;}
        public boolean hasModifyAll {get;set;}
    }

}

Visualforce Page

<apex:page controller="ProfileObjectAccess">
    <apex:form >
        <apex:selectList value="{!Profiles}" size="1">
            <apex:selectOptions value="{!Items}"/>
        </apex:selectList>

        <!--  <apex:commandButton value="Retrieve Profile Accesss" action="{!RetrieveProfileObjectPermission}" rerender="out" status="status"/> -->
        <apex:commandButton value="Retrieve Profile Accesss" action="{!RetrieveProfileObjectPermission}" />
    </apex:form>

    <apex:pageBlock id="out">
        <apex:repeat value="{!WrapperBlocks}" var="block">
            <apex:pageBlockTable value="{!block.wrappers}" var="w">
                <apex:column value="{!w.ProfileId}" />    
                <apex:column value="{!w.ProfileName}"/>  
                <apex:column value="{!w.ObjectName}"/>  
                <apex:column value="{!w.hasCreate}"/>  
                <apex:column value="{!w.hasRead}"/>  
                <apex:column value="{!w.hasEdit}"/>  
                <apex:column value="{!w.hasDelete}"/>  
                <apex:column value="{!w.hasViewAll}"/>
                <apex:column value="{!w.hasModifyAll}"/>                
            </apex:pageBlockTable>
        </apex:repeat>
    </apex:pageBlock>
</apex:page>
1
  • Please see my updated response.
    – JCD
    Mar 6, 2015 at 21:39

1 Answer 1

2

Your RetrieveProfileObjectPermission() method should be of type PageReference, rather than void (you will then want to return null at the end of that method, unless you are planning on redirecting to another page). In order to refresh the table and display your results, you will also want to use the rerender attribute in your button tag, as you have done in the commented-out version of your button.

Edit:

I threw this together in a sandbox and checked the debug logs, and found this:

EXCEPTION_THROWN|[EXTERNAL]|System.TypeException: Invalid conversion from runtime type String to List<String>

This stems from the fact that your Profiles bind variable is an array (list) of strings in your controller, but the page will treat it like just a string because your picklist is only of size 1. Also, your apex:pageBlock tag wasn't within the apex:form tag. Finally, you weren't actually adding the wProf variable to your lWrap array in the RetrieveProfileObjectPermissions method, so even if you got around the other problems, you would have never seen any data in the table. This seems to work just fine as far as I can tell:

Apex Controller:

public with sharing class ProfileObjectAccess {

    public string Profiles {public get; public set;}
    public limitWrapper[] WrapperBlocks{public get; private set;}

    public ProfileObjectAccess(){
        WrapperBlocks = new limitWrapper[]{};              
    }

    public list<SelectOption> getItems() {

        map<Id, Profile> mProf = new map<Id, Profile>([SELECT Id, Name FROM Profile]);
        List<SelectOption> options = new List<SelectOption>();
        for (Id idKey : mProf.keyset()){
            string sName = mProf.get(idKey).name;
            options.add(new SelectOption(idKey,sName));
        }
        return options;
    }

    public PageReference RetrieveProfileObjectPermission(){
        WrapperBlocks.clear();
        wrapper[] lWrap = new wrapper[]{};

        list<ObjectPermissions> op = [SELECT Parent.ProfileId, Parent.Profile.Name, sObjectType, PermissionsCreate, PermissionsRead, PermissionsEdit, 
                                             PermissionsDelete, PermissionsViewAllRecords, PermissionsModifyAllRecords 
                                      FROM ObjectPermissions where Parent.ProfileId = :Profiles];

        for ( ObjectPermissions o : op ) {
            wrapper wProf = new wrapper();                  

            wProf.ProfileId = o.Parent.ProfileId;
            wProf.ProfileName = o.Parent.Profile.Name;
            wProf.ObjectName = o.sObjectType;
            wProf.hasCreate = o.PermissionsCreate;
            wProf.hasRead = o.PermissionsRead;
            wProf.hasEdit = o.PermissionsEdit;
            wProf.hasDelete = o.PermissionsDelete;
            wProf.hasViewAll = o.PermissionsViewAllRecords;
            wProf.hasModifyAll = o.PermissionsModifyAllRecords;
            lWrap.add(wProf);
        }
        WrapperBlocks.add(new limitWrapper(lWrap));

        return null;
    }

    public class limitWrapper{
        public wrapper[] wrappers {get;set;}
        public limitWrapper(wrapper[] ws) {
            wrappers = ws;
        }
    }

    public class wrapper {   
        public Id ProfileId {get;set;}
        public string ProfileName {get;set;}
        public string ObjectName {get;set;}
        public boolean hasCreate {get;set;}
        public boolean hasRead {get;set;}
        public boolean hasEdit {get;set;}
        public boolean hasDelete {get;set;}
        public boolean hasViewAll {get;set;}
        public boolean hasModifyAll {get;set;}
    }

}

VF Page:

        <apex:commandButton value="Retrieve Profile Access" action="{!RetrieveProfileObjectPermission}" rerender="out" />

    <apex:pageBlock id="out">
        <apex:repeat value="{!WrapperBlocks}" var="block">
            <apex:pageBlockTable value="{!block.wrappers}" var="w">
                <apex:column headerValue="Profile ID" value="{!w.ProfileId}" />    
                <apex:column headerValue="Profile Name" value="{!w.ProfileName}"/>  
                <apex:column headerValue="Object Name " value="{!w.ObjectName}"/>  
                <apex:column headerValue="Create" value="{!w.hasCreate}"/>  
                <apex:column headerValue="Read" value="{!w.hasRead}"/>  
                <apex:column headerValue="Edit" value="{!w.hasEdit}"/>  
                <apex:column headerValue="Delete" value="{!w.hasDelete}"/>  
                <apex:column headerValue="View All" value="{!w.hasViewAll}"/>
                <apex:column headerValue="Modify All" value="{!w.hasModifyAll}"/>                
            </apex:pageBlockTable>
        </apex:repeat>
    </apex:pageBlock>

    </apex:form>
</apex:page>
0

You must log in to answer this question.

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