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
vamsi varmavamsi varma 

Criteria Based sharing rules using apex

Hi i have to give sharing rules to different users and groups for an object.

I want give them using apex managed sharing rules.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_bulk_sharing_creating_with_apex.htm

https://developer.salesforce.com/page/Using_Apex_Managed_Sharing_to_Create_Custom_Record_Sharing_Logic

i have used this links as reference and i have listened that using apex we shouldnt write sharing rules.

is this right way to write criteria based sharing rules using apex, anyone please let me know.

Thanks in Advance,
Vamsi Varma.

Navee RahulNavee Rahul
Hello Vamsl,


As for i know, if you have applied sharing rule on Object. for user and groups.
it will work perfect in Standard functionality list view,edit,view.

if you need to achive the same using apex class, then you need to use with sharing keyword in class.

if you did not specify the with sharing keyword , then sharing rule will not be applied for the VF page you created.


Example:

Standard Object list view will apply standard sharing rule.

Custom VF page with Custom codes to list data will not apply sharing rule unless specified in the code.

mark if this answer helps you.

Thanks
D Naveen rahul.
AshlekhAshlekh
Hi,

Criteria based sharing rules are not created via Apex.

You can write apex to give the access of user via code which is called Apex Managed Sharing.

-Thanks
Ashlekh Gera