[SalesForce] How to query list of roles that has access to a Account record shared via sharing rule

A sharing rule exists in Account which under certain criteria shares the record with a Role.

Is there a way we can query AccountShare object or any other table to check what Roles are Accounts shared with?

The relatedId in Group object says "For Groups of type “Role,” the ID of the associated UserRole."

Best Answer

Yes sure.

You can use SOQL like bellow:

SELECT AccountAccessLevel , AccountId , CaseAccessLevel , ContactAccessLevel , Id , IsDeleted , LastModifiedById , LastModifiedDate , OpportunityAccessLevel , RowCause , UserOrGroupId FROM AccountShare WHERE RowCause = 'Rule'

This will give you all account rows those are shared to Role. If you are interested in perticuler role then you can add condition like AND WHERE UserOrGroupId = 'ROLE_ID_XXXX'