+ Start a Discussion
nishad basha 7nishad basha 7 

How to display the custom error message using visualforce page

i want to display the custom error  message .how to solve the above solution please give any ideas.
NagaNaga (Salesforce Developers) 
Hi Nishad,

We can implement this requirement by creating new instance of ApexPages.message and then adding message to Apexpages using ApexPages.addmessage. Then displaying these messages in visualforce page.

We can add 5 different types of message in Visualforce Page. In the example below, we are showing 5 input fields of account. We have added a button on visualforce page. Different type of message will be shown on visualforce page if we will keep any field blank.


Please follow the below link for more info

http://www.sfdcpoint.com/salesforce/show-error-message-visualforce-page/

http://sfdcsrini.blogspot.com/2014/12/show-error-message-in-visualforce-page.html

Best Regards
NagaKiran
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi,

Please try below code.
Apex:-
if (fieldname == null)
{
    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter value'));
    return null;
} 
 
Insert the below tag inside the visualforce page within page block
 
<apex:pageMessages ></apex:pageMessages>
Let us know if it helps.
 
nishad basha 7nishad basha 7
Hi, Ashish_Sharma_DEVSFDC

can you please give me  the example of positive test cases in salesforce
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi nishad basha 7,

Here is an example.
@isTest 
private class HelloWorldTestClass {
    static testMethod void validateHelloWorld() {
       Book__c b = new Book__c(Name='Behind the Cloud', Price__c=100);
       System.debug('Price before inserting new book: ' + b.Price__c);

       // Insert book
       insert b;
    
       // Retrieve the new book
       b = [SELECT Price__c FROM Book__c WHERE Id =:b.Id];
       System.debug('Price after trigger fired: ' + b.Price__c);

       // Test that the trigger correctly updated the price
       System.assertEquals(90, b.Price__c);
    }
}

Let us know if it helps.
nishad basha 7nishad basha 7

Hi, Ashish_Sharma_DEVSFDC

How to throw  the custom error message  using custom object and don't fill the required fields and click save it will display custom error message .how to find  this issue give some ideas.
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi nishad basha 7,

Please try below code and let us know if it helps you.
 
trigger testTrigger on Account (before insert,after update) {

     for (Account newAcc : Trigger.new) {
         if(newAcc .Your Field == null){
             newAcc .addError('See error  message will appear when u insert or update an account');
         }
     }
}

 
nishad basha 7nishad basha 7

Hi, Ashish_Sharma_DEVSFDC

can you please give me  the example of how to override the standard error messages in visualforce page.
nishad basha 7nishad basha 7
Hi, Naga

my requirement you must have one  require field,when i click the save (don't fill the values) it will show custom error message.please give one example of that code
vipin indoravipin indora

Hi nishad basha 7

override the standard error messages in vf page
<style>
     .errorM4 .msgIcon, .errorSmall { background-image: url(/img/samples/flag_red.gif) !important; }

     .myCustomMessage .message { background: none !important; border: none !important; }
</style>

<apex:outputPanel styleClass="myCustomMessage">
      <apex:pageMessage severity="error" strength="1" summary="Your ErrorMessage" />
</apex:outputPanel>
 

Wassim Khan 1725Wassim Khan 1725
Hi Nishad,
On button write actionfunction like below:
<apex:commandButton onclick="return checkRequired();" value="Save" oncomplete="refreshPage()"/>
<apex:actionfunction name="save" action="{!Save1}"/> </apex:pageblockButtons> 
and on Apex page write :
for(String productName : productsList ) {
                    if(productName == 'Test& caseRec.fieldName)          
                        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Field should not be empty when selected');
                        ApexPages.addMessage(myMsg);
                        isError = true;
                    }
Wassim Khan 1725Wassim Khan 1725
Hi Nishad,
On button write actionfunction like below:
<apex:commandButton onclick="return checkRequired();" value="Save" oncomplete="refreshPage()"/>
<apex:actionfunction name="save" action="{!Save1}"/> </apex:pageblockButtons> 
and on Apex page write :
for(String productName : productsList ) {
                    if(productName == 'Test' && caseRec.fieldName == 'Test')          
                        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Field should not be empty when selected');
                        ApexPages.addMessage(myMsg);
                        isError = true;
                    }

Please let me know if you need any help further!!!!
Cheers
sd Noorsd Noor
Hello ,
I am new sfdc.... can anyone help me please1

How to display our own error message in the vf page . when the field is required or mandatory!.... through the controller .

 
Venkatesh Kumar SVenkatesh Kumar S
We are trying to remove all the custom buttons which was using Java Script code and replacing the same with VF buttons.(Migrating towards lightning)
However, i am not able to display the validation error message on my standard page layout(where the button is actually placed) which we are trying to validate on the apex controller class.

Example :

Controller Code
if(acc.phone == '' || acc.phone == null) ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter Account phone'));

VisualForce Code
<apex:pageMessages id="showmsg"></apex:pageMessages>

Is there anyway we can display the validation error message from my controller code to the standard page layout or even custom VF page in some cases.
Thanks in advance and appreciate your help.
Venkatesh