2

I have seen the other threads discussing this issue where the validation rule error messages from a Trigger are added to the VF pages <apex:pageMessages />. However, in my case, we have a custom object that will be inserted and in the case of a DML exception through the validation rule, an update is performed instead. In my case, I do not want the VF page to show the exception message at all. I have tried

try
{
    // insert operation
}catch(DmlException dex)
{   
    ApexPages.addMessages(dex);
    // perform my update operation 
    // if OK, remove the apex message. 
    ApexPages.getMessages().clear();
}

I have also tried to remove Apex Messages in my controller class before I add a success message if everything else is OK. But no matter, what I do, the exception message from the DML exception is shown on the screen.

If you think, this is badly designed. I would like to hear some ideas on how to overcome it as well. The trigger is written for duplicate check and the field that indicates the duplicate is actually another SObject. So, I am unable to do this through a simple duplicate check on the variable. Would be happy for any help or guidance.

Thanks!

-- New edit

I only added the ApexPages.addMessages(dex) above because it was suggested as a way to handle this issue.

But even without that, I can see the custom message popping up in my container.

This is the original code I had:

try {
         insert myObj;
    }catch(QueryException e) { 
        System.debug('QueryException in insertOrUpdate : '+e.getMessage());
        throw new CustomException('Unable to XXX',e);
    }
    catch(DmlException dex)
    {
        System.debug('DmlException in insertOrUpdate: '+dex.getMessage());
        if(dex.getMessage().contains('FIELD_CUSTOM_VALIDATION_EXCEPTION'))
        {
             try {
                   update myObj; 
                   return 'SUCCESS';
                 }catch(QueryException e) { 
                      System.debug('QueryException in insertOrUpdate : '+e.getMessage());
                      throw new CustomException('Unable to XXX',e);
                 }
        }
        throw new CustomException('Unable to XXX',e);
    }
   return 'SUCCESS';

1 Answer 1

1

Since you are adding exception object instance into ApexPages.addMessages(dex) thats why it is showing the error message.

Here during performing the update operation, if any exception occurs that will be caught in the exception block.

Approach will be like this:

try
{
    // insert operation
}catch(DmlException dex)
{   
    if(dex.getMessage().contains('<your validation message>'))
    {
        //perform update.
    } 
    else
    {
        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error, dex.getMessage());
        ApexPages.addMessage(msg); 
    }   
}
catch(Exception ex)
{
    ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error, ex.getMessage());
    ApexPages.addMessage(msg);
}
5
  • I would actually throw dex in else block, so you have a single place in the code you display messages
    – nchursin
    Aug 22, 2018 at 9:27
  • Please see the updated code. This was how it was originally written. The custom exceptions are handled at my controller in an appropriate way.
    – Randomizer
    Aug 22, 2018 at 9:30
  • In you newly edited code CustomException('Unable to XXX') is written outside of if() block and thats why always it is throwing the error. Review your code properly. Aug 22, 2018 at 9:56
  • Yes, I have a return before that (inside the if block)! And I can see the 'SUCCESS' string returned as a response. There is no exception thrown from my code on custom validation error. I also have debug logs indicating a successful update and return from that code block. So I don't think that is the issue.
    – Randomizer
    Aug 22, 2018 at 11:10
  • Any further help for this issue? I think the actual issue exists and it has been missed by the above misunderstanding. This is a serious issue I am facing and it would really help if someone could clarify why this happens and how to avoid it.
    – Randomizer
    Aug 24, 2018 at 13:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .