+ Start a Discussion
Sumeet_ForceSumeet_Force 

Apex not running in System mode even if [with sharing] is not used.

Hi,
I have a VF page and some apex code in related controller. The VF is ran by a user having limited access based on his profile. However the code tries to update a contact which results in a privilege error and says [Update access denied for Contact] which seems an issue for currently runnign user. This is fine as user profile doesnt allow him to edit/update Contacts. However if I have not used [with sharing], then ideally it should run in System mode which is somehow not happening. Can someone pls help by pointing out issue  or some direction pls ?
Siddharth ManiSiddharth Mani
As per the below link, Apex will always enforce "with sharing" if nothing is declared. Try using "without sharing" in your class declaration.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_sharing.htm
Ravi Dutt SharmaRavi Dutt Sharma
Hey Sumeet,

Can you explicitily mark the class as without sharing and let me know what the output in that case
Sumeet_ForceSumeet_Force
Hi Sid/Ravi,
Just now tried using without sharing and also by removing Edit permission on Contact for running user and it somehow failed with same error:

19:57:37.0 (24197099)|EXCEPTION_THROWN|[EXTERNAL]|System.VisualforceException: Update access denied for Contact, controller action methods may not execute
 
Ravi Dutt SharmaRavi Dutt Sharma
It should not happen this way. Do you mind sharing the code?
Ravi Dutt SharmaRavi Dutt Sharma
http://aluniya.blogspot.in/2014/02/salesforce-system-mode-user-mode-and.html 

As per this article, "with sharing" and "without sharing" keywords has nothing to do with CRUD permission, they only play role in selecting records on sharing basis.
So in your profile, if you do not have Edit permission on Contact object, you wont be able to update that contact record even if your class is without sharing. Without sharing will only help you to query the contact records on which do not have access.
Sumeet_ForceSumeet_Force

Hi Ravi,
Main lines causing the code:

Contact c = new Contact (Id=variableId);
c.Field = 'SOME VALUE HERE';
update c;

thats the main part causing error....it also does other things but above code i.e update statement is part causing the error.

Hi Ravi,
Everywhr its mentioned that in system mode , it will run in system context and can perform almost everthing ...not very sure if its restricted to only the query part.

Sumeet_ForceSumeet_Force
hi ravi,
snippet from link you mentioned says same thing: read below pls:

System mode is nothing but running apex code by ignoring user's permissions. For example, logged in user does not have create permission but he/she is able to create a record.
In system mode, Apex code has access to all objects and fields— object permissions, field-level security, sharing rules aren't applied for the current user. This is to ensure that code won’t fail to run because of hidden fields or objects for a user.
pratap murukutla 17pratap murukutla 17
i apologize for the delay. is this vf page from sites?