System mode or God mode in Apex – Gotchas

Gotchas of System mode or God mode in Apex and its impact by using “With sharing” keyword in Salesforce.

There is no specific Salesforce documentation which can explain that what is allowed or not allowed in System mode.

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) {

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;

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”.

Salesforce God Mode failing

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 :

  1. 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.
  2. 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

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Create a free website or blog at

Up ↑

%d bloggers like this: