SalesforceBlue

Feel the rhythm of Salesforce

Apex

Apex Static Variables Simplified

If you are aware of Marvel cinematic universe then you must have seen Eternals. They are a race of near-immortal synthetic beings created by the Celestials. So what do Eternals have to do with Apex Static Variables?

Like Eternals Apex Static Variables live till the whole Apex transaction is completed. Kind of near-immortal 🙂

In more technical terminology, it’s a Class level member variable that can be accessed without creating an instance of that class. Let’s see the Static variable in action.

public with sharing class BlueUtility {
    public static Boolean isAdminUser = false;
}

In the above code block, we have declared a Static Boolean variable isAdminUser with a static keyword. That’s all is required to create a static variable.

Now let’s see some super powers of Static Variables and how we can use them to create some awesome code.

One of the cool ways to use the Static variable is in Decision Making during the program execution. See the below block of code in which we are preventing a list to be updated again in the same transaction.

public with sharing class AccountTriggerHandler {
    public updateAccounts(List<Account> accountList) {
        if(! RecursiveSwitch.isUpdated) {
            update accountList;
            RecursiveSwitch.isUpdated = True;
        }
    }
}
public with sharing class RecursiveSwitch {
    public static Boolean isUpdated = false;
}

In the above code blocks, we are preventing the accountList to be re-updated during the same transaction. This approach is used to prevent recursive trigger calls.

If you are familiar with Trigger by now feel free to take a quick peek over here – Apex Trigger Recursion or you can revisit this topic post completion of Apex Trigger.

Another cool way to use Static variables is to Hold Data in the same Transaction. For this, we would be refactoring the BlueUtility class mentioned at the top.

public with sharing class BlueUtility {
    public static User currentUser;

    public static User getCurrentUserInfo() {
        if(currentUser != null) {
            return currentUser;
        } else {
            currentUser  = [SELECT Id, Profile.Name FROM User WHERE Id = :UserInfo.getUserId()];
            return currentUser;
        }
    }
}

In the above code block, we are initializing currentUser only if it was null using a single SOQL call. So what special thing it does?

Suppose you have written a class and your colleague has written another class. Both of you are referring to and invoking the BlueUtility.getCurrentUserInfo() method in the same transaction itself.

If the above approach wasn’t in place then you would have ended up doing SOQL on User twice which is not a very good approach considering the Salesforce Governor Limits.

Static variables possess lots of abilities and give superpowers to your code. Now it’s up to your creativity in using them 🙂

We are using the SOQL query in the above block and if you wanted to get familiar with it then it’s the right time to take a quick peek over here – Apex SOQL

Thank you for visiting SalesforceBlue.com
If you have any queries feel free to write down a comment below 🙂


One thought on “Apex Static Variables Simplified

Leave a Reply

Your email address will not be published. Required fields are marked *