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 ?