0

We have a unique field that blocks multiple records for being submitted for the same date. How do I customize this error message? Here is the section of my code grabbing the exception:

    public PageReference saveRecord(){
        try {insert fc;}
    catch(DMLException e){
            system.debug(e);
            ApexPages.addMessages(e);  
            return null;
        }

I simply have a tag for messages at the top of my VF page:

    <font color = 'red'>
        <apex:messages id="errorMsg" />
    </font>

enter image description here

  • Adding an apex:pageMessages component to the top of your page and then add some messages in programmatically via the ApexPages methods instead of showing the exception message because you can't customize the unique field error message. – codeyinthecloud Jun 14 '18 at 14:08
  • @RajeshVarmaMudunuri that makes sense, how do I add the messages programmatically? Do you have an example? – Jean Grey Jun 14 '18 at 17:52
  • Please look into this blog, this can give you some insight. sfdcpoint.com/salesforce/show-error-message-visualforce-page – codeyinthecloud Jun 14 '18 at 18:01
  • You can also create custom exceptions and throw a user friendly exception by creating a class that extends Exception. – codeyinthecloud Jun 14 '18 at 18:03
0

Like i mentioned in my above comments you need to do your own error handling instead of depending on data uniqueness error from salesforce because it is not customizable.

You need to do something like below.

Visualforce page

<apex:pageMessages />

Apex Controller:

  ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error Message.');
  ApexPages.addMessage(myMsg); 

 (Or)

 ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error Message.'));

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.