+ Start a Discussion
Irene SlessIrene Sless 

Change current record id in URL in Visualforce page

I have 2 buttons that toggle between 2 different views (detail and list) of an object (ie all calls for a sales rep) using the sam Controller for both.

In the list view the sales rep or the date can be changed to retrieve for another rep or for another date. When I click the toggle button however to display the detail view again, it displays the original record selected, not the new one I selected in the list view, as the ID parameter in the URL doesn't change when I select a new record.

How do I get it to change the ID in the URL to the latest one, or to pick it up regardless of the URL parameter? In my commandbutton I put in the new ID as below, yet when it loads the detail view through the controller it loads the old one.
<apex:commandButton id="CCActivitiesView" action="{!view}" title="Call Cycle Activities View" value="Call Cycle Activities Detail View" 
        onclick="location.href=('/apex/CallCycleView?id={!CallCycle.Id}')" />
And the code in my Controller is as follows:
public CallCycleController(ApexPages.StandardController std) {

stdCtrl=std;        
CallCycle = (CallCycle__c)std.getRecord();
date ccDate = CallCycle.Date__c;

if(CallCycle != null && CallCycle.Id != null){
    CallCycle = [Select Id, OwnerId, Name, Date__c, CycleStart__c, CycleEnd__c, SalesPerson__c, SalesPerson__r.Name
                 From CallCycle__c Where Id = :CallCycle.Id];
    LoadModel();
}
I've been looking for an answer to this for a while and one person suggested 'move the redirection code into server side', but I don't know what's meant by that (have asked but no reply and am getting a bit more desperate now). 
Best Answer chosen by Irene Sless
Ashish DevAshish Dev
You are relying on the constructor to fetch new record. Constructor is executed only once when page gets loaded, you need to call a method of controller on button click to get record.
Another observation you have specified “Action” and “onclick” attribute on commandbutton, which is incorrect you should utilize either, preferably action , in method called through action return PageReference.
 

All Answers

Ashish DevAshish Dev
You are relying on the constructor to fetch new record. Constructor is executed only once when page gets loaded, you need to call a method of controller on button click to get record.
Another observation you have specified “Action” and “onclick” attribute on commandbutton, which is incorrect you should utilize either, preferably action , in method called through action return PageReference.
 
This was selected as the best answer
Irene SlessIrene Sless
Thanks Ashish. That's what worked. I created a PageReference and changed the button to this:
<apex:commandButton id="CCActivitiesView" action="{!GetDetailActivities}" title="Call Cycle Activities View" value="Activities Detail View" />
 
public PageReference GetDetailActivities(){
        //Toggle back to DetailView - but keep the CC selected on the ListView (if changed from original)
        
            CallCycle__c CC;
            
            if(CallCycle != null){
                CC = [Select Id, OwnerId, Name, Date__c, CycleStart__c, CycleEnd__c, SalesPerson__c
                      From CallCycle__c 
                      Where Id =: CallCycle.Id];
                
                //Set the default CallCycle to the new one
                CallCycle = CC;
                
                LoadModel();

            PageReference callCyclePage = new ApexPages.StandardController(CallCycle).view();
            callCyclePage.setRedirect(true);
            return callCyclePage;
        }