Dev Tips

Learn How to Use Salesforce Lightning Out

Master Salesforce Lightning Out with our detailed guide and video tutorial. Learn how to create and host apps mirroring Salesforce aesthetic externally.

Last Update:
May 1, 2023
Published:
July 16, 2021

In this article, we’ll delve into the world of Salesforce's Lightning Out feature. We'll learn how to leverage this feature to build apps that mirror the Salesforce aesthetic, all while being hosted outside of Salesforce. You can expect a step-by-step guide, supplemented by a detailed video tutorial, aimed at helping you create a public opt-out page hosted outside of Salesforce.

Here are our 5 key takeaways:

  • Lightning Out can be used to create applications that have the same look and feel as Salesforce, but are hosted outside of Salesforce.
  • You can use Aura or Visualforce components, but this tutorial focuses on using Lightning Web Components.
  • The tutorial guides you through creating a public opt-out page hosted on GitHub Pages that updates or creates records in Salesforce.
  • You need to ensure that you have the correct attributes for your Aura Dependency App/Wrapper and add your newly created Lightning Web Component as an Aura dependency resource.
  • It's important to add the hosted page to the CORS settings in Salesforce and use debug logs on the guest user for testing and debugging the application.

Table of Contents

It’s no secret that Lightning Out has been out for a while and has been used to build apps that have the same look and feel as Salesforce by using components directly (instead of styling them only) while also hosting them outside of Salesforce. While you can use Aura or Visualforce components, I haven’t seen a lot of tutorials utilizing Lightning Web Components, so I decided to make one. 


In this video, we’re doing a public opt-out page hosted outside of Salesforce (GitHub Pages) to update or create records in Salesforce. The video is in more detail, but here are the steps:

  1. Enable Digital Experiences in your org (Communities)
  2. Create your Lightning Web Component app/page and host it inside Salesforce and test it first (tab, button etc.)
  3. Create a Blank Community Site (not a page)
  4. Create your Aura Dependency App/Wrapper with correct attributes
  5. Make sure you add your newly created Lightning Web Component as an aura dependency resource
  6. Create the HTML file you will host outside of Salesforce, use the correct parameters for JS functions, use the community URL for the lightning out JS library.
  7. Create a GitHub Page or anywhere you can host the html file
  8. Add the created page to the CORS settings in Salesforce
  9. Use debug logs on the guest user for our use case so you can test and debug the application
  10. Deploy the Salesforce dependencies using Blue Canvas!

And thank you Atlas for this video walkthrough! (Atlas is a Salesforce Developer at EMPAUA and a moderator at SFXD . He shares what he has learned over his years of own experience in the Salesforce community. You can read more articles of him on SalesforceBen , and follow him Twitter. )

The Basics of Salesforce Lightning Out

Salesforce Lightning Out is a unique framework that empowers developers to utilize Lightning Components outside of the Salesforce platform. These components can be embedded in any web app, regardless of its tech stack, greatly expanding their versatility. Lightning Out essentially acts as a bridge, enabling communication between Salesforce and external platforms.

The benefits and potential uses of Lightning Out are plentiful. One of the primary advantages is reusability. Developers can create a component once and utilize it across multiple platforms, increasing efficiency. Lightning Out also provides a consistent UI across applications, improving user experience. 

Prerequisites for Using Salesforce Lightning Out

To begin working with Lightning Out, you need to have a working knowledge of JavaScript and Apex, as well as a basic understanding of Salesforce Lightning Components. 

Regarding permissions, you need to have 'Modify All Data' permission in your Salesforce account to create and configure Lightning Out.

Step-by-step Guide to Using Salesforce Lightning Out

Let's dive into how to use Salesforce Lightning Out.

Create a Lightning Component for use with Lightning Out

First, you must create a Lightning Component. From your Salesforce Developer Console, navigate to 'File' > 'New' > 'Lightning Component', provide a name, and click 'Submit'. Write your component's code in the provided editor.

Setting up a Lightning Out Dependency App

Next, create a Lightning Out Dependency App, which is a Lightning Application that declares dependencies on Lightning Components. From the Developer Console, navigate to 'File' > 'New' > 'Lightning Application', name the application, and click 'Submit'. 

In the application's code, include 'ltng:outApp' as an interface and 'ltng:require' to list the dependencies, which would be your Lightning Component.

Deploying your Lightning Component on an external website

To deploy your Lightning Component, add a few lines of JavaScript to your external website's code. This JavaScript will initialize the Salesforce Lightning Component framework and render your Lightning Component.

Handling data exchange with Salesforce

For data exchange between your Lightning Component and Salesforce, you can use Apex. Create an Apex Controller with methods for fetching or updating data. In your Lightning Component, add 'controller' attribute referring to your Apex Controller.

