Learn how to launch, manage, and enrich Agentforce conversations in your React Native application.
Launch a Conversation
There are two methods for opening the conversation UI:
launchConversation()
Opens the conversation UI. If an existing conversation is available, it’s preserved and the user can continue where they left off.
1import { AgentforceService } from "@salesforce/react-native-agentforce";23// Must configure first4await AgentforceService.configure({5 /* ... */6});78// Launch -- resumes existing conversation if available9await AgentforceService.launchConversation();
This is the recommended method for most use cases. It avoids discarding conversation history and provides a smoother user experience.
startNewConversation()
Closes any existing conversation and starts fresh. The previous conversation is terminated and a new one is created.
1// Force a fresh conversation2await AgentforceService.startNewConversation();
Use this when you explicitly want to discard the previous conversation, for example, when the user switches accounts, resolves an issue, or navigates to a different context.
Preconditions
Both methods require:
The SDK must be configured via configure(). If not, the native module rejects with NOT_CONFIGURED.
On Android, a current Activity must be available. If not, the promise rejects with ERROR: Activity not available.
Error Handling
1try {2 await AgentforceService.launchConversation();3} catch (error) {4 // error.code can be:5 // 'NOT_CONFIGURED' -- configure() was not called6 // 'LAUNCH_ERROR' -- SDK failed to start session (iOS)7 // 'ERROR' -- general error (Android)8 console.error("Launch failed:", error.message);9}
Close a Conversation
Terminate the current conversation and dismiss the conversation UI:
iOS: Tapping the close button in the chat view’s top bar
Android: Tapping the back arrow in the toolbar
Conversation Lifecycle
Understanding how conversations are managed helps you build better user experiences.
Conversation Reuse
Calling launchConversation() multiple times reuses the same conversation object. This means:
The user sees their previous messages when re-opening the chat.
The agent retains context from the ongoing session.
Additional context set earlier is still active.
When Conversations are Cleared
Conversations are cleared when:
closeConversation() is called
startNewConversation() is called
resetSettings() is called (clears everything)
Switching modes (for example, calling configure() with type: 'employee' after previously configuring type: 'service')
setEmployeeAgentId() is called with a different agent ID (iOS only)
Additional Context
Additional context provides contextual information to the Agentforce agent during a conversation. This helps the agent deliver more personalized and relevant responses.
setAdditionalContext() must be called after launchConversation() or startNewConversation(). The context is set on the active conversation object, which doesn’t exist until a conversation is launched.
The bridge validates context variables before sending them to the native SDK:
Type names are case-sensitive. Use 'Text' not 'text', 'Boolean' not 'boolean'. Invalid types cause an error.
Each variable must have a non-empty name and type. Missing fields cause an INVALID_CONTEXT error.
Validation errors are thrown synchronously before the native call, so you can catch them immediately.
The description field is only supported on Android. On iOS, it’s silently ignored.
Complete Example with All Types
This example demonstrates all 11 context variable types:
1await AgentforceService.setAdditionalContext({2 variables: [3 // Text -- simple string value4 {5 name: "customerName",6 type: "Text",7 value: "Jane Smith",8 },910 // Number -- numeric value (double precision)11 {12 name: "accountBalance",13 type: "Number",14 value: 15234.56,15 },1617 // Boolean -- true/false18 {19 name: "isVIPCustomer",20 type: "Boolean",21 value: true,22 },2324 // Date -- ISO date string25 {26 name: "memberSince",27 type: "Date",28 value: "2024-01-15",29 },3031 // DateTime -- ISO datetime string with timezone32 {33 name: "lastInteraction",34 type: "DateTime",35 value: "2026-03-11T10:30:00.000Z",36 },3738 // Json -- JSON data (can be string or object)39 {40 name: "preferences",41 type: "Json",42 value: {43 language: "en",44 timezone: "America/Los_Angeles",45 notifications: true,46 },47 },4849 // List -- array of values50 {51 name: "recentOrderIds",52 type: "List",53 value: ["ORD-2026-001", "ORD-2026-002", "ORD-2026-003"],54 },5556 // Money -- monetary value57 {58 name: "creditLimit",59 type: "Money",60 value: 50000,61 },6263 // Object -- key-value map64 {65 name: "shippingAddress",66 type: "Object",67 value: {68 street: "123 Market Street",69 city: "San Francisco",70 state: "CA",71 zip: "94105",72 country: "US",73 },74 },7576 // Ref -- reference to another record77 {78 name: "primaryContactId",79 type: "Ref",80 value: "003xx0000001234AAA",81 },8283 // Variable -- generic variable84 {85 name: "sessionToken",86 type: "Variable",87 value: "sess_abc123def456",88 },8990 // With description (Android only, ignored on iOS)91 {92 name: "currentCaseId",93 type: "Text",94 value: "500xx0000009876AAA",95 description: "The active support case for this customer",96 },97 ],98});
The description field is only supported on Android. On iOS, it’s ignored.
Note
Hidden Prechat Fields
Hidden prechat fields let you pass values to the Service Agent prechat form without displaying them to the user. Common use cases include pre-populating a ContactId, AccountId, or session token.
:::warning Android Limitation
On Android, hidden prechat fields are stored but not sent to the native SDK during session initialization. This feature is fully functional on iOS only. If hidden prechat fields are critical to your use case, use iOS or wait for a future bridge update.
:::
Hidden prechat fields only apply to Service Agent conversations. They have no effect for Employee Agent conversations.
Note
Usage
Register hidden prechat fields before calling launchConversation():