5

So I'm trying to display an error message on a visualforce page using

ApexPages.addMessage

or

ApexPages.addMessages

As suggested in numerous threads and posts like this one: https://www.interactiveties.com/blog/2015/visualforce-apex-pagemessages.php

On my visualforce page I have

<apex:pageBlock>
    <apex:pageMessages></apex:pageMessages>
</apex:pageBlock>

but nothing is actually popping up there.

In my controller class I add a message like this:

public PageReference exchangeInv() {
  if (opp.StageName == 'Contract Executed') {

    // do some stuff

    PageReference pageRef = new PageReference('/'+opp.Id);
    return pageRef;
  }
  else {
    ApexPages.addMessages(new applicationException('error'));
    return null;
  }
}

It will get into that else case where I expect it to, and that error message will show up in the log, but will not show up inside

<apex:pageMessages>

Can anyone see where I'm going wrong here?

4
  • 1
    When is your exchangeInv() method called? – Mahmood Sep 5 '17 at 20:49
  • 1
    It's called by a button on the visualforce page the user will press once they have made their desired changes to the line item. – Adam O. Sep 5 '17 at 21:21
  • 1
    Can you replace "ApexPages.addMessages(new applicationException('error'));" with "ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'my error msg');" OR "ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, 'my error msg'));" – sunny Sep 5 '17 at 21:35
  • Ya I tried with a ApexPages.Message just like that, and that didn't display anything either :/ – Adam O. Sep 5 '17 at 22:03
6

You need to rerender pageMessages section by commandButton's attribute

<apex:pageBlock>
    <apex:pageMessages id="msgId"/>


    <apex:commandButton name="Submit" action="{!exchangeInv}" rerender ="msgId"/>
</apex:pageBlock>

Take this approach:

Since you are throwing applicationException, so better to use throw new applicationException and in the catch block, wrap that message into pageMessages

public PageReference exchangeInv() 
{
    try
    {
        if (opp.StageName == 'Contract Executed') {
        // do some stuff
        PageReference pageRef = new PageReference('/'+opp.Id);
        return pageRef;
      }
      else {
        throw new applicationException('error');
        return null;
      }
    }catch (Exception ex)
    {
        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error, ex.getMessage());
        ApexPages.addMessage(msg);
        return null;
    }  
}

or, if you don't want to use applicationException then use the error into pageMessages as follows:

public PageReference exchangeInv() 
{

    if (opp.StageName == 'Contract Executed') {
        // do some stuff
        PageReference pageRef = new PageReference('/'+opp.Id);
        return pageRef;
      }
      else {
        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error, 'error');
        ApexPages.addMessage(msg);
        return null;
      }

}
1
  • 1
    Yes! Using the render property on my command button did the trick! Thanks for the help. And I think I will restructure my code as per your suggestion and wrap my code in a try catch block, easier to follow I think. – Adam O. Sep 5 '17 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.