Skip to content

Instantly share code, notes, and snippets.

@brianmfear
Created February 9, 2022 17:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianmfear/e8b2e40b84af4862a643beb6bba5bebd to your computer and use it in GitHub Desktop.
Save brianmfear/e8b2e40b84af4862a643beb6bba5bebd to your computer and use it in GitHub Desktop.
Without Sharing overridden by System.runAs?
@isTest without sharing class wSharingTest {
@isTest static void test() {
Account a = new Account(Name='Name');
User u = [SELECT FIELDS(STANDARD) FROM User WHERE Id = :UserInfo.getUserId()].deepClone(false, false, false);
u.FederationIdentifier = '12345';
u.Alias = '12345678';
u.UserName += '.brian.fear';
u.CommunityNickname = '12345678';
u.ProfileId = [SELECT Id FROM Profile WHERE Name LIKE 'Standard%' LIMIT 1].Id;
System.runAs(new User(Id=UserInfo.getUserId())) {
insert a;
}
System.runAs(u) {
// This line fails with:
// System.DmlException: Delete failed. First exception on row 0 with id 0011T00002dJrIvQAK;
// first error: INSUFFICIENT_ACCESS_OR_READONLY, insufficient access rights on object id: []
delete a;
}
}
}
@navkn
Copy link

navkn commented Apr 15, 2022

It means In a System.runAs() method the sharing rules are enforced regardless of the apex class access modifier.
And In the above org, it shows that the account level sharing settings are set to public read/write but as the new user also present on the same hierarchy, it's not allowing to delete the record owned by the current user. This shows sharing rules are enforced forcefully when we use System.runAs().
This is very helpful information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment