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:
- In Bot Builder, open Variables and create a context variable, choosing a data type.
- Link it to a source: a standard field on the messaging or chat record, or a pre-chat or session field.
- Use it in dialogs, rules (for example tier = Gold skips verification) and actions.
- 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:
public with sharing class BotLookup {
public { String contactId; }
{ String tier; }
List<Res> {
Set<Id> ids = <Id>();
(Req r : reqs) ids.add(r.contactId);
Map<Id, Contact> m = <Id, Contact>([SELECT Id, Account.Type FROM Contact WHERE Id IN :ids]);
List<Res> out = <Res>();
(Req r : reqs) {
();
x.tier = m.containsKey(r.contactId) ? m.get(r.contactId).Account.Type : ;
out.add(x);
}
out;
}
}