3

I creating a Screen Action LWC on Account Record

But I want to launch the Screen Action only if some fields are blank, in our example: Report__c

So I wanted to implement connectedCallback() in order to check before the rendering the fields, and if it's not blank it will close the Quick Action with a Toast Message such as " A report already exist in this account.." I also tried renderedCallback() but it is worse.

getAccountReport.html :

<template>
<div class="spinner">
    <template if:true={isLoading}>
         <lightning-spinner class="spins" alternative-text="Loading" variant="brand" size="medium">
         </lightning-spinner>
    </template>
</div>
    <div class="slds-var-p-around_small">
        <lightning-card variant="narrow">
            <h1 slot="title">Are you sure to Request a Report for this Account ?</h1>
            <div slot="footer">
                <lightning-button class="slds-var-p-around_x-small" label="Cancel" onclick={closeAction}></lightning-button>
                <lightning-button class="slds-var-p-around_x-small" type="submit" label="Get Report" variant="brand" onclick={handleGet}></lightning-button>
            </div>
        </lightning-card>
    </div>

getAccountReport.js :


import { LightningElement, api, track, wire } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { getRecord } from 'lightning/uiRecordApi';
import NAME_FIELD from "@salesforce/schema/Account.Name";
import REPORT_ACC_FIELD from "@salesforce/schema/Account.Report__c";
import { CloseActionScreenEvent } from "lightning/actions";
const FIELDS = [NAME_FIELD, REPORT_ACC_FIELD]; 

import getAccountReport from '@salesforce/apex/WS_Account_Report.getAccountReport';

export default class getAccountReportQALWC extends LightningElement {
    @api recordId;
    @track isLoading = false;
    @wire(getRecord, {
        recordId: '$recordId',
        fields: FIELDS
    })
    account; 
    handleGet() {
        this.isLoading = true;
        getAccountReport({accId : this.recordId })
            .then((result) => {
                this.isLoading = false;
                if(result != true && result != 'true'){
                    this.showToastMessage(1,result);
                }
                else{
                    this.showToastMessage(2,'The report was successfully downloaded');
                }
                this.dispatchEvent(new CloseActionScreenEvent());
            })
            .catch((error) => {
                console.log(error);
            });
    }

    showToastMessage(fromWho, mess, urlToast) {
        var variantToast;
        var titleToast;
        if(fromWho == 1){
            variantToast = 'info';
        }
        else if(fromWho == 2){
            variantToast = 'success';
            titleToast = 'Success'
        }
        const toastMessage = new ShowToastEvent({
            title: titleToast,
            message: mess,
            variant: variantToast,
            messageData: [
                {
                    url: urlToast,
                    label: 'here'
                }
            ]
        });
        this.dispatchEvent(toastMessage);
    }

    connectedCallback() {
        if(this.account.data.fields.Report__c.value){
            //This doesn't work
            this.closeAction();
            //This is working
            this.showToastMessage(2,'A report already exist in this account..');
        }
    }
    closeAction() {
        this.dispatchEvent(new CloseActionScreenEvent());
    }
}

Why the close action doesn't work in connectedCallback() ?

How can I do to not open a screen action when the account doesn't meet the requirement ?

1 Answer 1

2

You can use imperative apex here as your apex calls needs to be dependent.

You can call fire an apex call first to fetch the data for Report__c field and show the toast if Report__c is true and fire CloseActionScreenEvent orelse do your other logic.

4
  • It's seems a good way ! But where I call my imperative apex ? Because if it's in connectedCallback() I just can't fire a CloseActionScreenEvent
    – Yaac0
    Jul 8, 2021 at 6:47
  • Try calling it after the apex has returned the value. Jul 8, 2021 at 6:57
  • But where I call it ? It's not clear, I just want the screen action to not be displayed at all if the account doesn't meet the requirement ?
    – Yaac0
    Jul 8, 2021 at 8:03
  • Do you want to hide the Quick Action button? Jul 8, 2021 at 8:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .