Agentforce gives you the flexibility to design intelligent chat assistants that respond differently based on your users' inputs and business logic. Two powerful tools that help make this happen are Context Variables and Custom Variables.
In this blog, we’ll break down what each of these are, how they work, and how to use them effectively in your Salesforce org to fine-tune your agent's behavior and decision-making flow.
Think of them as hidden tools behind the scenes helping your agent know what to do and when to do it.
What Are Context and Custom Variables?
Context and Custom Variables help you manage your assistant’s logic and response flow based on specific user actions, stages in the conversation, or system-level triggers.
Let’s understand both one by one:
Context Variables
Context Variables are built-in system variables provided by Salesforce that track the current state of the conversation and what the user is trying to do.
You don’t need to manually create these — they’re already available for use in your conditions, filters, and logic branches.
Some commonly used context variables include:
Context Variable | Description |
---|---|
CurrentIntent | The latest intent detected in the user’s message |
CurrentEntityValues | Extracted entities from the user’s request |
UserInput | Raw text input from the user |
PreviousIntent | Intent from the previous message |
SessionId | Unique ID for the user’s chat session |
Use Case Example: If CurrentIntent is “Check Order Status”, you can tell the assistant to guide the user to the Order Info screen.
Custom Variables
While context variables are predefined, Custom Variables are created by you — the developer or admin — to track any custom data you need across the conversation.
They’re super useful when you want to:
- Store values the user enters (like email, phone, product name)
- Save system values returned from Flows or Apex Actions
- Use your own business logic for branching the flow
Custom variables must be manually defined in the assistant setup.
You can name them anything meaningful like:
- userEmail
- isVerified
- productName
- accountType
How Do Variables Work in Action?
Let’s walk through a practical example.
Imagine a customer is chatting with your assistant and asks:
"Can you check my support case status?"
Here’s what happens:
- Intent Detected → CurrentIntent = CheckCaseStatus
- Assistant asks for case number → Stores user input into caseNumber (a custom variable)
- Flow runs → Sends caseNumber into a Salesforce Flow to fetch case details
- Assistant replies with case status → Uses another custom variable caseStatus
This simple combination of context + custom variables gives you a smooth, intelligent response flow.
Using Filters to Control Dialog Execution
Variables are most useful when combined with Filters to control whether a dialog should run or not.
Here’s an example of a filter condition:
CurrentIntent = “GetAccountInfo” AND isVerified = true
This means:
- The user must have asked for account info
- AND they must have already passed verification
If both are true, the assistant proceeds with showing the account info.
Setting Variable Values
You can set variable values in multiple ways:
1. From User Input
Map values directly from what the user types:
- Prompt: “What is your email address?”
- Store in: userEmail
2. From Flows or Apex Actions
- Use outputs from your Flows to update variables like isVerified, caseStatus, or orderDetails.
3. With Expressions
- You can use expressions like isVerified = true or caseStatus != null to control logic.
Best Practices
- Use clear, meaningful names for custom variables like isCustomer, preferredLanguage, etc.
- Reuse context variables wherever possible to avoid duplication.
- Use Flows to centralize your logic and return values into custom variables.
- Always test your filters — make sure dialogs don’t fire unintentionally.
Wrap Up
Context and Custom Variables are essential tools to personalize and control how your agent responds inside Salesforce. With these in place, you can create assistants that are more intelligent, dynamic, and helpful to your users.
Use context variables to understand the situation, and custom variables to guide the conversation with data that matters.