runAs Method in Salesforce

Generally, all Apex code runs in system mode, and the permissions and record sharing of the current user are not taken into account. The system method runAs enables you to write test methods that change either the user contexts to an existing user or a new user. When running as a user, all of that user's record sharing is then enforced. You can only use runAs in a test method. The original system context is started again after all runAs test methods complete.

The following items use the permissions granted by the user specified with runAs running as a specific user:
  • Dynamic Apex
  • Methods using or
  • Shared records
The original permissions are reset after runAs completes.
The runAs method ignores user license limits. You can create new users with runAs even if your organization has no additional user licenses.

No comments:

Post a Comment