Use Lightning Web Components in Visualforce

Use Lightning Web Components in Visualforce

Use Lightning Web Components in Visualforce

In this post, we will see how we can use LWC in Visualforce Pages or how to display Lightning Web Component In Visualforce Page. Even though the use of lightning is growing day by day, still some customers are using salesforce classic because of many reasons. Sometimes there is a requirement to use lightning web component in visualforce page.

Lightning Web Components for Visualforce is based on Lightning Out, a powerful and flexible feature that lets you embed Lightning web components into almost any web page. When used with Visualforce, some of the details become simpler. For example, you don’t need to deal with authentication, and you don’t need to configure a Connected App. If you are looking for using lightning aura components in visualforce pages, please refer to Use Lightning Components in Visualforce Pages.

In this example, we will see a very basic example of the lightning web component and then display lightning web component in the visualforce page.

helloComponentForVFLWC.html

<template>
    <lightning-card title="Use LWC in Visualforce" icon-name="custom:custom19">
        <div class="slds-m-around_medium">
            Hello, This is LWC for visualforce page
        </div>
    </lightning-card>
</template>

helloComponentForVFLWC.js

import { LightningElement } from 'lwc';
export default class HelloComponentForVFLWC extends LightningElement {}

HelloLWCExampleApp.app

Here important point is that ltng:outApp needs to be extended in app.

ltng:outApp adds SLDS resources to the page to allow our Lightning components to be styled with the Salesforce Lightning Design System. If we don’t want SLDS resources added to the page, we need to extend from ltng:outAppUnstyled instead.

<aura:application extends="ltng:outApp" access="GLOBAL">
    <aura:dependency resource="helloComponentForVFLWC" />
</aura:application>

HelloLWCExampleVF.page

<apex:page showHeader="false" sidebar="false">
    <apex:includeLightning />    
    <div id="LightningComponentid" />    
    <script>
    $Lightning.use("c:HelloLWCExampleApp", function() {
        $Lightning.createComponent("c:helloComponentForVFLWC",
          { 
          },
          "LightningComponentid",
          function(cmp) {
             console.log('LWC Componenet added in VF page');
          });
    });
    </script>
</apex:page>

In the above Visualforce page, apex:includeLightning tag imports necessary dependencies and scripts to enable Visualforce to act as Lightning component container.

We can use $Lightning.use() method in JavaScript to refer to Lightning Application which extends ltng:outApp.

We can use $Lightning.createComponent create Lightning Component dynamically.

We can call $Lightning.use() multiple times on a page, but all calls must reference the same Lightning dependency app.

We will get the following output

Use Lightning Web Components in Visualforce

For more detail please refer to official link

Permanent link to this article: https://www.sfdcpoint.com/salesforce/use-lightning-web-components-in-visualforce/

17 comments

Skip to comment form

    • wej on May 19, 2020 at 8:28 pm
    • Reply

    Hi Ankush,
    I did the same thing as you, with LWC I used ( record-edit-form) + (lightning-input-field) because I need to get record id after the creation to use it in (lightning-file-upload) the form works very well in my organization, but with VF the form didn’t return the record id, and the upload file didn’t work too.

    1. As per Salesforce documentation file upload component does not work with lightning out. Here is statement
      “This component is not supported in Lightning Out or standalone apps, and displays as a disabled input.”
      You can find it in official link from my post
      https://www.sfdcpoint.com/salesforce/file-upload-in-lightning-web-component/

    • Tillu on June 1, 2020 at 2:07 pm
    • Reply

    HI Ankush Dureja,

    I’m new to lightning web Component. while using Lightning web Component In visual force page . I got an error msg like “An internal server error has occurred” can you please help me with that .

      • Akash on June 18, 2020 at 11:38 pm
      • Reply

      Yes I got the same error

      1. Can you please share code?

          • Tillu on July 26, 2020 at 2:45 pm
          • Reply

          I copied the same code that you have mentioned in this site

  1. Excellent Content .thank you

    • rayyan on June 17, 2020 at 4:23 pm
    • Reply

    Can we do vice versa of it like showing/embed vf page inside LWC component

    • Aman Anil Tiwari on July 9, 2020 at 5:34 pm
    • Reply

    Tysm for this informative content!!

    • Pauline on November 6, 2020 at 6:36 pm
    • Reply

    Hi Ankush. I used your code on a blank vf page and my component works fine. However, when I insert the same code on an existing VF page with other content the component is not rendered. Any idea why?

    I have tried inserting the div in different parts of my page but only the other vf content is being rendered.

    Please help.

    • Manish Das on February 20, 2021 at 2:41 am
    • Reply

    HI Ankush,

    I am using LWC Record Edit form inside a VF page . After clicking on the save button of LWC record page then I am using the Id of the new record created to navigate using Navigation Mix. But on the Save Handler event , Record ID is not returned . Please help me . I am pasting the Handle Success Function:

    handleSuccess(event) {
    if(this.redirect == true){
    console.log(‘handleSuccess’+this.redirect);
    let creditnoteId = event.detail.id;
    console.log(‘creditnoteId’+this.redcreditnoteIdirect); /// – Here I am not getting the Record ID
    this[NavigationMixin.Navigate]({
    type: ‘standard__recordPage’,
    attributes: {
    recordId:creditnoteId,
    objectApiName:’Opportunity’,
    actionName:’view’
    }
    })
    }
    }

    • Yugandhar on May 19, 2021 at 2:25 pm
    • Reply

    check before posting the code here. Same error i also got.

    1. This code is still working for me. Can you please share your code.

    • amit on August 6, 2021 at 12:38 am
    • Reply

    hi ankush,
    when i try this with my app and component file its running normally but not showing anything on console

    • Prasan on September 27, 2021 at 10:47 am
    • Reply

    Hi ankush,

    I’m getting the below error and I’m searching for a way to resolve it. Could you please help me?

    https://salesforce.stackexchange.com/questions/358469/error-occurs-when-clicking-show-all-results-in-lightning-input-field-look-up

    Thank You,
    Prasan De Alwis

    • Gwen on March 23, 2022 at 9:13 pm
    • Reply

    Hi!
    Excellent Article & Information.
    I am new to LWC & VisualForce pages.
    I have an already existing LWC (update vehicle count) that is called from the Service Appointment page layout as a dropdown Action.
    I want to call the same LWC from a Custom Action (Gantt Action category) on the FSL Gantt chart (when you right-click on a Service Appointment).
    I have your code snippets working perfectly and I am calling from my new Custom Action on the FSL Gantt chart. Thank You.
    Now, I have to tweak the code snippets to call the exiting LWC.
    I think I need help with passing a RecordID parameter to the LWC.
    Any help is appreciated.
    Thanks!

    • Gwen on March 23, 2022 at 11:07 pm
    • Reply

    Hi Ankush! I’m not sure this question went through successfully…
    Excellent Article & Information.
    I am new to LWC & VisualForce pages.
    I have an existing LWC (update vehicle count) that is called from the Service Appointment page layout as a dropdown Action.
    I want to call the existing LWC (update vehicle count) from a Custom Action (Gantt Action category) on the FSL Gantt chart (when you right-click on a Service Appointment).
    ~ ~ ~
    I have your code snippets working perfectly and it is called from my new Custom Action on the FSL Gantt chart.
    Now, I have to tweak your code snippets to switch to call the exiting LWC (update vehicle count).
    I think I need help with passing a RecordID parameter to the LWC.
    Any help is appreciated.
    Thanks!

Leave a Reply

Your email address will not be published.