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
BoolsEyeBoolsEye 

apex:detail: ID is missing after 'Edit'

I have a weird behaviour when adding apex:detail and apex:commandButton on different tabs on a tabpanel.
 
In my code when I click on the commandButton on Tab2 the page gets refreshed.
 
Now I click on the standard 'Edit' button for the contact on Tab1, click on 'Cancel' and the Contact is gone.
 
It seems as if the ID is lost when the edit page is redirected back to my VF page.

It works if I do not click on the commandButton on Tab2 before using the edit button.
 
Is there any workaround for this or am I doing something wrong ?
 
Thanks.
 
 
Code:
<apex:page standardcontroller="Contact" extensions="MyContactControllerExt">
 <apex:tabpanel >
  <apex:tab label="Tab1">
   <apex:detail subject="{!Contact.Id}"/>
  </apex:tab>
  <apex:tab label="Tab2">
   <apex:form >
   <apex:commandButton value="Go" action="{!doSomething}"/>
   </apex:form>
  </apex:tab>
 </apex:tabpanel>
</apex:page>

 
Code:
public class MyContactControllerExt{

    private Contact c;
    
    public MyContactControllerExt(ApexPages.StandardController controller) {
        c = (Contact)controller.getRecord();
    }

    public PageReference doSomething() {
        return null;
    }
    
    public Contact getContact() {
        return c;
    }
}

 
BoolsEyeBoolsEye

I found a solution in this thread.

You have to override the 'Edit' button with a second page.

I hope there will be a more elegant way in the future release.

Code:
<apex:page standardController="Contact" extensions="ContactEditController" action="{!redir}"/>
Code:
public class ContactEditController {

    private final ApexPages.StandardController controller;
    
    public ContactEditController(ApexPages.StandardController controller) {
            this.controller = controller;    
    }
    
    public PageReference redir() {
            Contact c = (Contact)controller.getRecord();
            PageReference newPage = new PageReference('/' + c.id + '/e');
            newPage.getParameters().put('nooverride', '1');        
            newPage.getParameters().put('retURL','%2F' + c.Id);
            return newPage.setRedirect(true);    
    }     
}