Earn a Salesforce AI Certification by IIT + 12-Week Internship Secure Your Spot Today!

Earn a Salesforce AI Certification by IIT + 12-Week Internship Secure Your Spot Today!
Working With Variables in Salesforce Flow

Working With Variables in Salesforce Flow | saasguru

In the vast realm of programming, a variable acts like a container or a storage space. Imagine you have a physical box and can put something inside it – maybe a toy, a book, or a piece of clothing. In programming, instead of tangible items, this “box” (i.e., the variable) can store values, such as numbers, characters, or even more complex data structures. Like every box has a label that tells you what’s inside it, a variable has a name, making it easy to retrieve or change the stored value simply by referencing this name.

What will you learn:

  • Understand the fundamentals of variables in salesforce flow and their distinction from constants.
  • Dive into Salesforce-specific variables, their types, and usage.
  • Understand the process of setting and manipulating variables within Salesforce.

Distinction between Variables and Constants

  1. Mutability: This is the core difference. A variable, as its name implies, can vary. This indicates that a variable’s value may change while running a program. For example, if you have a variable representing a bank balance, this value can increase or decrease based on deposits and withdrawals. Conversely, a constant is a value that, once set, cannot be changed during the program’s execution. 
  2. Purpose & Usage: Variables are generally used for values that need to be modified or manipulated as the program runs. Conversely, constants are ideal for values that should remain consistent throughout the program’s life, such as mathematical constants (like π) or fixed configuration values.
  3. Declaration: Most programming languages have distinct ways of declaring constants and variables. Depending on the language, constants often use keywords like const, final, or read-only, signalling to the compiler or interpreter that their values should remain unchanged post-initial assignment.

In essence, while both variables and constants provide storage mechanisms in programming, their primary distinction lies in their flexibility and intended usage. Variables offer adaptability, adjusting to the dynamic needs of a program, while constants ensure stability and consistency in values that shouldn’t be altered.

Online Bootcamps India

Types of Variables in Salesforce

In Salesforce, particularly when working with its proprietary language, Apex, developers will encounter various variable types. These kinds are essential for an application’s proper operation as they specify the type of data that can be stored in the variable. Let’s break down these types:

1. Primitive Data Types: These are the basic types that represent simple values. Examples include:

  • Integer: Represents whole numbers.
  • Double: Represents floating-point numbers.
  • String: Represents sequences of characters.
  • Boolean: Represents true or false values.
  • Date: Represents a date.
  • Datetime: Represents a date and time.
  • Blob: Represents binary data.

2. sObjects: Salesforce Objects, abbreviated as sObjects, represent objects in Salesforce. They can be standard objects like Account or Contact or custom objects you’ve created in your org.

3. Collections: These are more complex data structures that can store multiple values:

  • List (or Array): An ordered collection of elements. Lists can contain primitive data types or sObjects.
  • Set: An unordered collection of unique elements.
  • Map: Contains key-value pairs. You can think of it as a dictionary where you look up a value (like a definition) using a unique key (like a word).

4. Enum: represents a set of predetermined values. Enums are frequently employed to represent constants that have a limited range of possible values.

Difference between Local and Global Variables

Understanding their scope is paramount when working with variables in any programming environment. In Salesforce’s Apex, as in many other languages, variables can be categorized based on their accessibility:

1. Local Variables:

  • Scope: These are declared within a method or a block, and their scope is limited to that method or block. They can’t be accessed outside of where they’re defined.
  • Lifetime: The existence of a local variable is limited to the execution of the block or method in which it’s declared. Once the method or block execution finishes, the local variable is destroyed.
  • Usage: Local variables are primarily used for temporary storage and intermediate operations within a specific function or procedure.

2. Global Variables:

  • Scope: These have a method- or block-specific scope because they are declared within those specific methods or blocks.
  • Lifetime: A local variable can only exist while the block or method in which it was declared is running.
  • Usage: These are used when multiple methods in a class need access to the same data.

How to Set Variables in Salesforce

Setting variables in Salesforce is fundamental to working within the platform, especially when crafting custom functionality using the Apex programming language. Here’s a straightforward guide on how to set variables in Salesforce without delving into the specifics of code:

1. Choose the Data Type: The first step in setting a variable is to determine the kind of data it will store. Salesforce offers a range of data types, from basic ones like whole numbers (Integers) and text (Strings) to more complex types that can hold a collection of items or represent Salesforce objects (sObjects).

2. Declare the Variable: Before a variable can be used, it must be introduced or “declared.” This involves specifying the variable’s data type and giving it a name. The name should ideally be descriptive enough to indicate the variable’s purpose.

