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
sgkmillssgkmills 

Displaying ApexPages.Messages on Redirected VisualForce Page via custom Controller

I created a visualforce page and a button that calls the visual force page to do some behind the scenes processing.  The processing basically clones a custom object record and its children, doing some other unique things.  The button is displayed on the standard detail record screen of the custom object and when clicked it will call the visual force page below, call it 'CLONE':

 

 

 

<apex:page standardController="custom_object__c" action="{!autoRun_Version}" extensions="custom_Controller_Ext" title="Cloning Custom object record"> <apex:sectionHeader title="Cloning of Custom object record"/> <apex:messages /> <apex:outputPanel > Click <apex:outputLink value="/{!$CurrentPage.parameters.id}"> Here </apex:outputLink> to return to the Custom object record </apex:outputPanel> </apex:page>

 

The code in the controller does all the processing and it works fine.  The issue is I want to display any error messages on the page where the button is clicked, but this is a standard page created by Salesforce and the company doesn't want to change it.  So I currently display the messages in the Visual force page, 'CLONE' above.  I would prefer to display the errors on the standard page where the button is clicked, if possible.  But, if it isn't, I want to display them on a Generic Error Page.  The error page will have the errors and then a link to go back to the standard detail page where the button is on.

 

The main part of the controller code is displayed, I didn't include everything because it would just complicate the matter:

 

 

public with sharing class custom_Controller_Ext { *** all the stuff such as constructors and private variables set above *** public PageReference autoRun_Version() { .... // *** all the stuff in autoRun_Version that wasn't pertinent has been left out *** .... if (Campaign_Inactive__c != true) { vIO(); vInv(); } else { System.debug('SKM-IO Campaign is Inactive'); PageReference pageRef = Page.ErrorPg; pageRef.getParameters().put('id', custom_object__c.id); System.debug('SKM- (INACTIVE) Page Reference =' + pageRef); pageRef.setRedirect(true); ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'ERROR: \'INACTIVE\' '); ApexPages.addMessage(myMsg); return pageRef; } } }

 

Lastly, the  visualforce page, ErrorPg, is attached.  This is my generic error page. As you see below, I have the <apex:messages /> code in the visual force page, but the message I added above int the controller doesn't get displayed.   I believe it has to do with the fact that the button exists on a standard detail page, which calls a visualforce page and then gets redirected to another visualforce page to display the message.  Is this permissable

 

BTW, I can see the error messages if I change the 'return pageRef' to 'return null'.  This basically shows the errors in the 'CLONE' visualforce page, not the generic error visualforce page.

 

 

<apex:page standardController="custom_object__c"> <apex:sectionHeader title="Generic Error Page"/> <apex:messages /> <apex:outputPanel > Click <apex:outputLink value="/{!$CurrentPage.parameters.id}"> Here </apex:outputLink> to return to the Campaign </apex:outputPanel> </apex:page>

 

 Any help will be appreciated.

 

Thanks

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

The problem here is that ApexPages.addMessage adds a message to the current page, not the page that your user will end up on.  You are setting the redirect attribute to true, which means that the client will receive a redirect to the new page.  This has the same effect as you typing in a new URL into the browser.

 

If you can pass some information about the error through the PageReference parameters, you could then define an action attribute on the final page that process the parameters and generates the error messages.

 

Returning null rather than a new PageReference simply refreshes the current page with the latest data, so that is why you would see the error messages but not on the correct page.

All Answers

bob_buzzardbob_buzzard

The problem here is that ApexPages.addMessage adds a message to the current page, not the page that your user will end up on.  You are setting the redirect attribute to true, which means that the client will receive a redirect to the new page.  This has the same effect as you typing in a new URL into the browser.

 

If you can pass some information about the error through the PageReference parameters, you could then define an action attribute on the final page that process the parameters and generates the error messages.

 

Returning null rather than a new PageReference simply refreshes the current page with the latest data, so that is why you would see the error messages but not on the correct page.

This was selected as the best answer
sgkmillssgkmills

Could you post an example?  I tried adding a parameter to the query and then on the page I read that parameter and tried to use another action parameter on the page line to run another function that would add the ApexPages.Messages and the refresh the page.  Nothing showed up.

 

 

sgkmillssgkmills

I did get this to work by making some modifications as the previous post implied.  I highlighted the main changes in red.  I made the following changes:

  1. I changed the generic visualforce error page to the following:

    <apex:page standardController="custom_object__c" action="{!errCheck}"
    extensions="custom_Controller_Ext" title="Generic Error Page">
    <apex:outputPanel id="errPanel">
    <apex:sectionHeader description="" title="Generic Error Page" subtitle="ERROR: 'INACTIVE' Campaign." rendered="{!$CurrentPage.parameters.err= 'INACTIVE'}"/>


    <h1>Click <apex:outputLink value="/{!$CurrentPage.parameters.id}"> Here </apex:outputLink> to return to the IO Campaign</h1>
    </apex:outputPanel>
    <apex:messages />
    </apex:page>

     

  2. added another function to the custom_controller_Ext with the following contents:

    public PageReference errCheck() {
    PageReference pageRef = ApexPages.CurrentPage();
    ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,
    'ERROR: \'INACTIVE\' Campaign.');
    ApexPages.addMessage(myMsg);
    return pageRef;
    }

     

  3. In the custom_Controller_Ext, under the autoRun_Version function, I added another parameter to the page with this line:

    pageRef.getParameters().put('err', 'INACTIVE');

 

Now, when the generic error page is called, the action parameter calls the 'errCheck' function, which just adds the errors to the current Apex Page.  Also, when the err parameter is set to 'INACTIVE', the error page will display the section header.  The 'Render' attribute takes care of that for me.   This allowed the <apex:messages /> to finallly display on the error page.

 

I supplied my solution so maybe someone else might need something similar to this and can learn from it.

 

'Bob_Buzzard', Thanks for pointing me in the right direction.

benwrigleybenwrigley

This works if you are redirecting the user to another VF page. What if you are redirecting them to a standard page like Opportunity?

bob_buzzardbob_buzzard

I think you'd be out of luck in that situation.  We tend to display the error to the user and ask them to click a button to be taken to the standard page.