I have a challenging scenerio here. I am using a VF page as a view page(ViewVF) ( which has detail page tag and apex pagemessage tag in it). I have a button on this detail page which does some operation say checks whether some fields are blank or has a value. The logic behind by custom button adds two parameter to the pagereference page that I return when there is an error like below:

PageReference pr = new PageReference('/' + recd.Id);
pr.getParameters().put('severity', 'ERROR');
pr.getParameters().put('pageMessage', 'There is an error');
return pr;

In the ViewVF page I check whether there is any pagemessage or severity parameter in the URL and if yes then I display the message. Below is the controller of VF Page:

public class PageMessageController {
    public PageMessageController(ApexPages.StandardController controller) {

            String pageMessage = ApexPages.currentPage().getParameters().get('pageMessage');

        if (pageMessage != null) {
            ApexPages.addmessage(new ApexPages.Message(ApexPages.Severity.ERROR, pageMessage));

VF Page(ViewVF):

<apex:page standardController="SObject__C" extensions="PageMessageController">
    <apex:pageMessages />
    <apex:detail subject="{! SObject__c.id}"/>

The Challenge : The page error message should only be displayed when the user clicks the custom button(that invokes a VF page which checks some validations). If the user clicks edit button and save the record again then it should not display the error message. It should be the normal ViewVF page with no error message.

Flow to reproduce this:

  1. Click custom button->
  2. the validation fails in controller->
  3. Error message is added to pageMessage parameter->
  4. The error shows up in ViewVF page->
  5. User click edit and save->
  6. the error still there(no custom button was clicked)

Eventually - I want to display any error message only when the cusotm button is clicked as that is what has the validation rules and error message. I do not want to display the error message if user click any other button.

The button logic:

VF Page:

<apex:page standardController="Sobject__c" extensions="sampleControllerClass" action="{!makecallout}">


public class sampleControllerClass {

    // snipped some code

    public PageReference makeCallout()
        // Does some validations here and gives the error back and returns null so that the message is displayed on the page
        If( A validation rule){
            PageReference pr = new PageReference('/' + sr.Id);
            pr.getParameters().put('severity', 'ERROR');
            pr.getParameters().put('pageMessage', 'An error occurred');
            return pr;

            // callout code here
        }Catch(Exception e){


What I know: This is something to do with where the page return from , if it return from Custom Button then error should be displayed if it is from another source like user tried to just edit the record the error message should not be shown at that time.

  • So what exactly is your question? – EricSSH Apr 24 '17 at 19:41
  • If my page is redirect from edit page then there should not be any apex page message on it. The apex message should only be displayed on click of the custom button. Hope I have made myself clear. Currently, in the flow I mentioned I get the error message when I click edit and save. – SfdcBat Apr 24 '17 at 19:43
  • Can I use Javascript to know where it is redirect from. The previous url should help me to find where the page is getting redirect from , edit page or custom button. Makes sense? But I am figuring out how to use document.referrer; here – SfdcBat Apr 24 '17 at 19:52
  • Hi All my solution lies in using the javscript referer syntax to identify where the page is getting redirected from , use it to either display or not display the error message. – SfdcBat Apr 24 '17 at 20:41

Your Answer

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

Browse other questions tagged or ask your own question.