0

I have a custom component to create quote and also have few validation rules been created. Whenever i am trying to create the quote, my validation rules are getting triggered and able to display them on UI, but Expected: i should see only validation message Actual: Im able to see the whole message like this. ValidationError

I am expecting to display only the highlighted error to display on components UI.

Apex class:

 try{
                insert qu;          
            }catch(Exception e){            
                throw new AuraHandledException('Error Details : '+e.getMessage());           
            }

javascript

if(state == "ERROR"){
                        var errors = a.getError(); 
                        //console.log(errors);
                            component.set("v.showErrors",true);
                            component.set("v.errorMessage",errors[0].message);                           
                    }

Component

<aura:if isTrue="{!v.showErrors}">       
        <div class="slds-notify slds-notify_toast slds-theme_error">
            <span class="slds-assistive-text">error</span>
            <div class="slds-notify__content">
                <h5 class="slds-text-heading_small slds-align_absolute-center">Error Message </h5>
                <br/>
                <p class="slds-align_absolute-center">{!v.errorMessage}</p>                
            </div>
        </div>
    </aura:if>

Please help me with the solution

1 Answer 1

2

If you catch this exception in DmlException catch block then you will get only validation error message. This exception is caused by a failure in DML operation so it should be catch in DmlException catch block.

Catch DmlException in your code like this.

try{
       insert qu;
    }catch (DmlException ex) {
        throw new AurahandledException('Error Details : ' + ex.getDmlMessage(0));
    } catch (Exception ex) {
        throw new AurahandledException('Error Details : ' + ex.getMessage());
    }
2
  • Hurray, it worked for me. thanks for your help bilal malik Mar 6, 2020 at 7:25
  • Great, Happy to help :) Mar 6, 2020 at 7:27

You must log in to answer this question.

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