2

I need to write a trigger on a Enhanced Notes ( which are stored as ContentNote records ). I need to match a certain text in the note body and then show the custom exception on the note component itself ( which is a standard component and does not show up in the Classic ).

Requirement: Show custom error message

Tried so far: addError(), AuraHandledException, custom built in exceptions

Issue: The only error message that the component shows is the generic one that goes like 'Unable to insert record. Please contact your salesforce admin.' enter image description here I have tried the above but it never shows the error messages that I put up. I believe this is because the lightning component also needs to be having the code that handles the exception cases which it is doing, but it is only showing the standard error message that it is bound to show.

This is what I have tried: P.S. I have removed the irrelevant code

public class ContentVersionTriggerHandler {
    public static void beforeInsertUpdate( List<ContentVersion> newList ) {
        for( ContentVersion cv : newList ) {
            if( CRITERIA_MATCHED ) {
                throw new AuraHandledException( 'Error occurred' );
            }
            //used this without try catch - throws the error but not this custom message
            /*if( match.find() ) {
                cv.addError( 'Error occurred' );
            }*/
        }
    }
}
  • 1
    How did you create the custom exception? Please edit your post to show us your code. – crmprogdev Nov 12 '18 at 5:45
  • @crmprogdev Apologies for poorly asked question, I have added the code and updated the screenshot as well. – Mohit Arora Nov 12 '18 at 9:13
0

This has been logged by Salesforce as a known issue. See this link: https://success.salesforce.com/issues_view?id=a1p3A0000003UvtQAE&title=lightning-experience-notes-trigger-adderror-method-on-contentversion-contentdocument-doesn-t-display-the-custom-error-message-for-enhance-notes

| improve this answer | |
1

As per error, the object which you are accessing is not available. So may be your IF condition is throwing error.

Please try to use try catch block and throw custom exception in catch block also.

public class ContentVersionTriggerHandler {
public static void beforeInsertUpdate( List<ContentVersion> newList ) {
    try
    {
        for( ContentVersion cv : newList ) {
            if( CRITERIA_MATCHED ) {
                throw new AuraHandledException( 'Error occurred' );
            }
        }
    }
    catch(exception ex)
    {
        throw new AuraHandledException( 'Error occurred' );
    }
}

}

| improve this answer | |
  • The code is throwing the error at the desired place, the issue is that the message that I am passing while throwing the error ( in this case, 'Error occurred' ), is not showing up. Instead the code shows up the message that is generic and also not relevant. – Mohit Arora Nov 13 '18 at 6:10
  • Is it Before or after trigger? Can you post trigger code? – Dhanik Lal Sahni Nov 13 '18 at 11:06
  • It is a before trigger as the requirement that I am trying to accomplish cannot be completed via a after trigger. – Mohit Arora Nov 13 '18 at 12:18
  • public class HandleCustomException extends Exception { public static void LogException(string e) { System.debug('My Custom Excetion' + e); } } and call like HandleCustomException.LogException('Error occurred' ); – Dhanik Lal Sahni Nov 13 '18 at 13:24
  • Logging the exception like this way only debugs the exception message in the logs. It does not show the exception message on the notes component. – Mohit Arora Nov 13 '18 at 17:10

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.