+ Start a Discussion
vandana rajuvandana raju 

INSUFFICIENT_ACCESS_OR_READONLY

Hello friends
I am getting the error " INSUFFICIENT_ACCESS_OR_READONLY" when I execute apex code from trigger and the logic performs an DML operation.
I went through several posts to understand this error I understood that in the context of current user not having permission to perform edit on the object is resulting in this error.

The code was written by another user with 'WITH SHARING" keyword and also he has written as test class.
I checked with sysadmin so I do not have permissions to perform any edit on the said object.


My question is how do I resolve this error.

A) Can I actually remove the "WITH SHARING" keyword and then run the code from my login ?
B) In test class can I remove the option of 'WITHOUT SHARING"?
C) Also Pls lt me know how system.RunAs() works in the above context.
D) as of now this code is not deployed, so once it is deployed if this error occurs pls explain how to to resolve

I really hope I am clear.
Thanks
vandana R

 
Best Answer chosen by vandana raju
Lalit Mistry 21Lalit Mistry 21
Hi Vandana,

Ideally you should be executing your apex code in with sharing mode to ensure all sharing settings are enforced, but there can be exception scenarios where you need to run apex in system mode. Considering yours been the later case, you can update your class to run in without sharing mode i.e. update 'with sharing' with 'without sharing'.
In addition to that, you might also need to update your test class if there is any test method which tests for sharing.

So to answer your question,
1. removing with sharing wont help you because that would mean that your class will run in context of the caller, which can be either with or without sharing. You'll explicitly need to run your class in without sharing mode.
2. with or without sharing on test class do not have any impact on test execution.
3. System.runAs() is used in test classes to switch the user context. So if you execute your test class as system admin and want to test for apex execution for an user with different profile then you can use System.runAs() option by providing the user from different profile.
4. Same as step 1.

Hope I answered all your questions.

All Answers

Lalit Mistry 21Lalit Mistry 21
Hi Vandana,

Ideally you should be executing your apex code in with sharing mode to ensure all sharing settings are enforced, but there can be exception scenarios where you need to run apex in system mode. Considering yours been the later case, you can update your class to run in without sharing mode i.e. update 'with sharing' with 'without sharing'.
In addition to that, you might also need to update your test class if there is any test method which tests for sharing.

So to answer your question,
1. removing with sharing wont help you because that would mean that your class will run in context of the caller, which can be either with or without sharing. You'll explicitly need to run your class in without sharing mode.
2. with or without sharing on test class do not have any impact on test execution.
3. System.runAs() is used in test classes to switch the user context. So if you execute your test class as system admin and want to test for apex execution for an user with different profile then you can use System.runAs() option by providing the user from different profile.
4. Same as step 1.

Hope I answered all your questions.
This was selected as the best answer
vandana rajuvandana raju
Hi Lalit

A) so as per ur post so when I run the apex code from my login ,I should mention as "WITHOUT SHARING" so can I do this manually from my
     login or should I contact system admin?
B) Assume test class defined as  'WITH SHARING " option, so it has no impact on execution.
C) so for testing purpose , test class I should run from sys admin profile and not from my profile(with less permisisons)?

Please validate the above stmts
Thanks
Vandana R
Lalit Mistry 21Lalit Mistry 21
My reply as below
A) Only System Admin should be able to update the apex class, so you'll need to get in touch with your system admin if you are not the admin.
B) With or without sharing will have no impact on test class. Test classes are always run in system mode unless you use System.runAs().
C) Ideally you should be testing with different profiles, but for code coverage purpose, you can do away with profile testing.

 
vandana rajuvandana raju
Hi Lalit
Thanks for validating.
On more thing, can u share any links or frm ur experience any apex errors that may happen during deployment ?
Thanks
Vandana R
Lalit Mistry 21Lalit Mistry 21
It will be difficult to point you to a single list of deployment exceptions, but if both your production and sandbox configurations are in sync with only differences that you plan to deploy then its more likely your deployment will succeed without any hickups as long as your tests are successfully run and overall coverage is more than 75%.
That said, in case you encounter any issues, people on this forum should be able to help you out :)