Headless Quick Actions in Lightning Web Components

The Headless Quick Actions are a significant addition to the toolbox of Lightning Web Component developers. They allow you to execute custom code within a Lightning web component without opening a modal window, distinguishing them from traditional screen actions. This feature offers greater flexibility and control, enabling you to create more streamlined and efficient user experiences.

To enable your Lightning web component to function as a Headless Quick Action, you need to configure a target. This configuration file defines a headless action.

<?xml version="1.0" encoding="UTF-8" ?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
  <apiVersion>52.0</apiVersion>
  <isExposed>true</isExposed>
  <targets>
    <target>lightning__RecordAction</target>
  </targets>
  <targetConfigs>
    <targetConfig targets="lightning__RecordAction">
      <actionType>Action</actionType>
    </targetConfig>
  </targetConfigs>
</LightningComponentBundle>

One notable difference between LWC quick actions and other Lightning web components on record pages is the absence of the recordId in the connectedCallback(). If your code requires access to the recordId, you’ll need to set its value manually in your code.

_recordId;
set recordId(recordId) {
    if (recordId !== this._recordId) {
        this._recordId = recordId;
    }
}

Implementing the invoke() Method

In your Lightning web component, it’s essential to expose the invoke() method as a public method for Headless Quick Actions. The invoke() method is executed each time the quick action is triggered, making it a crucial part of your component’s functionality.

Here’s a simple example of an LWC component with the <strong>method:</strong>

import { LightningElement, api } from "lwc";

export default class HeadlessSimple extends LightningElement {
  @api invoke() {
    console.log("Hi, I'm an action.");
  }
}

Creating an Empty Template

In order to prevent the quick action from being executed multiple times in parallel during long-running actions, you should include an internal boolean flag. Although the return type of invoke() is void, it can be executed asynchronously by utilizing a Promise.

Here’s an example of using a boolean flag to ensure single execution and a Promise to manage asynchronous execution:

import { LightningElement, api } from "lwc";

export default class HeadlessAsync extends LightningElement {
  isExecuting = false;

  @api async invoke() {
    if (this.isExecuting) {
      return;
    }

    this.isExecuting = true;
    await this.sleep(2000); // Simulate a 2-second action
    this.isExecuting = false;
  }

  sleep(ms) {
    return new Promise((resolve) => setTimeout(resolve, ms));
  }
}

Navigation with Lightning Experience

To navigate to another page, record, or list within the Lightning Experience, you can utilize the navigation service. The following example demonstrates how to navigate to the contact home page:

import { LightningElement, api } from "lwc";
import { NavigationMixin } from "lightning/navigation";

export default class NavigateToRecordAction extends NavigationMixin(LightningElement) {
  @api invoke() {
    this[NavigationMixin.Navigate]({
      type: "standard__objectPage",
      attributes: {
        objectApiName: "Contact",
        actionName: "home",
      },
    });
  }
}

Dispatching Custom Events

Headless Quick Actions provide the ability to dispatch custom events, enhancing the interaction and feedback within your components. In the following example, we dispatch two toasts sequentially using the event provided by the lightning/platformShowToastEvent module:

import { LightningElement, api } from "lwc";
import { ShowToastEvent } from "lightning/platformShowToastEvent";

export default class DispatchEventAction extends LightningElement {
  @api async invoke() {
    let event = new ShowToastEvent({
      title: "I am a headless action!",
      message: "Hi there! Starting...",
    });
    this.dispatchEvent(event);

    await this.sleep(2000); // Simulate a 2-second delay

    event = new ShowToastEvent({
      title: "I am a headless action!",
      message: "All done!",
    });
    this.dispatchEvent(event);
  }

  sleep(ms) {
    return new Promise((resolve) => setTimeout(resolve, ms));
  }
}

Other user cases of headless quick action Below are a few additional scenarios along with examples and code snippets to illustrate how you can leverage this feature:

Data Validation and Error Handling

You can use headless quick actions to perform data validation and handle errors when users interact with records. For example, you might want to ensure that certain fields meet specific criteria before allowing a record update.

import { LightningElement, api } from 'lwc';

