Handle validations from trigger to show as exception or toast on managed package layout

exceptionlightning-aura-componentslightning-web-componentsstringvalidation

I currently have a custom pagelayout from a managed package. In this pagelayout a list of records can be selected to create new records for another object. I have a before apex trigger in which I want to validate some custom fields which are not part of the managed package for the new records.

I want to show my validations before exceptions.

I cant add a lightning component to that pagelayout to catch the validations/exceptions since its managed.

Currently I can throw an error as follows:

throw new applicationException(m);

But the result message is ugly since I want to show a list of records and the validations they failed with a brakeline. This is the current result:

JobApplicationTrigger: execution of BeforeInsert caused by: JobApplicationTriggerHandler.applicationException: <br/>ERROR: Please have a look a following Candidates and/or Jobs. <br/>Jobtest has not the status ready. <br/>Candidate: Candidatetest GDPR status is not approved<br/> ()
Error is in expression '{!create}' in component <apex:commandButton>

I also tried to redirect the user to an url where I would display the validation error(s) but this does not work as follows:

  ..     
public JobApplicationTriggerHandler(){
        }
    
         public static PageReference redirectToPage(){
    
          PageReference retURL = new PageReference('https://www.google.com/');
          retURL.setRedirect(true);
          return retURL;
    
     }
..





@AuraEnabled
    public static void beforeInsert(List<application__c> newList){
..
if (result == true){  
 
redirectToPage();
 throw new applicationException(m);
}
..
}
  1. Can I add breaklines to the applicationException?
  2. If not how can I redirect the user to another page/lightning component to show the missed validations

Thanks in advance

Best Answer

Your redirectToPage method does nothing, as it doesn't return the PageReference back to the client. Further, you can't both throw an exception and return a value. In addition, you should be throwing an AuraHandledException if that's your intent; ApplicationException isn't a standard Exception for Lightning components.

Can I add breaklines to the applicationException?

No, the errors will have their whitespace collapse, so it's impossible to make those line breaks work as you expect.

If not how can I redirect the user to another page/lightning component to show the missed validations

The actual process would involve something like:

Map<Integer, String> errors = new Map<Integer, String>();
Database.SaveResult[] results = Database.insert(newList, false);
for(Integer index = 0; index < results.size(); index++) {
    if(!results[index].isSuccess()) {
        errors.put(index, results[index].getErrors()[0].getMessage());
    }
}
if(!errors.isEmpty()) {
    throw new AuraHandledException(JSON.serialize(errors));
}
Related Topic