Introduction
AgentForce is a cutting-edge AI-powered platform that revolutionizes customer service automation within the Salesforce ecosystem. At websites, AgentForce features Salesforce Service Agent – an intelligent virtual assistant that seamlessly integrates into Salesforce Experience pages to deliver automated, context-aware customer support. Service Agent under AgentForce enhances the standard Einstein bot capabilities by providing real-time assistance, handling routine customer inquiries, and enabling sophisticated self-service options while maintaining the security context of logged-in users. This powerful combination allows organizations to deliver personalized customer experiences efficiently, significantly reducing the burden on human agents while ensuring high levels of customer satisfaction through immediate and contextually relevant support.
What is Context Variable?
Context variables in Service Agent work like memory slots that store important information about the logged-in user. Think of them as containers that hold specific details about who’s using the system right now. For example, when a customer logs into your Experience page, context variables can remember things like their name, account number, or user Id. This helps the Service Agent provide personalized responses without asking the user to repeat information we already possess.
Where to use Context Variables ? (Possible use cases)
     The key reasons why context variables are essential on Experience sites:
- Automatic User Recognition – Context variables instantly identify logged-in users, eliminating the need for customers to re-enter their information when interacting with the Service Agent.
- Security Management – They ensure users can only access their own data by operating within the logged-in user’s security context, maintaining data privacy and compliance.
- Intelligent Personalization – Service Agent can provide immediate, personalized responses by accessing user-specific data (like account details, case history) through context variables.
- Seamless Handoffs – When escalating from Service Agent to live agents, context variables ensure all user information transfers automatically, creating a smooth transition without information repetition.
Why do we need to ask for PII (Personally Identifiable Information) like Name or Email when the user has already logged-in to the site?
To solve this, we use the context variable in the service agent that can be set up by the steps in this article.
![](https://www.absyz.com/wp-content/uploads/2025/02/Fig1.webp)
Prerequisites
Required Salesforce editions and licenses:
- Enterprise, Performance, Unlimited, or Developer Edition withÂ
- Agentforce (Default) – to enable Agentforce
- Agentforce Service Agent Builder license
- Agentforce Service Agent User license
- User License: Einstein Agent license
Permission requirements:
- Agentforce Service Agent UserÂ
- Agentforce Service Agent Permissions
- Access Agentforce Default Agent – Gives users access to the default Agentforce agent in Salesforce.
- Agentforce Service Agent Configuration – Build and manage autonomous AI service agents
- Agentforce Service Agent Object Access – Access knowledge articles and manage cases and contacts as an autonomous AI service agent.
Agentforce Service Agent User – Analyze topics and perform actions as an autonomous AI service agent.
Guide to set up Service Agent with Context Variable
A. Enable Service Agent
- Navigate to Setup → Einstein Setup.
- Turn on Einstein toggle and refresh your browser.
- In the Setup → Agents.
- Turn on Agentforce toggle.
B. Create a Custom AgentForce Service Agent
- Navigate to Setup → agents → New Agent.
- Select Agentforce Service Agent option as this would be the only option available.
- Configure : As per the use case
Note : When it prompts to select a user for this agent, go with Create a new user option.
    4. Add context variables to include the MessagingSession.EndUserContactId field.
![Context Variable in Agent](https://www.absyz.com/wp-content/uploads/2025/02/Fig2-219x300.png)
   5. Save and activate the agent.
C. Permission Set to the Agent’s User Profile
- Go to Users in the Setup.
- Click the EinsteinServiceAgent User created in Step B.
- Scroll down to the Permission Set Assignments section and click Edit Assignments.
- Select the Agentforce Service Agent User & Agentforce Service Agent Permissions permission, add to the Enabled Permission Sets list.
- The Enabled Permission Sets list should look like: Agentforce Service Agent User and Agentforce Service Agent Permissions.
- Click Save
Â
D. Creating Flow Actions or APEX actions for Agent
Now, depending on the requirement of the use case that you are implementing, you can choose which type of Agent Action you want to create, either Flow Action or APEX Action, which will use the context variable as one of the input to get customer specific output.
For Flow Actions, create an input variable Resource with data type as Contact Record. Under Topic Configurations tab, update the instructions to get the contact details from Messaging Session instead of getting email address from customer
![Context Variable for APEX & Flow Action](https://www.absyz.com/wp-content/uploads/2025/02/Fig3-300x230.png)
- If the customer is not known, always ask for their email address and get their Contact record before running any other actions.
- Always use the MessagingSession.EndUserContactId found in context variables to get the Contact Details before running any other actions. Don’t ask customers about their email address.
- Never use user-provided information for this input.
For APEX Action, create the Apex class which implements your logic with a wrapper to get the input contact of the contact user logged-in on the experience site. The image given can be used for reference.
Note : Make sure that the profile of EinsteinServiceAgent User has the access to the APEX class for which the action is created. If this access is not given, the agent will be able to execute the action.
![Wrapper to capture Context Variable](https://www.absyz.com/wp-content/uploads/2025/02/Fig4-1-768x73.png)
E. Create a Routing Configuration
- Navigate to Setup → Omni-Channel → Routing Configuration → New
- Configure the following Routing Configuration and Save
![](https://www.absyz.com/wp-content/uploads/2025/02/Fig5-768x353.png)
F. Create a Messaging Queue
- Navigate to Setup → Queues → New Queue.
- Configure the following:
- Queue Name: Enter a name (e.g., “Messaging Queue”).
- Object: Select Messaging session
- Routing Configuration: Select the routing configuration created in Step D
- Add Einstein User, EinsteinServiceAgent User and user who has profile of System Administrator (here Neha Mewada) in the Queue as shown in image below.
- Save the queue.
![](https://www.absyz.com/wp-content/uploads/2025/02/Fig6-768x363.png)
G. Create an Omni-Flow
- Navigate to Setup → Flow → New Flow.
- Select Omni-Flow and click Next.
- Add the following elements to the flow:
    a. New Resource
![Resource to capture logged-in User’s ID and Messaging Session ID](https://www.absyz.com/wp-content/uploads/2025/02/Fig7-254x300.png)
    b. Get Record element to get Contact UserÂ
![](https://www.absyz.com/wp-content/uploads/2025/02/Fig8-297x300.png)
    C. Get Record element to get Messaging Session
![](https://www.absyz.com/wp-content/uploads/2025/02/Fig9-294x300.png)
    d. Update Record element to update Messaging User
    Value: {!Get_Messaging_Session.MessagingEndUserId}
![](https://www.absyz.com/wp-content/uploads/2025/02/Fig10-263x300.png)
     e. After Update Messaging User, add Route Work element:
        Route To: Select Agentforce Service Agent.
        Agent: Choose the custom agent created in Step B.
        Fallback Queue: Select the Messaging Queue.
![Route Work (i)](https://www.absyz.com/wp-content/uploads/2025/02/Fig11-300x94.png)
![Route Work (ii)](https://www.absyz.com/wp-content/uploads/2025/02/Fig12-768x239.png)
      g. Save and activate the flow.
      h. The flow should look like this:
![](https://www.absyz.com/wp-content/uploads/2025/02/Fig13-188x300.png)
H. Create a Service Channel
- Navigate to Setup → Messaging Settings → Service Channels.
- Click New Service Channel and choose Messaging for In-App and Web
- Configure the following:
- Name: Enter a name (e.g., “ESA Channel”).
- Routing Type: Select Omni-Flow.
- Flow Definition: Choose the flow you have created in Step G.
![Service Channel](https://www.absyz.com/wp-content/uploads/2025/02/Fig14-768x350.png)
     e. Create a new Parameter under the Custom Parameters section.
![Custom Parameter](https://www.absyz.com/wp-content/uploads/2025/02/Fig15-768x76.png)
     f. Map the channel parameters with the flow variables.
     g. Go to Parameter Mappings section and click New
![Parameter Mapping](https://www.absyz.com/wp-content/uploads/2025/02/Fig16-768x82.png)
    h. Save the service channel.
I. Configure Embedded Service Deployment
- Navigate to Setup → Embedded Service Deployments → New Deployment.
- Select Web Messaging.
- Configure the following:
- Name: Enter a deployment name (e.g., “Public Chat Deployment”).
- Endpoint Domain: Enter the domain of the Experience site (Setup → Domains → Copy Experience site domain)
- Under Messaging Settings, select the Service Channel you created in Step H.
- Save the deployment.
![Embedded Service Deployment](https://www.absyz.com/wp-content/uploads/2025/02/Fig17-768x370.png)
J. Publish the Experience Cloud Site
- In the Setup Quick Find, search and select All Sites.
- Click New
- Choose a template from the Experience Workspaces Creation wizard
- Click Get Started
- Name the site and add it to the URL
- Click Create
![Experience Site with Service Agent](https://www.absyz.com/wp-content/uploads/2025/02/Fig18-1024x437.png)
    7. Configure the site according to your requirements
    8. Click the Components widget .
    9. Search Embedded Messaging and drag and drop the component over any     section.
     Note : exact placement is up to you, and it may take a few seconds for the    component to appear.
   10. In the Experience Site Builder, Go to Settings → Advanced → Edit Head Markup,   paste the following sample script into the Head Markup
       <script type=‘text/javascript’>
       window.addEventListener(“onEmbeddedMessagingReady”, e => {
       embeddedservice_bootstrap.prechatAPI.setHiddenPrechatFields({
         “Customer_User_Id”: $A.get(‘$SObjectType.CurrentUser.Id’)
             });
           });
       </script>
     11. Update the Content Security Policy to Relaxed CSP and publish it.
![CSP of Experience Site](https://www.absyz.com/wp-content/uploads/2025/02/Fig19-768x371.png)
Troubleshooting Guide
- If you cannot see your agent on experience site, update Set Security Protocols: Add your Experience Builder site domain to your CORS allowlist
Setup → CORS → New → Experience Cloud site domain
![CORS Setting](https://www.absyz.com/wp-content/uploads/2025/02/Fig20-768x379.png)
2. Make sure API name of Omni-flow variable (here Customer_User_Id), Name of variable for Script of Experience site Header (here Customer_User_Id) and Name of Custom Parameters and Parameter Mappings (here both are Customer_User_Id) variables are same.
Conclusion
AgentForce Service Agent, powered by Salesforce’s AI capabilities, has revolutionized how businesses handle customer service on Experience sites. By integrating context variables into the Service Agent deployment, organizations can now deliver truly personalized and secure customer experiences. This comprehensive guide has walked through the essential steps of implementation. The seamless interaction between AgentForce and context variables creates an efficient support ecosystem that adapts to each user’s specific needs and context, ultimately delivering the responsive, personalized service that modern customers expect.Â
For successful implementation, remember to:
- Follow the prerequisites carefully
- Pay attention to permission settings
- Ensure consistency in variable naming across all configurations
Authors: Jashuva Pilla & Yogesh Kumar Soni