Integrate Agentforce SDK

To use the SDK, be sure to import the Agentforce SDK packages.

import com.salesforce.android.agentforceservice.*

Implement Core Service Interfaces

The Agentforce Mobile SDK is designed for flexibility and delegates several core responsibilities to the host application. You must create concrete implementations for the following interfaces:

  • AgentforceAuthCredentialProvider: Supplies the SDK with the authentication token (e.g., OAuth 2.0 access token) required to communicate securely with Salesforce APIs.
  • AgentforceInstrumentation: A handler for the SDK to emit instrumentation and telemetry data into your app's analytics system.

For authentication, create a credential provider using one of the supported authentication methods.

class MyAppCredentialProvider : AgentforceAuthCredentialProvider {
   override fun getAuthCredentials(): AgentforceAuthCredentials {
       return AgentforceAuthCredentials.OAuth(
           authToken = "YOUR_AUTH_TOKEN",
           orgId = "YOUR_ORG_ID",
           userId = "YOUR_USER_ID"
       )
   }
}
class MyAppCredentialProvider : AgentforceAuthCredentialProvider {
   override fun getAuthCredentials(): AgentforceAuthCredentials {
       return AgentforceAuthCredentials.OrgJWT(orgJWT = "YOUR_ORG_JWT")
   }
}
class MyAppCredentialProvider : AgentforceAuthCredentialProvider {
   override fun getAuthCredentials(): AgentforceAuthCredentials {
       return AgentforceAuthCredentials.Guest(url = "YOUR_GUEST_URL")
   }
}

Related links:

Configure Agent

In order to use the Agentforce SDK you must first identify the agent type you are targeting: Employee Agent or Service Agent. A single app may target each, but requires configuration and instantiation of separate Agentforce clients. To learn more, see Create an Agent from a Template in Salesforce Help.

tip: For guidance on gathering information for the agent configuration object, see Agent Type Setup in Before You Begin.

The following snippets illustrate how to configure your agent for each agent type.

// Configure an Employee agent
val configuration = AgentforceConfiguration.builder(
    authCredentialProvider = myAuthCredentialProvider
)
    .setApplication(application)
    .setSalesforceDomain("https://your-domain.my.salesforce.com")
    .setAgentId("YOUR_AGENT_ID")
    .setThemeManager(createCustomAgentforceThemeManager(themeMode = AgentforceThemeMode.SYSTEM))
    .setNetwork(myNetworkProvider)
    .setLogger(myLogger)
    .setNavigation(myNavigation)
    .build()

// Set mode for Employee agent
val agentforceMode = AgentforceMode.FullConfig(configuration)

// Create client
agentforceClient?.init(
    authCredentialProvider = configuration.authCredentialProvider,
    agentforceMode = agentforceMode,
    application = application
)
// Configure a Service agent
val agentforceMode = AgentforceMode.ServiceAgent(
    serviceAgentConfiguration = ServiceAgentConfiguration.builder(
        serviceApiURL = "https://your-service-api-url.com",
        organizationId = "YOUR_ORG_ID",
        esDeveloperName = "YOUR_CHAT_DEVELOPER_NAME"
    ).build()
)

// Create client
agentforceClient?.init(
    authCredentialProvider = configuration.authCredentialProvider,
    agentforceMode = agentforceMode,
    application = this.application
)
val config = AgentforceConfiguration.builder(
   authCredentialProvider =
       object : AgentforceAuthCredentialProvider {
           override fun getAuthCredentials(): AgentforceAuthCredentials {
               return AgentforceAuthCredentials.OAuth(
                   authToken = "YOUR_AUTH_TOKEN",
                   orgId = "YOUR_ORG_ID",
                   userId = "YOUR_USER_ID"
               )
           }
       }
).setApplication(application)
   .setSalesforceDomain("https://your-domain.my.salesforce.com")
   .setNetwork(myNetworkProvider)
   .setLogger(myLogger)
   .setNavigation(myNavigation)
   .setDataProvider(myDataProvider)
   .setFeatureFlagSettings(
       AgentforceFeatureFlagSettings.builder().build()
   )
   .setCameraUriProvider(myCameraUriProvider)
   .setVoiceManaging(myAgentforceVoiceManaging)
   .setInstrumentationHandler(myAgentforceInstrumentationHandler)
   .setUser(myUser)
   .setPermission(myPermission)
   .setThemeManager(myAgentforceThemeManager)
   .setViewProvider(myAgentforceViewProvider)
   .build()

val agentforceMode = AgentforceMode.FullConfig(config)
agentforceClient?.init(authCredentialProvider = config.authCredentialProvider, agentforceMode = agentforceMode, application = application)

Create and retain an instance of AgentforceClient. It's best to hold this in a lifecycle-aware component, like a ViewModel, to ensure the conversation state persists across configuration changes.

Start a Conversation

Once you've initialized the AgentforceClient, you can start a conversation with an agent using the startAgentforceConversation method.

// Start the conversation
agentforceClient.startAgentforceConversation()

// Get the conversation session
val session = agentforceClient.fetchAgentforceSession(YOUR_AGENT_ID)

This method returns an AgentforceConversation object, which represents a single conversation with an agent.

Next Steps