For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
Sign inTry it free
DocsGuidesSDKsIntegrationsAPI docsTutorialsFlagship blog
DocsGuidesSDKsIntegrationsAPI docsTutorialsFlagship blog
  • Guides
    • Cheatsheets
    • Feature flags
    • AgentControl
    • Experimentation
    • Statistical methodology
    • Metrics
    • Infrastructure
    • Account management
    • Teams and custom roles
    • SDKs
      • Unit testing with Jest
      • Use cases for SDK wrappers
      • Using LaunchDarkly without a supported SDK
      • Using mobile SDKs
      • Using the Lua SDK with HAProxy
      • Using the Lua SDK with NGINX
      • Using the JavaScript SDK in Salesforce Lightning Web Components
      • Resilient architecture patterns for LaunchDarkly's SDKs
    • Integrations
    • REST API
    • Additional resources
Sign inTry it free
LogoLogo
On this page
  • Prerequisites
  • Download the LaunchDarkly JavaScript Client
  • Set up your Salesforce environment
  • Using the LaunchDarkly client in a Lightning Web Component
  • Conclusion
GuidesSDKs

Using the JavaScript SDK in Salesforce Lightning Web Components

Was this page helpful?
Previous

Resilient architecture patterns for LaunchDarkly SDKs

Next
Built with

This guide explains how to configure your Salesforce environment to use the LaunchDarkly JavaScript or React Web client-side SDKs in Lightning Web Components (LWCs).

Salesforce Lightning Web Components are an implementation of standard web components that allow you to leverage HTML, CSS, and JavaScript in a lightweight framework. To learn more, read Introducing Lightning Web Components.

The example code in this guide is extracted snippets. We show it here as if it is being used in a JavaScript class.

Prerequisites

To complete this guide, you must have the following prerequisites:

  • Basic working knowledge of the LaunchDarkly JavaScript client-side SDK or React Web client-side SDK
  • Basic working knowledge of Salesforce Lightning Web Components

Download the LaunchDarkly JavaScript Client

To begin, find the latest version under Releases in the JavaScript SDK GitHub repository. Then download the desired version from https://unpkg.com/launchdarkly-js-client-sdk@<VERSION>/dist/ldclient.min.js.

Set up your Salesforce environment

Next, set up your Salesforce environment. To do this:

  1. Upload the file to your Salesforce organization as a static resource in your org
  2. Enable Lightning Web Security in your org
  3. Add https://*.launchdarkly.com as a CSP Trusted Site, under Manage CSP Trusted Sites

Using the LaunchDarkly client in a Lightning Web Component

In the JavaScript file of your Lightning Web Component, import the static resource:

Import LaunchDarkly client
1import ldclient from '@salesforce/resourceUrl/ldclient';

Then, load the imported script for usage in the file, using the Lightning Web Component Platform Resource Loader:

Load script
1Promise.all([
2 loadScript(this, ldclient)
3])
4.then(() => {
5// Post Script Load Code Here
6})

Finally, initialize the LaunchDarkly client:

Initializing LaunchDarkly client
1const context = {
2 kind: 'user',
3 key: 'example-context-key'
4};
5this.LDClient = ldclient.initialize('example-client-side-id', context);
6
7try {
8 await this.LDClient.waitForInitialization(5)
9 // initialization succeeded,
10 // flag values are now available through the client
11} catch (err) {
12 // initialization failed or did not complete before timeout
13}

You can access the flag value using a JavaScript getter method:

Example flag getter method
1get exampleFlag() {
2 return await this.LDClient.variation['example-flag-key', false];
3}

To learn more, read LWC Data Binding. You can then use HTML to allow feature flags to control your user interface (UI) display.

Here is an example of an LWC HTML snippet:

LWC HTML snippet
1<template if:true={exampleFlag}>
2 Flag evaluation is TRUE
3</template>
4<template if:false={exampleFlag}>
5 Flag evaluation is FALSE
6</template>

Conclusion

In this guide, we discussed how to use the LaunchDarkly JavaScript and React Web client-side SDKs in Lightning Web Components. This allows you to leverage HTML, CSS, and JavaScript in a lightweight framework.