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
bozotheclownbozotheclown 

PageRefere​nce / setredirec​t Confusion

Hello.  I was wondering if anyone could give me some guidance with the below.

 

A commandLink invokes the below controller code.  The goal with the below code is to advance to a new page (MyPage)...and populate this page with a specific record's data (based on the value of the below 'myRecordId').

 

In addition to populating the page with the data from the 'myRecordId' record, I also want to add a message to the top of the page (based on the value of a variable computed by the controller).

 

I have been able to easily do this on other pages.  However, this time, my problem is that I cannot get my message to render on the page.  While I usually simply add my code immediately below the "public PageReference GoToPage" line, I think my use of the 'setredirect' code is causing some problems.

 

Thanks in advance for any guidance.  I will be glad to explain further if this does not make complete sense.

 

public PageReference GoToPage() {
PageReference aaaa = new PageReference('/apex/MyPage?id=' + myRecordId);
aaaa.setredirect(true);
// The below code is intended to set a variable 'statusmessage' based on the 'salestotal' value.
// Unfortunately, the 'statusmessage' is not rendering on the page
// I think the 'setredirect' code is somehow preventing the below to exexute properly.
if(salestotal>3000)
{
statusmessage = 'sales today are acceptable';
}
ELSE
{
statusmessage = 'sales today are unacceptable';
}
return aaaa;
}

 

Best Answer chosen by Admin (Salesforce Developers) 
NzgonNzgon

If you are using same controller make in your constructor for controller if sttement that will not override values that you are declaring on first page already when it load on second page.

 

Ex:

 

public string myString {get;set;}

public My_Controller (){

if (myString!=null){

myString='default value';

}

}

 

On this way you will have default value on first page, you can update it and use on second page.

Hoppe this will help.

Nash

 

All Answers

NzgonNzgon

You need to use same APEX controller (class) on pages in order to preserve values and pass it from one page to another.

 

Another way is to compose Url parameters and pass it in call to another page.

 

Nash

bozotheclownbozotheclown
Thanks for the response. Actually, I definitely am using the same controller between these two pages. That is why I am so confused.

I have not used setredirect very much - so I was not sure if that command was creating the problem (and how I should work around it).

Thanks
NzgonNzgon

If you are using same controller make in your constructor for controller if sttement that will not override values that you are declaring on first page already when it load on second page.

 

Ex:

 

public string myString {get;set;}

public My_Controller (){

if (myString!=null){

myString='default value';

}

}

 

On this way you will have default value on first page, you can update it and use on second page.

Hoppe this will help.

Nash

 

This was selected as the best answer
bozotheclownbozotheclown
Thanks a lot. That makes sense.