function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TM_MikeTM_Mike 

How do you determine current user sharing access for a record using apex?

In an apex custom controller, is there a way to determine the current user's sharing access for a custom object record?

Example:

//Standard building of a custom select list
List<SelectOption> theList = new List<SelectOption>{};

List<CustomObject__c> items = Database.query(query);
                        for(CustomObject__c i : items){
                                theList.add(new SelectOption(i.id, i.name));
                        }

 



//What I would like to do
List<SelectOption> theList = new List<SelectOption>{};

List<CustomObject__c> items = Database.query(query);
                        for(CustomObject__c i : items){
                               
                               //determine user sharing rights on record
                               boolean optionDisabled = false;
                               string?? userAccess = i.methodThatReturnsCurrentSharingAccess;
                               if(userAccess != 'read/write'){
                                         optionDisabled = true;
                               }
                                theList.add(new SelectOption(i.id, i.name, optionDisabled));
                        }

 



I understand that I would probably need to get the current user object using the global and pass that to whatever method determines the sharing access. Unless the user object has the method and I pass it the record object. I will look into that while I wait for a reply.

 

 

Mike

MarkWaddleMarkWaddle

To tell if an object can be edited or deleted by the current user:

 

Boolean userCanWrite = Schema.SObjectType.CustomObject__c.updateable;
Boolean userCanDelete = Schema.SObjectType.CustomObject__c.deletable;

This technique works with any type of object, standard  or custom. If you plan to use these values in a loop (like your example) it is best to store them in variables and reference the variables because there is a limit to the number of Schema calls you can make in one request.

 

 

Regards,

Mark

MarkWaddleMarkWaddle

I just realized that your question was related to sharing, and my solution above does not take into account sharing. I actually do not know how to inspect whether a specific record is editable by a user. The sharing rules can be quite complex, so simply inspecting the CustomObject__Share object would be very difficult. I am looking into this now as I am curious about this myself.

 

Mark

TM_MikeTM_Mike

Thanks Mark.

 

The schema lookup is useful to know too, but you are right that it only returns the raw data access settings from the profile. I read somewhere that there is a CustomObject__Sharing object created for objects that have apex sharing, managed sharing, and user defined sharing but that it doesn't store sharing related to role heiarchy. I don't know where they store the non-apex sharing rules.

 

Mike