How to make Global variables in salesforce?


In Salesforce, global variables are the variables that can be accessed from any Apex code or Visualforce page in a Salesforce ORG. You can use these variables to store information that needs to be accessed and used by multiple functions, classes, pages, or triggers. There are two types of global variables:

  1. Apex Global Variables
  2. Visualforce Global Variables

Apex Global Variables

Apex global variables are the variables that are defined at the top of an Apex class and are accessible from anywhere within the class. They are commonly used to store values that are used frequently or that need to be shared across multiple methods within the class. You can create your global variable using the “static” keyword.

Global Variables

To create a global variable in apex, you should define the variable at the top of the class, outside of any method or block of code. You can also define data type of that variable and give that an initial value.

public static String myGlobalVariable = 'Initial value';

You can then use the global variable anywhere within the class by simply referring to its name. For example:

myGlobalVariable = 'New value';

Visualforce Global Variables

Visualforce global variables are the global variables that are defined on a Visualforce page. They are accessible from any component on the page. They are generally used to store values that are used continuously or that require to be shared across multiple components on the page.

To create a Visualforce global variable, you must define it in the tag of the page using a tag. You can define the name and default value of the variable using var and value attributes, respectively. For example:

<apex:page controller="MyController" showHeader="false" standardStylesheets="false" applyHtmlTag="false" applyBodyTag="false">
    <apex:variable var="myGlobalVariable" value="Global variable value"/>

You can then use the global variable in any component on the page by simply referring to its name. For example:

<apex:outputText value="{!myGlobalVariable}"/>

Remember, global variables should be used periodically. Perhaps, you can use local variables and class-level variables rather than using global variables as they are very difficult to understand and maintain.

You can simply refer the global variables by their name to use them in your code and Salesforce automatically determines them to their corresponding values. Some of the variables may have limited availability depending on how they are used.

Please contact us at or call us at +91-8800519180 for any support related to Salesforce. You can also visit the Salesforce development page to check the services we offer.