Visualforce, Aura and LWC

What custom Salesforce user interfaces are built with today: Visualforce, Aura components and Lightning Web Components, how they coexist, which to choose.

You need a custom screen in Salesforce: do you build it with Visualforce, with Aura components or with Lightning Web Components (LWC)? All three can coexist in the same org. This page explains what each one is, how they coexist and which to choose, with a private-banking example.

What each one is

A UI framework is a set of building blocks and rules for building screens.

Visualforce

Visualforce is a framework for building custom user interfaces hosted on the Salesforce Platform. It has a tag-based markup language, similar to HTML (HyperText Markup Language). A Visualforce page joins two parts: the markup, inside a single <apex:page> tag, and a controller. The controller decides what happens when the user acts on the page and gives access to the data.

There are four kinds of controller:

  • Standard controller. It repeats the logic of the object’s standard page. With the Account controller, the Save button does the same as on an account’s edit page.
  • Standard list controller. It works with a set of records, such as a list or a related list.
  • Custom controller. A class in Apex, Salesforce’s programming language, that implements all of the page’s logic.
  • Controller extension. An Apex class that adds to or overrides the behavior of another controller.

The difference that matters most is security. The standard controller runs in user mode: it enforces the user’s permissions, field-level security and sharing rules. Custom controllers and extensions run in system mode: they ignore the user’s permissions and field-level security. With the with sharing keyword on the class, they respect the user’s organization-wide defaults, role hierarchy and sharing rules.

A Visualforce page can override a standard button, define a tab, sit inside a page layout or add actions in the Salesforce mobile app.

Aura components

Since Spring ’19 (API version 45.0, the application programming interface), Salesforce offers two programming models for building Lightning components: the original model, Aura Components, and Lightning Web Components. “Lightning components” is the umbrella term for both.

An Aura component is a self-contained, reusable unit of an app. It can be as small as a single line of text or as large as an entire app.

Lightning Web Components

Lightning Web Components is a framework for building custom user interfaces, web and mobile apps and digital experiences on the Salesforce Platform. Each LWC component is a custom HTML element, written with HTML and JavaScript. LWC is built on Web Components, the browser standards for creating your own elements. It is built on code that runs natively in the browser. Most of the code you write is standard JavaScript and HTML. The framework is open source.

Salesforce provides base components, ready-made parts built on Lightning Design System, its design system. Lightning Experience, the Salesforce user interface, is built with them. Base components exist as LWC components and as Aura components.

LWC and Aura are available in Lightning Experience and in Enterprise, Performance, Unlimited and Developer Editions.

How they coexist

LWC components and Aura components can coexist and interoperate on the same page. To an admin or an end user, both are Lightning components, and they cannot tell which model each one was built with.

Coexistence runs one way. An Aura component can contain LWC components; an LWC component cannot contain Aura components. The DOM (Document Object Model, the page’s tree of elements) of an LWC component must be made only of LWC components.

That rule gives the way out when LWC does not support something. You write the component in LWC and wrap it in an Aura component that only gives access to the missing feature. Five tools do not support LWC directly: Chatter Extensions, Global Actions, List View Actions, Related List View Actions and Standard Action Overrides.

Visualforce coexists with the other two as well. An existing Visualforce page can be reused inside Lightning Experience. In the other direction, an LWC component can go inside a Visualforce page with Lightning Out, still in beta. Two limits are worth knowing: Visualforce pages do not adapt to mobile on their own, and they are not supported in Lightning Experience on iPad Safari.

Lightning Data Service

Lightning Data Service (LDS) is the layer an LWC or Aura component uses to read and write records without Apex code. It enforces sharing rules and field-level security by itself. Salesforce compares it, as a simplification, to the Visualforce standard controller.

LDS caches records and shares them across components. If several components on a page show the same record, the record is loaded once and all of them show the same version. When one component changes the record, the others receive the new value. Data that arrives through Apex does not get this treatment: you must refresh it yourself.

