0

When I insert a Case I have a trigger that automatically inserts an opportunity among other things to that Case. When something fails due to an expected error I want to be able to customize the error message and display to the user why it failed. I know this is possible straight from the Case Trigger using caseInstance.addError('customMessage'); but I want to throw the custom message from the Opportunity Trigger handler since this is where it fails in the image below. (This is supposed to fail, I just want to customize a more user friendly message for the user).

Does anyone know if this is possible? enter image description here

  • Where is the DML message coming from? – Raul May 23 '19 at 11:07
  • It's coming from inside the Opportunity Trigger. From this method marked in red is the message I want to display when trying to insert the Case: prntscr.com/nsbhbp – Andegosu May 23 '19 at 11:18
0

In the trigger or a central place, I usually add try catch block to handle DML exception and throw error to display appropriate error message. Psuedo code:

try {
    // your code
} catch(System.DmlException dmlEx) {
    // handle DML exception to display proper error message
    for (Integer i = 0; i < dmlEx.getNumDml(); i++) {
        Trigger.new[0].addError(dmlEx.getDmlMessage(i));
    }
} catch(System.Exception ex) {
    // handle any other exception
    Trigger.new[0].addError(ex.getMessage());
}

There could be a better solution though. But this may be bit better than string manipulation to cut out proper error message.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.