+ Start a Discussion
RelaxItsJustCodeRelaxItsJustCode 

Need help with Exception error formatting???

I created a trigger that would look through a custom objects records that is related via a lookup to the case object. The validation does work and stops the case from being closed if training hasn't been completed or cancelled, while that said then the training status would be in Pending or Scheduled status.

Below is the trigger I'm using, the problem I need to solve is when the validation via the trigger below is triggered, the big nasty error message below this trigger code appears at the top of the case when I'd rather it just say, "Training has not been completed or cancelled"...  Any ideas?
 
trigger CaseCloseTrigger on Case (after update) {
    public class CaseCloseException extends Exception{}
 
    for(Training_Information__c customobj : [Select Id, Status__c From Training_Information__c Where Case__c in: Trigger.newMap.keySet() AND Case_Status__c = 'Completed']){
        if(customobj.Status__c == 'Pending' || customobj.Status__c == 'Scheduled'){
            throw new CaseCloseException('Training has not been completed.');
        }
    }
}



Error: Invalid Data. Review all error messages below to correct your data. Apex trigger CaseCloseTrigger caused an unexpected exception, contact your administrator: CaseCloseTrigger: execution of AfterUpdate caused by: CaseCloseTrigger.CaseCloseException: Training has not been completed.: Trigger.CaseCloseTrigger: line 8, column 1
Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

Your trigger is not bulkified but this will help you get the error you intend:

 

change

 

throw new CaseCloseException('Training has not been completed.');

 

to

 

trigger.new[0].addError('Training has not been completed.');