function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Chris760Chris760 

How to write a custom DML Exception Error Message

I have an After Update trigger that updates the fields on a parent object, and that object has several validation rules that have the potential to fire if they're met when the After Update trigger runs.

 

Instead of the normal DML exception though where it reads like gibberish and sends me an error email and leaves the user scratching their head, how would I display a custom error message, prevent the DML insert, and have the system NOT clog my email with exception messages?

 

The section of my code that triggers the DML is as follows:

 

    try{
    update processedOpportunitiesList;
    } catch(DMLException e){
        if(e.getMessage().contains('FIELD_CUSTOM_VALIDATION_EXCEPTION, Please enter Shipping Address, City, State and Zip')){
            e.addError('You can\'t select this stage until you first enter the Shipping Address info on the opportunity.');
        }
    }

 In the above code, I'm trying to say that, if running the DML update with the list "processedOpportunitiesList" results in a DML exception that contains the sentence "FIELD_CUSTOM_VALIDATION_EXCEPTION, Please enter Shipping Address, City, State and Zip", then I want to instead display "You can't select this stage until you first enter the Shipping Address info on the opportunity." to the user, prevent salesforce from inserting the record, and have it not email me about the exception.

 

However, I seem to not be grasping how this particular class is structured or how I'm supposed to write it.  I keep getting "Method does not exist or incorrect signature" errors as I fiddle around with the code and I'm realizing that I'm not "getting" this and I should probably ask on here for help.  Can someone please tell me how I would need to write or structure this trigger properly?

 

Thanks.

Sapana WSapana W

for(Object__c oNew : trigger.new)
{

  for(Object__c sOld : sOldList)
  {
    if(oNew.StartDate__c >= sOld.StartDate__c && oNew.StartDate__c <= sOld.EndDate__c)
    {
      oNew.addError('Overlap Session');
    }
  }
}

 

Try this out. Let me know if you face any issue.

AditiSFDCAditiSFDC

Hi,

My first suggestion is to manually check any opportunity missing Shipping address, city, state or zip and addError() to that particular record with your custom message and rerender the <apex:pageMessages> to display error on page.

 

for(Opportunity o : OpportunityList) {
    if(o.City ==null || o.State == null) {
         o.addError('You can\'t select this stage until you first enter the Shipping Address info on the opportunity.');
    }
}

 

If above solution doesn't work for you then use ApexPages.addMessage() but it will not remove the existing "Field Validation Error", it will only add your custom message also.

 

try {
	insert processedOpportunitiesList ;    
} catch(DMLException e ) {
	if(e.getMessage().contains('FIELD_CUSTOM_VALIDATION_EXCEPTION, Please enter Shipping Address, City, State and Zip')){
		e.setMessage('You can\'t select this stage until you first enter the Shipping Address info on the opportunity.');
		ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, e.getMessage()));

	} 
}