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

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.

Salesforce System Mode fail- With Sharing keyword
Salesforce System Mode fail- “With Sharing” keyword

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
Salesforce God Mode failing

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 :

  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.

Related posts

  • Aaditya

    That’s Great article indeed, Jitendra !!!

  • Aditya

    Hi, smilar differenence in case of standard controller with extension. It was not allowing to query on the records on which i was not having access.

  • Ankush Somani

    Hi, Jitendra,

    I just came to your site today..and almost stick with it like fevi-quick. It has very good content.

  • Sudipta Deb

    Hi Jitendra –

    In my case, I have tested with below scenarios.

    Account OWD is private. “Sys Admin” Profile is having all access to Account, whereas “Cognizant Employee” profile is having only read access to Account. I have created an account with Sys Admin profile and manually shared with user – Pradipta Deb(Profile – “Cognizant Employee”) as read/write. I have written a custom controller (with sharing) and VF page to support Creation of Account and Updation of Existing Account.

    Now when I am running the VF page with Pradipta Deb, I am able to create the new Account where the Owner is Pradipta Deb. But Pradipta Deb’s profile i.e. “Cognizant Employee” is not allowed to create any new account. Seems like profile level permission is completely ignored here.

    Now when trying to update the existing account (The account created by Sys Admin and Manually Shared), I am able to do that. Seems like it is again ignoring profile level permission (read only) and considering only the manual sharing permission (read/write).

    So my understanding is that “with sharing” doesn’t care when creating the new record, it is not applicable when trying to edit the existing record. Could you please let me know whether my understanding is correct? Thanks in advance.

    Regards,
    Sudipta Deb

  • Anand Vivek

    Hi,

    As per your explanation though the chatter user did not have access to a particular object, the trigger worked in making changes to the particular object. But the same does not seem to work for Community License who do not have access to the tasks.

    When an action is performed by a Community user I am trying to close a task of a particular type. This does not seem to be possible unless the tasks ISVISIBLEINSELFSERVICE is marked as true. Tried the same by explicity marking a class as without sharing also but did not work. Am I doing something wrong?