I would like to retrive the Object,Apex Class,VF pages,Tab settings etc from a permission set via apex. I'm not able to find the exact query for it. Found the PermissionSet class in salesforce fits my requirement but not quite sure to implement it.

Any idea?


You'll have to query the ObjectPermissions, FieldPermissions, PermissionSetTabSetting, SetupEntityAccess, etc objects (most of which are children of PermissionSet). Use the SOAP API Developer Guide to discover the object names, fields, and so on that you need to use.

  • ObjectPermissions query is displaying the records which is having either of CRUD permissions only of an object. Is there a way to fetch the records having NO access too? – Rv1 Apr 24 at 10:49
  • @Rv1 No, you basically have to do a Describe Objects call and compare it to the query. See salesforce.com's example implementation for ideas. – sfdcfox Apr 24 at 11:13
  • Above github or link example doesn't have information related to Custom Apps,Record Type, System settings etc. Can't we fetch those? – Rv1 Apr 25 at 8:36
  • @Rv1 I don't use the metadata objects a lot, so I'm not 100% on everything you can query that way; for everything else, consider the metadata wrapper API, which can give you access to everything else. – sfdcfox Apr 25 at 14:12

MetadataAPI includes all the information you need. In addition, the link you shared refers to PermissionSet class which extends Metadata as mentioned there.

PermissionSet - Represents a set of permissions that's used to grant additional access to one or more users without changing their profile or reassigning profiles. You can use permission sets to grant access but not to deny access. This type extends the Metadata metadata type and inherits its fullName field.

The only thing is to know how to use MetadataAPI, below is an example which will help you.

  1. Go to mdapi in github and use the MetadataService and MetadataServiceExample classes.
  2. Write the code below in the developer console execute:

    MetadataService.MetadataPort service;
    service = MetadataServiceExamples.createService(baseURL, sessionId);   
    List<MetadataService.ListMetadataQuery> queries = new List<MetadataService.ListMetadataQuery>();        
    MetadataService.ListMetadataQuery queryForMetaData = new MetadataService.ListMetadataQuery();
    queryForMetaData.type_x = typeToQuery;
    String[] fullNames = new List<String>();
    MetadataService.FileProperties[] fileProperties = service.listMetadata(queries, 42);        
    for(MetadataService.FileProperties fileProperty : fileProperties){
    MetadataService.IReadResult readResult = new MetadataService.ReadPermissionSetResult();
     readResult = service.readMetadata(metadataType,fullNames);
     List<MetadataService.Metadata> listForLoop = readResult.getRecords();     
     for(MetadataService.Metadata temp : listForLoop){
          MetadataService.PermissionSet obj = (MetadataService.PermissionSet) temp;
          system.debug(obj.classAccesses); // retrieves apex classes permission
          system.debug(obj.tabSettings); // retrieves tab setting
          system.debug(obj.pageAccesses); // retrieves visualforce pages    
          system.debug(obj.objectPermissions); // retrieves CRUD permission per object
  3. in the MetadataServiceExample class, pay attention that the sessionId there is set well.

Let me know if you face some issues.

  • there are few variables used. Tried to replace with static text like 'permissionset' for permissionset variable ended up with errors. Can you provide an valid example. – Rv1 May 2 at 10:21

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.