• Home
  • Integrations
  • SDKs
  • Guides
  • API docs
No results for ""
EXPAND ALL

EDIT ON GITHUB

Apex SDK reference

Read time: 1 minute
Last edited: Sep 24, 2021

Overview

This reference guide documents the features available in the Apex SDK, and explains how these features work. LaunchDarkly's SDKs are open source. The source is available in GitHub or the API documentation. You can also try this SDK out by cloning and running a sample application that uses this SDK.

Getting started

After you complete the Getting Started process, follow these instructions to start using the LaunchDarkly SDK in your Salesforce Apex application.

The first step is deploying the SDK to Salesforce:

1git clone https://github.com/launchdarkly/apex-server-sdk.git
2cd apex-server-sdk
3sfdx force:source:deploy --targetusername='YOUR TARGET ORG' --sourcepath='force-app'

Initializing the client

Initialize the client as follows:

1LDClient client = new LDClient();

Using client, you can check which variation a particular user should receive for a given feature flag:

1LDUser user = new LDUser.Builder('abc').build();
2
3if (client.boolVariation(user, 'your.flag.key', false)) {
4 System.debug('feature is on');
5} else {
6 System.debug('feature is off');
7}

After the SDK is deployed to Salesforce, start the bridge, and you can begin evaluating flags. The bridge must be running in order to receive flag updates and publish events to LaunchDarkly.

Using the LaunchDarkly Salesforce bridge

The Apex server-side SDK is architected differently than our other SDKs. In most of our SDKs, the SDK downloads feature configurations and sends events by itself. The Apex SDK is different because it uses an external bridging application to connect LaunchDarkly and Salesforce.

Because the SDK uses a bridge to handle state management, there is no initialization delay required to download flags. Additionally, the lack of state inside the SDK means that initializing multiple instances of the SDK is not problematic.

The Apex SDK exposes two HTTP endpoints that the bridge uses: /store and /event. The bridge pushes flag updates to Salesforce through one endpoint, and pulls events from Salesforce with the other.

The bridge is a Go application configured with environment variables. The bridge needs authorization for both LaunchDarkly and Salesforce.

To build the bridge from source install Go 1.14 or higher. To install Go, read Go's documentation. Build and run the bridge like this:

1cd bridge && go build .
2
3export LD_SDK_KEY='Your LaunchDarkly SDK key'
4export SALESFORCE_URL='Your Salesforce Apex REST URL'
5export OAUTH_ID='Your Salesforce OAuth Id'
6export OAUTH_SECRET='Your Salesforce OAuth secret'
7export OAUTH_USERNAME='Your Salesforce username'
8export OAUTH_PASSWORD='Your Salesforce password + security token'
9
10./bridge

The logs indicate if the bridge is running. If it fails, the logs show the errors that occurred. You must deploy the SDK to Salesforce before you run the bridge.

Supported features

This SDK supports the following features: