In many of my previous implementations I have used advantage of Apex code running in System mode however never tried how far it can go in terms of capabilties. There is no specific Salesforce documentation which can explain that what is allowed or not allowed in System mode.
In this post, we will go through some scenario and will try to understand what really is possible in Apex System mode and when it can fail.
Before diving more lets discuss how many types of mode do we have in Apex ?
User Mode : As per this Salesforce post, all Profile level permissions, sharing rules and Field level security are enforced in Apex if it runs in User mode. Standard Controller and Anonymous Apex runs in User mode.
System Mode : Same post conforms that custom controller, trigger, Apex class, controller extension works in System mode. Means eventhough user does not have necessary profile level permission, record level permission or field level permission, but still they can perform any operation on it.
Creating test scenario
For testing purpose, I am going to use a user with “Chatter free” license and existing Opportunity record. Consider OWD for opportunity is “private”.
Apex class :
In this class, I am trying to update existing Opportunity
public class SystemMode_Scenario { public static void perform(){ Opportunity Opp = [SELECT NAME,StageName FROM Opportunity WHERE Id = '006K000000CD1fZIAT'] ; Opp.StageName = 'Negotiation/Review' ; update Opp; } }
Trigger on Feeditem :
Whenever chatter free user is posting any chatter comment, below trigger should run and execute method in Apex class
trigger updateOpportunityFromChatterComment on FeedItem (before insert) { SystemMode_Scenario.perform(); }
Chatter free user trying to update Standard Opportunity record – God mode in Action
As we know, Chatter free user cannot access any standard or custom objects. so as per documentation there is no way chatter user can access or update existing Opportunity.
However, if you try to add comment in chatter using chatter user, trigger will be able to update existing Opportunity eventhough user does not has CRUD permission at profile level. So, we can say Apex really works in GOD mode.
When Apex does not work in System or God mode – Gode mode failing because of “with sharing” keyword
Example 1 :
Update Apex class with below code, only difference you can spot is addition of “with sharing” keyword.
public with sharing class SystemMode_Scenario { public static void perform(){ Opportunity Opp = [SELECT NAME,StageName FROM Opportunity WHERE Id = '006K000000CD1fZIAT'] ; Opp.StageName = 'Negotiation/Review' ; update Opp; } }
If you try to add chatter comment, it will fail saying “List has no rows for assignment” because logged in user does not has access to existing Opportunity and Apex class is defined using keyword “with Sharing” and makes sense.
Example 2:
In this example, lets try to create new record for Lead object and therefore update Apex class with below code :
public with sharing class SystemMode_Scenario { public static void perform(){ Lead l = new Lead(LastName='Zaa',Company='Cognizant'); insert l; } }
In previous example, we have seen that Apex class failed because of “with sharing” keyword and it makes sense because chatter does not has record level permission.
In this example, we are not trying to access any existing record rather creating a new lead record. “with sharing” keyword only checks if user has access to existing record or not and therfore this code should work if Apex works in God mode.
However, code will still fail with below error saying “Invalid User Type”.
Conclusion:
So saying, Apex runs in God mode not realy true if class is defined using keyword “with sharing”. “with sharing” keyword not only checks record level permission however somehow it enforces profile level permission as well.
My two cents – Should we use System mode or rather I will say abuse God mode ?
So, there is way to get out of licensing cost. Consultants or developers may think that we can buy low cost license and using custom Apex and visualforce, same functionality can be achieved. However, I would not suggest because of below two reasons :
- Salesforce documentation clearly segregates capabilities of difference licenses and they can change System mode functionality anytime. In fact, they have already started it by introducing “with sharing” keyword.
- You’re violating the Master Service Agreement and/or related agreements. You could be charged for full licenses retroactively, and failure to pay is breach of contract that will result in a lawsuit for damages, plus the loss of data that could result.
I would suggest to go through this article of Abhinav as well, which also explains similar to what I am trying to explain here.
If any information here are not correct or you have different experience, please share in comment. I would try to update this post with your feedback.
Leave a Reply