Troubleshooting Common Issues with Salesforce Lightning Out

While working with Lightning Out, you might encounter issues like a blank component rendering on your web page or data not being fetched or updated. A common cause for such problems is incorrect configurations or errors in the JavaScript code. Ensure you've followed the instructions correctly and thoroughly check your code.

To troubleshoot, utilize the browser's developer tools, particularly the console for JavaScript errors and the network tab for request/response issues. 

Advanced Techniques and Best Practices

Performance optimization of Lightning Out involves efficient data handling. Minimize data fetches, use pagination for large data sets, and leverage client-side caching.

Ensure your external website uses HTTPS to maintain secure communication with Salesforce. Also, don't expose sensitive data in your Lightning Components.

When coding, use a modular design approach, making your components reusable and manageable. Follow Salesforce's best practices for deploying Lightning Out, like testing on all target platforms before deploying.

Use Case Examples of Salesforce Lightning Out

A great real-life example of Lightning Out usage is the integration of a Salesforce CRM with an external eCommerce website. The 'Add to Cart' Lightning Component can be embedded into the eCommerce site, providing real-time inventory updates.

In this case, Lightning Out enabled seamless integration between the two platforms, enhancing the user experience and streamlining the data flow.

Harness the Power of Salesforce Lightning Out with Blue Canvas

In this digital era, the ability to integrate platforms and reuse code is invaluable. Salesforce Lightning Out provides this capability, opening new possibilities for developers and businesses alike.

As we've explored, the range of benefits that Salesforce Lightning Out provides is extensive. From code reusability, enhanced integration capabilities, to improving user experiences, it's a tool that empowers developers and businesses to step up their digital strategy game.

As you become more and more familiar with the extent of Lightning Out capabilities, we also recommend you look into tooling that can help you deploy and synch Salesforce metadata across your various Salesforce organizations. At Blue Canvas we offer Salesforce Deployment Tools that helps you compare and synch metadata across sandboxes in minutes! For further assistance with implementation or to learn more about these features, don't hesitate to reach out to our team at Blue Canvas today.

--------

FAQs

What is Salesforce Lightning Out?

  • Salesforce Lightning Out is a unique framework that allows developers to use Lightning Components outside of the Salesforce platform. These components can be embedded in any web application, regardless of its tech stack, significantly enhancing their versatility. Lightning Out essentially functions as a bridge, enabling communication between Salesforce and external platforms.

What are the benefits of using Salesforce Lightning Out?

  • One of the primary benefits of Lightning Out is its reusability. Developers can create a component once and use it across multiple platforms, which enhances efficiency. Lightning Out also provides a consistent user interface across applications, thereby improving the user experience. Moreover, it enables seamless integration between Salesforce and other platforms, streamlining data flow and operations.

What prerequisites do I need to use Salesforce Lightning Out?

  • To start working with Lightning Out, you need to have a working knowledge of JavaScript and Apex, as well as a basic understanding of Salesforce Lightning Components. Regarding permissions, you need to have 'Modify All Data' permission in your Salesforce account to create and configure Lightning Out.

How do I create a Lightning Component for use with Lightning Out?

  • You can create a Lightning Component from your Salesforce Developer Console. Navigate to 'File' > 'New' > 'Lightning Component', provide a name, and click 'Submit'. You can then write your component's code in the provided editor.

How can I troubleshoot common issues with Salesforce Lightning Out?

  • Issues like a blank component rendering on your webpage or data not being fetched or updated could occur. These problems are usually due to incorrect configurations or errors in the JavaScript code. You can troubleshoot these issues using the browser's developer tools, particularly the console for JavaScript errors and the network tab for request/response issues.

How can I optimize the performance of Lightning Out?

  • Performance optimization of Lightning Out involves efficient data handling. Minimize data fetches, use pagination for large data sets, and leverage client-side caching. Also, make sure your external website uses HTTPS to maintain secure communication with Salesforce.

Can you provide an example of Salesforce Lightning Out usage?

  • A real-life example of Lightning Out usage is integrating a Salesforce CRM with an external eCommerce website. The 'Add to Cart' Lightning Component can be embedded into the eCommerce site, providing real-time inventory updates. This application of Lightning Out enables seamless integration between the two platforms, enhancing user experience and streamlining data flow.

How can Blue Canvas help with Salesforce Lightning Out?

  • Blue Canvas offers a robust suite of tools and a team of experienced professionals to help you implement and optimize Salesforce Lightning Out. They can guide you through the entire process, from initial setup and deployment to troubleshooting and optimization, ensuring that you get the most out of Lightning Out to suit your specific needs.

More like this