# How do Einstein Bot context variables preserve customer session data during bot-to-agent transfers?

**Topic:** Service Cloud · **Difficulty:** Medium · **Tags:** Service Cloud, Einstein Bots, Context Variables

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-einstein-bot-context-variables-preserve-customer-session-data-during-bot-to-agent-transfers

## Answer

**Basics:** Einstein Bots (now alongside the newer Agentforce service agents) use two kinds of variables. Conversation variables are collected or computed during the chat. Context variables are populated from the session: for example ContactId, CaseId, LeadId, EndUserLanguage, or custom values mapped from pre-chat fields. They let the bot greet a known customer and skip questions.

**How it works:**
1. In Bot Builder, open Variables and create a context variable, choosing a data type.
2. Link it to a source: a standard field on the messaging or chat record, or a pre-chat or session field.
3. Use it in dialogs, rules (for example tier = Gold skips verification) and actions.
4. On Transfer to Agent, the bot routes through Omni-Channel; the session record (MessagingSession for Messaging for In-App and Web; legacy chat transcript for Chat) carries mapped fields, so the agent sees them. Variables not mapped to a record field are lost when the bot ends.

**Advanced / deep dive:** Authentication state typically comes from a verified user or pre-chat data, and should never be trusted from an unverified client value. Pass only what agents need and avoid storing sensitive data in transcripts. Use a Flow or Apex bot action to enrich context (for example look up entitlement tier) and write results to conversation variables. To preserve data through a transfer, map variables to fields on the session object and display them in the agent console. Legacy Chat is being replaced by Messaging; verify current status in release notes.

**Code / config example:**
```apex
public with sharing class BotLookup {
    public class Req { @InvocableVariable(required=true) public String contactId; }
    public class Res { @InvocableVariable public String tier; }
    @InvocableMethod(label='Get Account Tier')
    public static List<Res> getTier(List<Req> reqs) {
        Set<Id> ids = new Set<Id>();
        for (Req r : reqs) ids.add(r.contactId);
        Map<Id, Contact> m = new Map<Id, Contact>([SELECT Id, Account.Type FROM Contact WHERE Id IN :ids]);
        List<Res> out = new List<Res>();
        for (Req r : reqs) {
            Res x = new Res();
            x.tier = m.containsKey(r.contactId) ? m.get(r.contactId).Account.Type : null;
            out.add(x);
        }
        return out;
    }
}
```

**Hidden gotchas & must-remember points:**
- Context variables are read at session start; changed values do not update the source record unless you write them.
- Unmapped variables disappear after transfer.
- Pre-chat field names must match exactly.
- Invocable actions must be bulk-safe even though bots send one request.
- Bot users need Apex class access.
- Do not put passwords or sensitive IDs in transcripts.

**Best practice:** Map the minimum useful context to the session record and document each variable's source.

---
Source: ForceChallenges · last updated 2026-11-02
