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
PathikritPathikrit 

Display trigger error message in visualforce page

Hi,

I am having an issue with displaying trigger error on a Visualforce page. In my trigger, I'm having the following piece of code to throw the error:

myCustomObjectInstance.addError('You are not allowed to do this...');

Again, on my VF page, I have added the following snippet:
<apex:pagemessages/>
The problem is that when I simulate the error condition, it does not display the error message on the VF page, but the same is visible in debug log as follows:
VF_PAGE_MESSAGE|You are not allowed to do this...


Anyone has any idea about it?

Thanks..
Best Answer chosen by Pathikrit
PathikritPathikrit
Thanks everyone for their reply. I was able to figure out the problem. For everyone's benefit here is my Save method:
public PageReference createObject()
    {
        try
        {
            if(objReference!=null)
            {
                insert objReference;
                return new PageReference('/'+objReference.id);
            }
        }
        catch(Exception e)
        {
            Apexpages.addMessage(new Apexpages.message(ApexPages.Severity.Error,e.getMessage()));
        } 
        return null;
        
    }
The problem though was in my trigger. As I mentioned in my first post that I was using addError method to throw the error, the problem occured because the addError was throwing the error at the record level. In case for the VF page to catch and display the error I needed to throw back the error at the trigger context.
It was a bad mistake i believe. Thanks again to everyone for their time.

All Answers

Abhinav GuptaAbhinav Gupta
It could be a re-rendering problem i.e. your reRender list on actionfunction/commandbutton/etc might not be including block having apex:pageMessages. You can also refer pageMessages directly as :

<apex:pageMesages id="msgs" />

<apex:commandButton action="{!save}" value="Save" rerender="otherId, msgs" />


PathikritPathikrit
Thanks for the suggestion Abhinav and sorry if I did not mention it in my first post. I'm having rerender attribute on my save button.
Here is the code snippet
<apex:pageMessages id="errorMsg"/>
<apex:pageBlock title="Create Object" mode="edit">
<apex:pageBlockButtons >
      <apex:commandButton value="Save" action="{!Save}" reRender="errorMsg"/>
</apex:pageBlockButtons>

 


Abhinav GuptaAbhinav Gupta
any redirect after save ? as redirect will clear pageMessages etc
PathikritPathikrit
On save it just inserts a new record and redirects to the record detail page.
Subramani_SFDCSubramani_SFDC
can you post ur apex class save method?
Shri RajShri Raj
This should surely display if you have this in your class. 

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'ADD YOUR MESSAGE HERE.');
ApexPages.addMessage(myMsg);


PathikritPathikrit
Thanks everyone for their reply. I was able to figure out the problem. For everyone's benefit here is my Save method:
public PageReference createObject()
    {
        try
        {
            if(objReference!=null)
            {
                insert objReference;
                return new PageReference('/'+objReference.id);
            }
        }
        catch(Exception e)
        {
            Apexpages.addMessage(new Apexpages.message(ApexPages.Severity.Error,e.getMessage()));
        } 
        return null;
        
    }
The problem though was in my trigger. As I mentioned in my first post that I was using addError method to throw the error, the problem occured because the addError was throwing the error at the record level. In case for the VF page to catch and display the error I needed to throw back the error at the trigger context.
It was a bad mistake i believe. Thanks again to everyone for their time.
This was selected as the best answer