Code Monkey home page Code Monkey logo

lwcaction's Introduction

⚡ IMPORTANT ⚡

Due to Summer '21 release, LWC is now natively available as Quick Action.


Deploy to Salesforce

LWCAction

To use LWC as Lightning Action we need to wrap it with Aura Component. Instead of creating new unnecessary Aura Components, coding close Lightning Action event handlers and adding spinners, we can create generic Aura Component which will dynamically create LWC depending on Lighting Action API Name.

Sample code

https://github.com/andrii-solokh/LWCAction-samplecode

How to use

  1. In LWC component's '.js-meta.xml' file update 'isExposed' field:
<isExposed>true</isExposed>
  1. Lightning Action should invoke Aura Component 'LWCLightningAction'.
  2. Lightning Action API Name should be the same as LWC API Name. I can advise naming LWC as 'SObjectName + ActionName + Action', example: 'quoteApplyDiscountAction'.
  3. LWC should extend 'LwcAction':
import LwcAction from 'c/lwcAction'
export default class QuoteApplyDiscountAction extends LwcAction {}
  1. Stop spinner when LWC is ready:
this.ready()

Features

  • Closing action from LWC:
this.closeAction()

From template '{closeAction}' accordingly:

<lightning-button label="Close" onclick={closeAction}></lightning-button>
  • Show spinner:
this.showSpinner()
  • Hide spinner:
this.hideSpinner()
  • Refresh view:
this.refreshView()
  • Fire any Aura Event, 'this.fireAuraEvent(eventName, params):
this.fireAuraEvent('e.force:createRecord', { entityApiName: "Contact" })
  • Id and sObject name of Record from which Lighting Action was invoked is passed to LWC and can be accessed with:
this.recordId
this.sObjectName

Actions not in LwcAction context

  • To close action, show or hide spinner from component other than LwcAction:
  1. Import 'LwcAction':
import LwcAction from'c/lwcAction'
  1. Invoke required method:
LwcAction.fireCloseAction(this) // close Lighting Action
LwcAction.fireShowSpinner(this) // show spinner
LwcAction.fireHideSpinner(this) // hide spinner
LwcAction.fireRefreshView(this) // refresh view
LwcAction.fireAuraEvent(this, 'e.force:createRecord', { entityApiName: "Contact" }) // call any Aura event with params

Additional Features

  • You can keep component hidden until it's ready to be presented by wrapping it with:
<template if:true={isReady}></template>

Example:

<template>
  <template if:true={isReady}>
    <div>Your component</div>
  </template>
<template>

Present LWC and hide spinner call:

this.ready()

lwcaction's People

Contributors

andrii-solokh avatar andrii-solokh-wedo avatar rvmzes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

lwcaction's Issues

TypeError: Cannot read property 'actionName' of undefined (via Account->Activity Panel -> Event drop down arrow button

The action button not working via Activity Panel -> Event -> drop down arrow button:

image

I can confirm that action button does however work via the header button on the record of the object the action is created on.

error message:

LWCLightningAction.js:44 Uncaught (in promise) TypeError: Cannot read property 'actionName' of undefined
at eval (LWCLightningAction.js:44)
at s (aura_prod.js:37)

Error Line:

const actionName = response.actions.shift().actionName;

let me know if you have a fix and I'll do the same if identified.

Thanks,
Peter

License?

Could you please add a license to the code so that others could use it in their projects, if appropriate.

Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.