[SalesForce] Doing query as specific user

Situation:

  • We are doing SOAP API (custom made apex webservice) calls to service using admin user.
  • Those calls do search on Accounts (millions of records, search mostly on Name field)
  • Account are set Private (org-wide) with many sharing rules and hierarchy sharing
  • Calls pass userId (or email) of user in whose context result should be returned

Is there a way to apply sharing restrictions to the query I'm doing?

I have tried following:

  • Use UserRecordAccess but it only allows to query for 200 records which is definitely not enough
  • Use AccountShare table but it contains Groups for Role and Subordinates, which doesn't have any members in GroupMember table that means I would have to query for all the roles in role hierachy (which we have over 1000) also this table doesn't include sharing above in the hierarchy

Any ideas?

Best Answer

The only way to automatically enforce sharing is to log in as the user whom the query should be performed as, not as an admin user. As you've noted, using UserRecordAccess is exceptionally limited, so you'd have to perform many queries based on an initial query result. Assuming the account query returned less than about 20,000 rows, you could probably use UseRecordAccess without running out of query statements (and if it's more than that, you should consider narrowing the results anyways).

Related Topic