export default class DataValidationAction extends LightningElement {
    _recordId;
    set recordId(recordId) {
      if (recordId !== this._recordId) {
        this._recordId = recordId;
      }
    }
    @api
    async invoke(recordId) {
        // Retrieve the record data
        const record = await this.retrieveRecordData(recordId);

        // Validate data
        if (record && record.Field__c !== 'Valid Value') {
            this.displayValidationErrorToast();
        } else {
            // Perform the action if data is valid
            await this.performRecordUpdate(recordId);
        }
    }

    // Implement methods for data retrieval, update, and displaying toasts
}

Integration with External Services

Headless Quick Actions can be used to integrate with external services or APIs. This is especially useful when you need to synchronize data between Salesforce and external systems.

Dynamic Record Creation

You can use Headless Quick Actions to create new records dynamically based on certain conditions. For example, you might want to create a related record when a specific field is updated.

Custom Record Updates

You can implement custom logic for record updates based on specific criteria. For instance, you might want to update related records when a particular field is modified.

Conclusion

Headless Quick Actions in Lightning Web Components offer a powerful toolset for enhancing user experiences and executing custom code with precision. By configuring your component as a Headless Quick Action and implementing the invoke() method, you can streamline and optimize your development process. Additionally, you can seamlessly navigate to different pages and dispatch custom events, adding interactivity to your components. So, leverage these capabilities to their full potential and take your Lightning Web Component development to the next level.

About the blog

SFDCLessons is a blog where you can find various Salesforce tutorials and tips that we have written to help beginners and experienced developers alike. we also share my experience and knowledge on Salesforce best practices, troubleshooting, and optimization. Don’t forget to follow us on:

Newsletter

Subscribe to our email newsletter to be notified when a new post is published.

  • AlertFlow: Build a Configurable Notification Banner System for Salesforce
    The article discusses implementing a notification banner system in Salesforce to alert users about critical account information, such as sanctions or credit holds. It highlights four key problems this solution addresses, eliminates the need for coding to create alerts, and provides a step-by-step installation guide for the AlertFlow package.
  • A Practical Guide to Salesforce-to-QuickBooks Sync Problems
    Many organizations still use disconnected systems despite the growth in business software, leading to inefficiencies in data sharing. A well-executed integration between Salesforce and QuickBooks can resolve these issues. This guide outlines common integration challenges, potential solutions, and tips for achieving efficient synchronization between the two platforms.
  • From Zero to 100: How AgentExchange Became Salesforce’s Fastest-Growing App Publisher
    AgentExchange has published 100 apps in six months, achieving significant growth with 122 unique apps and 102 developers. The platform’s rapid expansion demonstrates the increasing demand for Salesforce-native solutions, particularly in sales and analytics. By offering reusable AI building blocks, AgentExchange enhances productivity and innovation within the Salesforce ecosystem.
  • Shopify + Salesforce: How to Build a Seamless Ecommerce Engine
    Integrating Shopify with Salesforce streamlines e-commerce operations by eliminating data silos. The integration synchronizes orders and customer information for accurate reporting and smarter marketing. This synergy improves operational efficiency, enhances customer support, and ensures reliable growth. Proper planning and phased implementation are vital for success and maintaining data integrity as businesses scale.
  • Linking Google Sheets to Salesforce: Full Guide
    Integrating Google Sheets with Salesforce using G-Connector simplifies data management by enabling two-way data synchronization. This allows users to automate reporting, reduce manual data entry errors, and improve collaboration. Teams can easily update records, share live data, and leverage Google Sheets’ tools for insightful analysis, enhancing productivity and efficiency.
Arun Kumar
Arun Kumar

Arun Kumar is a Salesforce Certified Platform Developer I with over 7+ years of experience working on the Salesforce platform. He specializes in developing custom applications, integrations, and reports to help customers streamline their business processes. Arun is passionate about helping businesses leverage the power of Salesforce to achieve their goals.

Articles: 162

Leave a Reply

Discover more from SFDC Lessons

Subscribe now to keep reading and get access to the full archive.

Continue reading

Discover more from SFDC Lessons

Subscribe now to keep reading and get access to the full archive.

Continue reading