+ Start a Discussion
SirishaMSirishaM 

How to display exception message

Hi,

I used try and catch statements in Controller extension to catch DML errors.

How can I disply the error message ? I know that I can get the message from e.getMessage() where "e" is the exception.
But  how do I display it on the current page . I tried using ApexPages but cud not succeed.

Thanks in advance!

Sirisha
jwetzlerjwetzler
Check out this doc page

You can use ApexPages.addMessage() and then use the apex:messages or apex:pagemessages component to display them.

Keep in mind when trying to actually implement that example you will need to have a custom field (in this case NumberofLocations) that has a validation rule on it.

Also this might help, and this (last example).
mtbclimbermtbclimber
Not sure what you were doing wrong but I've added some sample code in your other thread on the same subject


GoForceGoGoForceGo
I am experimenting with this functionality and it seems to be buggy when working with Tabs.


I have an object called Declaration with a validation rule that the Unit of Measure (UOM) must be mg. UOM is a pick list field.

I have a custom controller with 3 tabs. Each tab is displaying a table of Declarations objects with UOM being one of the  columns.

Each tab has it's own "save" button. I also have a global "Save All" button which is supposed to save changes in all Tabs

An actual error in UOM is in Tab 2 on 4th row - one of the rows has a UOM as gram ( I managed to populate gram through an API call by turning off validations in the API call).

I can see this validation error if I go to the standard salesforce edit page of this object and click the save button.

Now I see bizarre behavior trying to show this error message on tab 2 on the row/column that has the validation error.

I initially have no <apex:PageMessages/>, since I want to see errors at exact row/column.

1. If I click save button on Tab 2, I don't see the error message on the exact row/column that I would like to see it in!
2. If I put a <apex:PageMessages> on Tab 2 and keep Tab 2 in focus, I see the message on top of Tab2 - whether I click "save all" or Tab 2 save button
3. If I click "Save all" to save changes in all Tabs, and have Tab 1 in focus, I see the error message against the first row on Table in Tab 1 - this row has no validation errors -  I verified in the standard detail page.
4. If remove the <apex:PageMessages> from Tab 2 and put it in Tab 3 and click "Save All" with Tab 3 in focus, I see the message on top of Tab 3.



In the code for SaveTab 1/Tab 2/Tab 3, I have following:

        try {
           update this.m_DItems[0/1/2];
        } catch (DmlException e) {
           ApexPages.addMessages(e);
        }

In code for "Save All" - I have tried both way - call the SaveTab1/Tab2/Tab3 functions and doing the following:


        try {
           update this.m_DItems[0];
           update this.m_DItems[1];
           update this.m_DItems[2];
        } catch (DmlException e) {
           ApexPages.addMessages(e);
        }



Message Edited by GoForceGo on 06-26-2008 10:20 AM
GoForceGoGoForceGo
When I work with just one table (just on one tab), I seem to have similar issues:

If I have data errors in row 1 for UOM field and errors on  row 2 of the table (on other field weight),  the system shows errors only against row 1 of the table.

If I fix my error in row 1 without fixing the error in row 2 and click save, it still show me an error in row 1.

If I fix the error in row 2, it error disappears!




GoForceGoGoForceGo
If there was a way of displaying the error messages at exact row/column, I would appreciate a post by 2:30pm this afternoon - I have a prospect demo where it would be very helpful.

Before summer 08, I wrote some custom code to create a string with all the errors and show it at the top with row numbers included. Works but not elegent.





rohit.mehtarohit.mehta
Hi,

Were you able to resolve this issue?
I am having the same issue. When I have my validation error on the 2nd or 3rd row...and on, the first row also shows the error message.

Thanks,
Rohit

mtbclimbermtbclimber
This is a known issue that will be addressed in a forthcoming patch.