+ 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.