Skip to main content

Log a Call custom quick action using Lightning Web Components

Quick actions are not supported yet by LWC as per salesforce documents. As a workaround we need to create a container Lightning aura component and add the LWC components to it and the Lightning aura component can be added to quick actions.

To add lightning component to a quick action go to setup-> Object manager -> Object -> Buttons & Actions and add the lightning component. The component should have the force:lightningQuickActionWithoutHeader interface

LogACall.cmp

<aura:component implements="force:lightningQuickActionWithoutHeader,force:hasRecordId" access="global">
    <c:LogaCall_LWC recordId="{!v.recordId}" onsave ="{!c.saveFromLWC}"/>
</aura:component>

LogACall.js
({
saveFromLWC : function(component, event, helper) {
         $A.get('e.force:refreshView').fire();
}
})
The lightning aura component will call the LWC component and also we are passing a custom even from LWC component to refresh the page after action is complete. I am refreshing it from lightning aura component instead of LWC since I couldn't find a way to refresh from LWC.

Also , we are passing the record id to LWC component which will be captured from the lightning aura component associated to the quick action from the record page.

Here are the detail of LWC

LogaCall_LWC.html


<template>
    <lightning-card title="Log a Call" icon-name="action:call" >
     
            <div class="slds-m-around_medium">
             
                <div class="dropdown">
                    <lightning-combobox
                        name="Subject"
                        label="Search Subject"
                        value={rec.Subject}
                        placeholder="Select Subject"
                        options={options}
                        onchange={subjectChange} >
                    </lightning-combobox>
                </div>
                <lightning-input type="text" label="Comments" value={rec.Description}  onchange={descChange}></lightning-input>
                <lightning-input type="checkbox" label="Public" value={rec.IsVisiblePortal} onchange={visibleChange}></lightning-input>
                           
             
                <div class="slds-m-top_medium">
                    <lightning-button type="submit" variant="brand"
                                label="Log a call"
                                class="slds-m-top_medium"
                                onclick={handleLAC}>
                    </lightning-button>
                </div>
            </div> 
         
     
    </lightning-card>

</template>


LogaCall_LWC.js


import { LightningElement,api, track } from 'lwc';
import SUB_FIELD from '@salesforce/schema/Task.Subject';
import COMMENT_FIELD from '@salesforce/schema/Task.Description';
import PUBLIC_FIELD from '@salesforce/schema/Task.IsVisibleInSelfService';
import {ShowToastEvent} from 'lightning/platformShowToastEvent';
import logACallInit from '@salesforce/apex/LogaCallController.logACallInit';

/* eslint-disable no-console */
export default class LogaCall_LWC extends LightningElement {

    @api recordId;
    @track subject = SUB_FIELD;
    @track description = COMMENT_FIELD ;
    @track isVisiblePortal =PUBLIC_FIELD ;
    @track success;
    @track error;

    @api rec = {
        Subject : this.subject,     
        Description : this.description,     
        IsVisiblePortal: this.isVisiblePortal,
    }

    get options() {
        return [         
            {label:'Call Inbound',value:'Call Inbound'},         
            {label:'Call Outbound',value:'Call Outbound'},
       ];
    }
 
 
    subjectChange(event) {
        console.log('SUBJECT ::'+event.target.value);
        this.rec.Subject = event.target.value;     
    }

    descChange(event) {
        console.log('Comment ::'+event.target.value);
        this.rec.Description = event.target.value;
    }

    visibleChange(event) {
        console.log('Visible to Portal::'+event.target.value);
        this.rec.IsVisiblePortal = event.target.value;
    }

 
    handleLAC(){
     
        createLAC({           
            tsk: this.rec
            })
            .then(result => {
                    this.message = result;
                    this.error = undefined;
                    if(this.message !== undefined) {
                     
                        this.rec.Subject = '';
                        this.rec.Description = '';
                        this.rec.IsVisiblePortal = false;
                        this.dispatchEvent(
                            new ShowToastEvent({
                                title: 'Success',
                                message: 'Record successfully created',
                                variant: 'success',
                            }),
                        );
                     
                    }
                 
                    const buttonEvent = new CustomEvent('save');
                    // Fire the custom event
                    this.dispatchEvent(buttonEvent);
                 
                    console.log(JSON.stringify(result));
                    console.log("result", this.message);
            })
            .catch(error => {
                                     
                this.error = error;
                this.message = undefined;
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Error creating record',
                         message: error.body.message,
                        variant: 'error',
                    }),
                );
                console.log("error", JSON.stringify(this.error));
            });

         
         
    }
}


LogaCallController.apxc


public class LogaCallController {
@AuraEnabled
    public static String logACallInit(Task tsk) {
         
        try{
          tsk.whatId= object.Id;
          tsk.RecordTypeId=recordType.id;
          tsk.Status ='Completed';
          tsk.Type='Call';
          insert tsk;
          return 'Success';
        }catch(Exception ex){
          return 'Exception :'+ex.getMessage()+'Line # :'+ex.getLineNumber();
        }
     
    }

}

Comments

Popular posts from this blog

Einstein Bot user authentication

Using Bot for data manipulation use case in your company requires need of implementing some extra security layer to make it spoof proof. One of exciting salesforce feature we have is the well known Einstein Bot which many companies have implemented. I am going to cover detail step-by-step implementation of User validation use case using encrypted token. STEP-1: Create a Site & Site User go to setup > Sites & Domains > Sites Create a Site and make your user as "Site Contact". This is a prerequisites for live agent or Embedded Service setup. STEP-2 : Create a Embedded Service(formerly snap-ins) go to Service setup > Embedded Service Create a new Embedded Service Deployment record and copy the embedded service code snipped generated in a notepad. STEP-3  : Create a Visualforce page to test the chatbot (it will simulate the actual web portal which is going to consume the embedded service snipped.) BotController.apxc public class BotControlle...

Dynamically populate lightning:combobox

In order to dynamically populate values in lightning:combobox using javascript we can create a list variable and push the values. We also require the values like ids associated to the particular option for back-end logic. For example  : If we want to show the list of contacts using lightning:combobox component where the option should display the contact name but the value should be the contact id which will be used for back-end logic For this we can create a temporary Map variable in our lightning component and set the value and label parameters of the lightning:combobox. Here is the code for this component Combo.app <aura:application extends="force:slds" access="global" description="Lightning Combo Demo"> <c:Combo_Cmpt/> </aura:application> Combo_Cmpt.cmp <aura:component implements="flexipage:availableForAllPageTypes" access="global" controller="ComboController"> <!-- ...

Use of wrapper class in lightning:datatable

As you guys know the wrapper class concept in visualforce pages , we generally use it to create a data-table which fetches data from different objects or if we want to redirect user to some other page on click of a link as one of the column of data-table.        For example we want a column "Account Name" on the data-table which is a link and once user clicks it should redirect respective account record. Or , suppose we want to display a column with some images or icons as a visual indicator Or what not. These requirements require us to use a wrapper on the lighting data-table (lightning:datatable) I am going to use my previous account search example ( Account Search Lightning Component ) and explain the use of wrapper. AccountSearchWrapper.app <aura:application extends="force:slds" access="global" >     <c:AccountSearchWrapper /> </aura:application> AccountSearchWrapper.cmp <aura:component controller="...