[SalesForce] Displaying ApexPages.Messages on redirected VisualForce Page

I hope this is a simple problem and I'm just missing something.

My Controller is something like this

public pagereference processtempcon(){
    //some code here
    delete record;
    pagereference p = apexpages.Currentpage();
    p.setredirect(true);
    apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.Info,'Total Number of Records Processed:' + selectedtempcon.size());
    apexpages.addmessage(msg);
    return p; 
}

Which is not supported.Any Idea on this

Best Answer

So you want to reload the page an show some info in the message box. The setRedirect(true) parameter removes the info you want to show:

If set to true, a redirect is performed through a client side redirect. This type of redirect performs an HTTP GET request, and flushes the view state

I have created a simple example and it works fine:

Controller:

public with sharing class test1{
    public Integer myInt { get; set; }
    public test1(){
        myInt = 0;
    }
    public pagereference processtempcon(){
        pagereference p = apexpages.Currentpage();
        apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.Info,'Total Number of reloads: ' + (myInt++));
        apexpages.addmessage(msg);
        return p; 
    }
}

Page:

<apex:page controller="test1">
    <apex:form >
        <apex:messages/>
        <apex:commandButton action="{!processtempcon}" value="Reload"/>
    </apex:form>
</apex:page>

Result:

enter image description here

Related Topic