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
vanessa veronvanessa veron 

ApexPages.Message NOT WORK

Hii
My error messages do not work.
I have a method within this method and use the code below:

APEX CONTROLLER:
global PageReference schedulejob(){
..............................
if (nomJob == null){
    ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'job null');
    ApexPages.addMessage(myMsg);
    }
    else

        system.schedule(NomJobSchedulable , expression, p);
..............

PageReference pageRef = new PageReference('/apex/BBBBPage');
        pageRef.setRedirect(true);
        return pageRef ;
}

Inside the Visualforce page, the button will execute this method that contains the error message, but does not work. why?

..........
<apex:PageBlock id="block01">
  <apex:pagemessages id="showmsg" />

.............

Nom Job.:&nbsp;<apex:inputText id="nomJobIP" value="{!nomJob}" />

...................

 <apex:commandButton value="Create" action="{!schedulejob}"/>



Vishant ShahVishant Shah
add return null;  after ApexPages.addMessage(myMsg);
bob_buzzardbob_buzzard
The problem here is that after adding the messages, you do a client side redirect to another page - this will discard the state of the curent page, including any messages that you have added.  You'll need to add the messages to the URL in this case, and change your target page code to pull the messages from the URL.
Vishant ShahVishant Shah
Hi Bob,

wouldnt my above suggestion stop the page redirect as we doing a redirect after setting the page message

Vish
Vishant ShahVishant Shah
sorry meant doing a return not redirect
bob_buzzardbob_buzzard
It might - it depends on what the OP was looking to do. Just telling someone to change their code to work in a different way without explaining the underlying problem won't always help them. If the OP absolutely needs the user on the target page, then your solution would show the message but potentially break the app.  Quality of answer is always better than quantity.
Vishant ShahVishant Shah
Agreed, Thanks Bob. Will keep this in mind with my answers

Vish
vanessa veronvanessa veron
Bob I really need after clicking the button, move to a new page ...
How should I make the error message work?

Thank you
vanessa veronvanessa veron
Please, give me an example!
bob_buzzardbob_buzzard
I haven't got an example of exactly what you are trying to do, but the following blog post:

http://bobbuzzard.blogspot.co.uk/2011/06/execute-custom-search-when-opening-page.html

shows how to extract parameters from the URL and use them in the controller.
vanessa veronvanessa veron
Thank you!!!!