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
kdmrkdmr 

How to create error messages for visualforce page

Hi,

I am using a visualforce page and would like to add some error messages into it for example if some field is empty etc...... I do not want to set a validation rule, but would like to work on the save function in the controller to display the error message. Would be of great help if someone could point me out the direction to do that.

I have one more question relating to displaying an output in visualforce. I have a lookup field, it has quantity field too, i would like to populate the quantity from the master object, if i change the lookup the value for quantity should also change respectively, I tried formulas but they effect after save, I would like to have immediate change.

Thanks

KD 

Best Answer chosen by Admin (Salesforce Developers) 
ShaliniShalini

Hi,

 

Try this code inside the apex class,within save function before doing an upsert

 

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>

 

 

If the field is blank, the above code will give an error meassage when you try to save a record.

All Answers

lvivaninlvivanin

Example 4: Preventing Saves on [addError() method]

 

link

 

kdmrkdmr

Hi,

I have 2 things to work on this visual force page of a custom object. There are 5 fields totaly in the object, Field1, Field2, Field3, Field4 and Field5. Field1, Field2 and Field3 are all lookup fields while Field4 and Field 5 are number fields. Field1 is the controlling field for Field2 and Field3. Meaning in the visual force page I have create a dependent Lookup functionality. I would also like to make Field4 dependent on Field3.

As soon as I select a value for Field3 i want Field4 to display the number that is part of the Field3. I do not want this to be updated after save but to be rendered immediately on selection. I would also like to have my own custom error in Apex class not a trigger or validation rule.

Thanks

KD 

ShaliniShalini

Hi,

 

Try this code inside the apex class,within save function before doing an upsert

 

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>

 

 

If the field is blank, the above code will give an error meassage when you try to save a record.

This was selected as the best answer
Brad BarrowesBrad Barrowes
Awesome!  Thank you
nishad basha 7nishad basha 7

Hi, Shalini
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.
 
nishad basha 7nishad basha 7
Hi, Carolina Ruiz Medina
             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.
 
manu manu 23manu manu 23
Hi nishad basha 7, 
To display the custom error messages directly in the visualforce page, you have a tag called <apex:pagemessage>
You can write your custom errors over there. Please look at the code below 
<apex:page standardcontroller="account">
<apex:pagemessage summary="Your page error"  severity="error" strength="1">
</apex:pagemessage>
</apex:page>

The above code has the tag called <apex:pagemessage> (Used in the vf page to display the custom error messages in vf page).
It  has three attributes
1. Summary(your custom error),
2. Severity(what kind of message is it) , in severity you have four kinds (error, confirm, info and warning) depending on the severity, the icon will be displayed
3. The last attribute is strength is the size of the icon, 1 being small and 3 being big. 

However, you been asking for the message to be popped only when the certain value is null, you need to use rendered on the message, which populates error only when the condition in the rendered attributes satisfies.  please look at the below code 

<apex:page standardcontroller="account" >
 <apex:form>
<apex:pagemessage summary="please fill the site" severity="error" strength="3" rendered="{!account.site=null}"/>
<apex:pageblock>
 <apex:pageblocksection>
 <apex:inputfield value="{!account.rating}"/>
<apex:inputfield value="{!account.industry}"/>
 <apex:inputfield value="{!account.site}"/>
 </apex:pageblocksection>
 <apex:commandbutton value="save" action="{!save}"/>
 </apex:pageblock>
</apex:form>
</apex:page>

For more info about the custom error messages in visualforce please refer to the link 
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_pageMessage.htm