0

I have a variable (SelectedProfiles) that returns a list of profiles that are selected from a multi-select picklist on a VF page, and is used in the where clause in a SOQL query that returns the CRUD permissions by profile for the objects. How do I pass the variable values from the controller (GetFLSController) to the extension to use in the query?

Here is the extension.

public class OLSExtension {

public OLSExtension(GetFLSController obj){}

List<ObjectPermissions> ObjectPerms = [SELECT Id, ParentId, SobjectType, 
PermissionsViewAllRecords, PermissionsRead, 
PermissionsModifyAllRecords, PermissionsEdit, PermissionsDelete, 
PermissionsCreate 
     FROM ObjectPermissions
     Where (ParentId IN :GetFLSController.SelectedProfiles)];
        public List<ObjectPermissions> getObjPerms(){
        return ObjectPerms;
    }
}
  • 1
    I'm slightly confused here. Normally you either have a custom controller or a standard controller and one or more controller extensions, but you seem to be saying you have both a custom controller and controller extensions. Is that what you mean? What does the custom controller look like, and can you also include the actual apex:page tag from your Visualforce page file so we can see how you are using both? – Phil W Jun 24 at 15:55
0

I'm with Phil W here as Extension classes apply to StandardControllers and it looks like you are using OLSExtension as a Helper class, not an extension

Hence, if this is correct, then you would code as follows

public class OLSHelper {
  GetFLSController flsController;
  public OLSHelper(GetFLSController flsController){
     this.flsController = flsController;
  }

   List<ObjectPermissions> ObjectPerms = [SELECT Id, ParentId, SobjectType, 
         PermissionsViewAllRecords, PermissionsRead, 
         PermissionsModifyAllRecords, PermissionsEdit, PermissionsDelete, 
         PermissionsCreate 
     FROM ObjectPermissions
     Where (ParentId IN :flsController.SelectedProfiles)];

  public List<ObjectPermissions> getObjPerms(){
        return ObjectPerms;
    }
}

and you would invoke it from class GetFLSController as follows

ObjectPermission[] objperms = new OLSHelper(this).getObjPerms();

Having said all this, I would almost never pass an instance of a controller as an object to a helper class; the helper class should accept the list of profiles as the argument as it doesn't really care about how the profiles are collected. See Separation of Concerns principle).

That is, this is better:

ObjectPermission[] objperms = new OLSHelper().getObjPerms(profileList);

in which case the code looks like:

public class OLSHelper {
  public OLSHelper(){}


   List<ObjectPermissions> ObjectPerms = [SELECT Id, ParentId, SobjectType, 
         PermissionsViewAllRecords, PermissionsRead, 
         PermissionsModifyAllRecords, PermissionsEdit, PermissionsDelete, 
         PermissionsCreate 
     FROM ObjectPermissions
     Where (ParentId IN :flsController.SelectedProfiles)];

  public List<ObjectPermissions> getObjPerms(){
        return ObjectPerms;
    }
}
  • Thanks for the responses. I was trying to add the option to list out the objects and permissions by profile to an existing VF page that we have. I didn't want to add it to the current custom controller to minimize the potential of causing problems with it. That said, I'm new to apex development so any suggestions are welcome. I added the second suggestion, but am getting an error "Constructor not defined: [OLSHelper].<Constructor>()" – DGOU182 Jun 25 at 12:53
  • @DGOU182 - amended answer – cropredy Jun 25 at 14:42

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.