Before updating a record I want to check a text field is black or not. If the text field is black then only my record is going to update or else I want to throw an Error message in ShowToastEvent.
@AuraEnabled
public static void saveRecord(Id caseId) {
Case caseRec = [SELECT Id, Checkbox__c, Textfield__c FROM Case WHERE Id = :caseId];
if(String.isNotBlank(caseRec.Textfield__c)){
throw new AuraHandledException('Unable to update the case as the Textfield is blank.');
}
caseRec.Checkbox__c = true;
update caseRec;
}
Here I unable to get the error message in UI whereas I can see this AuraHandledException('Unable to update the case.');
in developer Console, But I can see the success message after the record is updated
Javascript
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class CaseUpdate extends LightningElement {
@api recordId;
caseMigration() {
saveCaseRec({caseId: this.recordId})
.then(()=>{
const evt = new ShowToastEvent({
title: 'Case updated',
message: 'Operation sucessful',
variant: 'success',
mode: 'dismissable'
}).catch(error => {this.error = error;});
this.dispatchEvent(evt);
});
}
}