function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
nishad basha 7nishad basha 7 

How to override the standard error messages in salesforce?

can you please  give me the example of that code.
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Can you explain your scenario little more ?
nishad basha 7nishad basha 7
Hi, Ashish_Sharma_DEVSFDC

How to display the custom error message in visualforce page (here iam using the  custom object&fields, when i wont enter data and click save that time it show standard errors.but we want to create my own custom error messages.what can i do for this?.     please give any example related that one.
JayantJayant
Use Try - Catch in your controller and create a message in catch block and add to the page. You will also need to have the <apex:pageMessages> component on your page to display the custom messages you create.

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please enter search criteria.');
ApexPages.addMessage(myMsg);

eg.
try{
//your code goes here
}
catch (Exception e){
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Error: Please enter search criteria.');
ApexPages.addMessage(myMsg);
}
JayantJayant
If this is resolved, please do mark the question as Resolved and the most appropriate/helpful answer as the best answer :).
If none of the answers helped you significantly, please post the solution. You may also mark your solution as the best answer.
nishad basha 7nishad basha 7
Hi, Jayant

how to display the custom error message using visualforce? please give me the example of that scenario.
 
JayantJayant
Error: Please enter search criteria. This is the error message that you will see on your VF page with Salesforce styling instead of the actual Exception message (that you caught).
 
ApexPages.Severity.ERROR. This enum will show it either as a Warning, Info or Error.

catch (Exception e){
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Error: Please enter search criteria.');
ApexPages.addMessage(myMsg);
}


If you do not want to create a message in the Controller, you may also use <apex:pageMessage> on the Page and define the message their itself. You may use a variable to decide whether this message is rendered or not.
sunny522sunny522
Hi nishad basha 7,
We have one alternate.
Steps
Step 1: In vf page,First use required = false for that required field so that custom error messages will be displayed.
Step 2: Use sample code below to make that field required
 <apex:outputPanel layout="block" style="float: left">
          <apex:outputPanel >
                <div class="requiredInput"><div class="requiredBlock"/>
                      <apex:inputField value="{!acc.name}" required="false" />          
                </div>
          </apex:outputPanel>                              
   </apex:outputPanel>

Go through the example in the link below.Hope it helps you.
http://salesforceglobe4u.blogspot.in/2016/08/how-to-override-standard-required-field.html

Let me know if you need any further help...
pramod vemulapramod vemula

HI all,

  I am getting two exceptions those are one is standard salesforce error.

 and the other one ( warning one) is from try/Catch block.

    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,ex.getMessage()));  
can you suggest me hoe to stop standard message error.

 

User-added image

Prudhvi Paladugu 2Prudhvi Paladugu 2
Hi Pramod,

Where you able to override the standard Salesforce Error?

Thanks,

Prudhvi