In LWC, LDS sits behind the base components lightning-record-form, lightning-record-edit-form and lightning-record-view-form, and behind the lightning/ui*Api modules. In Aura, it sits behind force:recordData and lightning:recordForm and its variants.

LDS is built on User Interface API, the public API Salesforce uses to build Lightning Experience and its apps for iOS and Android. Its responses respect CRUD (create, read, update and delete) permissions, field-level security and sharing. LDS does not use calls from the org’s API allocation, although it is subject to general limits. It supports all custom objects and the standard objects User Interface API supports; it does not support custom metadata types.

The criterion, in four steps

For new development, Salesforce recommends Lightning Experience low-code tools and LWC components over Visualforce. Between LWC and Aura, LWC unless a feature is missing. Existing code is decided case by case. The four steps chain those rules. At each step, a yes follows the arrow to the decision; a no drops to the next step.

Which UI framework to use in SalesforceFour steps in order. At each step, a yes follows the arrow to the decision; a no drops to the next step. Step 1: if it is a Visualforce page that already works and needs no better experience, accessibility, multiple devices or more performance, reuse it in Lightning Experience. Step 2: if it is an existing Aura component, migrate one as a trial and then decide. Step 3: if the component goes in a tool LWC does not support, write it in LWC and wrap it in an Aura component. Step 4: if it needs a base component that exists only in Aura, write it in Aura. If the answer is no at all four steps, Lightning Web Components.Step 4Step 3Step 2Step 1NoNoNoNoVisualforcethat works andneeds noimprovement?Reuse itin LightningExperienceExistingAuracomponent?Migrate oneas a trial,then decideTool thatLWC does notsupport?LWCwrappedin AuraBasecomponent onlyin Aura?AuraLightning WebComponents

Step 1 calls for judgement. Salesforce advises rebuilding a Visualforce page in LWC when you want one of four things: a more engaging and responsive user experience, support for the WCAG (Web Content Accessibility Guidelines) accessibility guidelines, an experience for multiple device form factors, or better performance. If you want none of them, the page can stay as it is.

Step 2 has no single answer. After migrating one component, Salesforce sets out three paths: a larger migration, LWC for new components only, or staying with Aura for now.

Example: the screens of a private bank

A private bank reviews five screens in its org:

  1. Account creation. A Visualforce page overrides the New button on accounts. It works, it is used only on desktop and nobody is asking for changes: it is reused in Lightning Experience.
  2. Visit planning. Another Visualforce page is used by relationship managers on iPad with Safari, where Lightning Experience does not support it: it is rebuilt in LWC.
  3. Wealth summary. A new component on the client record shows several account fields: LWC with lightning-record-view-form. LDS reads the record without Apex and without using API calls.
  4. Bulk review. A list view action sends several clients to compliance review. List View Actions does not support LWC: it is written in LWC and wrapped in an Aura component.
  5. Onboarding wizard. A wizard in Aura has six nested components. A leaf is migrated first, a component that contains no others. The parent Aura component can contain the new LWC component, and the page keeps working.

When to use each one

The criterion that separates them is whether the code already exists and whether LWC covers what is needed.

Framework Use it when Do not use it when
Lightning Web Components You are building a new component. The target tool does not support it: wrap it in Aura.
Aura components You need to wrap an LWC component, or a base component that exists only in Aura. The component is new and LWC covers everything it needs.
Visualforce An existing page works and needs none of the four improvements. It is new development, or the page has to run on iPad Safari.

Before production

  1. Run every screen through the diagram’s four steps and record the decision.
  2. Declare with sharing on Visualforce custom controllers and extensions, or justify in writing why not.
  3. Read and write records with LDS before Apex; if you use Apex, refresh the data after every change.
  4. Declare in each LWC component’s configuration file the targets where it will be used, and wrap in Aura the ones that go in the five unsupported tools.
  5. Test every reused Visualforce page in the Salesforce mobile app and on the users’ devices.
  6. Before migrating Aura, inventory the components, their dependencies and their test coverage, and migrate the leaves first.
  7. After each migration, run full regression testing.

Official sources

Back to the knowledge base