[SalesForce] Validation rule Error message not displaying in LWC UpdateRecord toast event

I have a record which I'm updating via the UpdateRecord method from LWC, using Toast events to display success or error messages, which all works fine. What I'm not getting though, when there's a validation error, is the error message from the validation rule. I'm getting a generic error message which is not much good as it doesn't tell me what's wrong.

  updateRecord(record)
  .then(() => {
    this.dispatchEvent(
      new ShowToastEvent({
        title: "Success",
        message: "Record has been " + this.resultmessage,
        variant: "success"
      })
    );
  })
  .catch((error) => {
    this.dispatchEvent(
      new ShowToastEvent({
        title: "Error on update",
        message: error.body.message,
        variant: "error"
      })
    );
  });

Instead of seeing the Validation Rule message, I get this:
enter image description here
What do I need to do to display the Validation Rule error?

UPDATE I've tried displaying the error by using this instead:

   .catch((error) => {
    this.dispatchEvent(
      new ShowToastEvent({
        title: "Error on update",
        message: error.body.output.errors[0].message,
        variant: "error",
        mode: "sticky"
      })
    );

but this returns a long ugly error:

MyTrigger: execution of AfterUpdate caused by: MyTriggerHandler.CustomException: This account requires XYZ to be done before approval. Class.MyTriggerHandler: line 448, column 1 Class.MyTriggerHandler: line 151, column 1 Class.TriggerDispatcher.Run: line 35, column 1 Trigger.MyTrigger: line 11, column 1

I went back into the class and created a custom Exception:

public class CustomException extends Exception {}
...
try {
  update accToUpdate;
} catch (DMLException e) {
  string errorMessage = e.getDmlMessage(0) == null
    ? e.getMessage()
    : e.getDmlMessage(0);

  system.debug('####catch DML Exception errorMessage: ' + errorMessage);
  system.debug('####catch DML Exception e.getDmlStatusCode: ' +        e.getDmlStatusCode(0));
  throw new CustomException(errorMessage);
}

In my debug log the debug statement shows errorMessage to be just the message (ie the 'This account requires XYZ to be done before approval.') without the class name and line numbers, so I know it goes in there and debug stops at that point after the throw.
When I run this from my LWC, however, it still just displays the original error, not my custom one.

I've also tried changing the throw to AuraHandledException, but it still doesn't display just the custom message. All that changes is instead of in the long ugly error message it saying CustomException, it says AuraHandledException.

What do I need to do?

Best Answer

if you see the error message in console, error.body.message is having a generic message:- An error occurred while trying to update the record. Please try again as value.

You need to fetch the error message from the below json structure:-

error.body.output.errors[0].errorCode + error.body.output.errors[0].message

so the working code would be like:-

this.dispatchEvent(
      new ShowToastEvent({
        title: "Error on update",
        message: error.body.output.errors[0].errorCode + '- '+ error.body.output.errors[0].message,
        variant: "error"
      })
    );

enter image description here

Related Topic