I have a list of users and I want to show which custom objects each user has access to. Instead I am getting a list of all custom objects for each user

public Set<string> getCus_obj() {
    Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
    FOR (User u: user_label){
        for(Schema.SObjectType f : gd.values())
            Schema.DescribeSObjectResult ds = f.getDescribe();
            if(ds.isCustom() == true && ds.isAccessible())
           return cus_obj;
          return null;

public List<User> user_label {get;set;}
Set<String> cus_obj = new Set<String>();

What do I need to change in my code to get the desired list of objects by user?

  • 1
    check the user profiles users may have the Accessibility but may not have the write or read permissions so based on that you may need to change your if conditions while fetching the object names.
    – Suresh
    Jul 8 '15 at 19:53

The describe APIs return information that related to the current running User. In non-test code, there is no way to switch the User, hence in your code you get the same results for each User because your User loop has no effect. (In tests System.runAs can be used.)

There is a UserRecordAccess object that can be queried via SOQL. It has various constraints on what you can do (see the link), but for example in a dev org of mine I can run:

select RecordId, HasReadAccess, HasEditAccess, HasDeleteAccess
from UserRecordAccess
where UserId = '00550000001J0oE'
and RecordId in ('0015000000cura9AAA', '0035000001C44pO')

and get different results depending on the UserId I specifify. Unfortunately you have to supply both where terms.

So to use this for what you are looking to do you would have to:

  • Use dynamic SOQL to query to find a sample record ID of each type returned by the global describe and add them to a set; presumably it's specific IDs because if sharing rules are turned on you would get different results for different records of the same type
  • Query to find all the UserId values of interest
  • Execute a query similar to the above for each UserId using the set of record IDs obtained in the first step

I've never coded this up - just a best guess based on the documentation and running the query.

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.