I have a scenario where I'm trying to disable/enable the button and show the text conditionally on the lightning web component based on a picklist field value on the account record page. I'm setting the 'isDisabled' property in the method below "needsLegalEntity" but it is not getting called when I hit save. Can anyone help how can I call the method when I save an account record?
--Code
Example.html
<template>
<lightning-card variant="Narrow" title="Account Info." icon-name="standard:account">
<p>
<lightning-button variant="brand" label="Create" disabled={isDisabled} onclick={openModal} class="slds-align_absolute-center" ></lightning-button>
<template if:false={isDisabled}>
<div class="warning">`
You must select a <strong>Billing Legal Entity</strong> for the account before you can enable Button
</div>
</template>
</p>
</template>
example.js
import { LightningElement, api, track } from 'lwc';
import accountHasLegalEntity from "@salesforce/apex/AccountController.accountHasLegalEntity";
export default class CreateAccountlwc extends LightningElement {
@api recordId;
errors;
@track legalEntity;
@track isDisabled;
needsLegalEntity(){
window.console.log("Inside Method ");
window.console.log ('Record Id: ', this.recordId);
accountHasLegalEntity({accountId : this.recordId})
.then(result => {
window.console.log("Result is: ", result);
this.legalEntity = result;
if(this.legalEntity === true){
this.isDisabled = true;
} else{
this.isDisabled = false;
}
this.errors = undefined;
}).catch(error => {
window.console.log("Error is: ", errors);
this.errors = error;
this.isDisabled = undefined;
});
}
}
--Apex controller Method:
public class AccountController {
@AuraEnabled
public static boolean createCustomerAccount(String accountId) {
Account acct = [SELECT Id, Admin_Account_ID__c FROM Account WHERE Id = :accountId];
acct.Admin_Account_ID__c = 'Pending-' + accountId;
// Perform isAccessible() and isUpdateable() checks here
update acct;
return true;
}
}
The same scenario is working in Lightning components with the below code.
<lightning:card iconName="standard:account" title="Account Info." >
<p>
<lightning:button
label="Create"
onclick="{!c.openConfirmDialog}"
disabled="{!v.needsLegalEntity}"
variant="brand"
class="button-center"
/>
<aura:if isTrue="{!v.needsLegalEntity}">
<div class="warning">You must select a <strong>Billing Legal Entity</strong> for the account before you can enable Button</div>
</aura:if>
</p>
</lightning:card>
({
checkReady : function(component, event, helper) {
var action = component.get("c.accountHasLegalEntity");
var accountId = component.get("v.recordId");
action.setParams({
"accountId": accountId,
});
action.setCallback(this, function(response){
var state = response.getState();
if (state === "SUCCESS") {
console.log(response.getReturnValue())
if (response.getReturnValue()) {
component.set("v.needsLegalEntity", false);
} else {
component.set("v.needsLegalEntity", true);
}
}
});
$A.enqueueAction(action);
},
})
2 respostas
Hi Neelesh,
This might help-:
https://medium.com/@debprotim.seal/how-to-disable-a-lightning-button-in-lightning-web-component-lwc-980690705812