3. Assign a Value: After declaring a variable, you can assign it a value or “assign” data. The kind of value a variable can hold depends on its data type. For example, a variable declared to hold text (String) can be assigned a name or a phrase.

4. Working with sObjects: When dealing with Salesforce objects or sObjects, there are typically two scenarios:

  • Querying involves fetching existing records from the database and storing them in a variable.
  • Creating New Instances: You’re preparing a new record to be added to Salesforce. The variable holds the data until you save or “insert” it into the system.

5. Handling Collections: Salesforce offers data structures that can hold multiple items:

  • List: An ordered collection of items. Imagine a line of people waiting at a bank. Each person (item) has a specific position in that line.
  • Set: A collection where each item is unique, much like a real-world set of cards.
  • Map: There are pairs of keys and values in this. Consider it to be like a dictionary where each key (word) has a definition (value).

6. Constants: Sometimes, you’ll want to set a variable whose value shouldn’t change as the program runs. In Salesforce’s Apex language, such a variable is termed a “constant.” After setting its initial value, this variable remains unchanged.

Creating a Variable

Variables are essential for storing and managing data in your Salesforce Flows. Whether you choose to create a variable when you need it or in the Toolbox, the process is quite straightforward. In this guide, we’ll explore how to create a basic text variable using the Toolbox. Feel free to use the method that suits you best.

Step 1: Start by accessing the Setup menu, then enter “Flows” in the Quick Find box, and click on “Flows.”

Step 2: Click on the “New Flow” button to begin creating a new flow.

Step 3: Select the desired flow type and click “Create.” Any flow type will suffice, as they all support variables. However, for now, it’s best to avoid “Record-Triggered Flow” as it involves additional setup steps.

Step 4: Toggle the Toolbox to display it on the screen.

Step 5: Under the Toolbox section, click on “New Resource.” You can also hide the Toolbox at any time by clicking “Toggle Toolbox.”Screenshot 2023 10 23 at 2.55.10 PM

Step 6: Choose “Variable” as the Resource Type.

Step 7: Provide an API name and description for your variable.

Step 8: For Data Type, select “Text.”Screenshot 2023 10 23 at 2.55.36 PM

Step 9: Click “Done” to complete the variable creation process.

That’s it! You’ve successfully created a container for storing data, and you can easily access it in the Toolbox.Screenshot 2023 10 23 at 2.55.54 PM

As you went through the variable creation process, you may have noticed other settings available. Don’t worry if you’re unsure about these options; they will be covered in more detail in other Flow tutorials. As a general guideline, avoid enabling any of these settings unless you have a specific need for them. For now, you can take pride in creating your very first variable!

Become a Salesforce Certified Professional

Conclusion

The journey to mastering Salesforce development continues after understanding variables. Instead, it’s a vital stepping stone that paves the way for deeper exploration into the platform’s expansive capabilities. Whether you’re a seasoned developer or just starting, the efficient use and understanding of variables will always remain a cornerstone of effective and efficient application development.

Join us at saasguru’s Slack community, where the Salesforce horizon expands daily, unveiling new patterns and the latest innovations. In this thriving hub, engage in thought-provoking dialogues, exchange wisdom, and network with Salesforce aficionados. As you dive deeper, explore the art of passing variables between flows, a vital skill in the Salesforce domain.

Furthermore, saasguru offers an exclusive gateway to exceptional offers and online bootcamp programs designed to fuel your Salesforce journey. Seize the moment and chart your Salesforce bootcamp with us!

Table of Contents

Subscribe & Get Closer to Your Salesforce Dream Career!

Get tips from accomplished Salesforce professionals delivered directly to your inbox.

Looking for Career Upgrade?

Book a free counselling session with our Course Advisor.

By providing your contact details, you agree to our Terms of use & Privacy Policy

Unlock Your AI -Powered Assistant

Gain Exclusive Access to Your Salesforce Copilot

Related Articles

Mass Assign User Permission Set in Salesforce

Learn how to efficiently mass assign user permission sets in Salesforce using tools like Data Loader. Save time and ensure accuracy. Read now!

How to Clone a FlexCard in Salesforce

Learn how to clone a FlexCard in Salesforce with this step-by-step guide. Simplify customization and boost efficiency effortlessly!

Salesforce Introduces Agentforce 2.0: Redefining How Work Gets Done

Salesforce introduces Agentforce 2.0, the AI platform transforming digital labor with autonomous agents and seamless workflows. Read now!