Search code examples
salesforcevisualforce

Capture only the error message from validation rule and display in VF page?


i have set up a few validation rules and i have a VFpage using this object. I am able to capture the validation exception and display it on the VF page. The issue is that it shows message as

   " Upsert Failed : First exception on row 0: first error;

     FIELD_CUSTOM_VALIDATION_EXCEPTION Quantity cannot be empty

Is there a way i can just capture the error message in validation Rule "Quantity cannot be empty" ?

Thanks


Solution

  • Visualforce can do this for you

    1. Add the <apex:pageMessages/> tag to your page. (This is the container that displays any error messages if present)
    2. Surround your DML call with try {} catch(DMLException e) {} (When you catch the exception you won't get redirected to the error pages, but salesforce will automatically create a "PageMessage" for the validation failure.