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
uHaveOptionsuHaveOptions 

Visualforce Save Button redirects inside another detailed page after save

I have a VF page in a custom object that has a save button inside a tabpanel and the VF page has a InlineEditSupport.  Everytime I update a field and save it, it updates but loads another Detail Page inside my VF page.  Any work around this?

 
<apex:page standardController="Property__c" showHeader="true" sidebar="false">
    <!-- Define Tab panel .css styles -->
    <style>
    .activeTab {background-color: #081f3f; color:white; background-image:none}
    .inactiveTab { background-color: lightgrey; color:black; background-image:none}
    </style>

    <!-- Create Tab panel -->
    <apex:tabPanel switchType="client" selectedTab="Property__c" id="AccountTabPanel"
        tabClass="activeTab" inactiveTabClass="inactiveTab">
        <apex:tab label="Property Information" name="name1" id="Property">
         <apex:form > 
         <apex:pageBlock >
         <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" id="saveButton" value="Save"/>
            </apex:pageBlockButtons>

           <apex:pageBlockSection columns="2">
                <apex:pageBlockSectionItem >
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Property Name" for="Property__c.Name" style="font-weight:bold"/>
                    <apex:outputField value="{!Property__c.Name}" id="Property"/>     
                </apex:pageBlockSectionItem>
​<apex:inlineEditSupport event="ondblClick" 
                        showOnEdit="saveButton,cancelButton" hideOnEdit="editButton" />
           </apex:pageBlocksection>           
        </apex:pageBlock>
        </apex:form>

       </apex:tab>
    </apex:tabPanel>
</apex:page>

 
Best Answer chosen by uHaveOptions
Krishna SambarajuKrishna Sambaraju
You can create an extension to standard controller and override the save method and return null after the update. Then it will remain on the same page instead of redirecting to the standard detail page.
 
public class PropertyExtensionController{
    Property__c property;
    
	public PropertyExtensionController(ApexPages.StandardController controller)
    {
        property = (Property__c)controller.getRecord();
    }
    
    public PageReference save()
    {
        update property;
        return null;
    }
}
And in your visualforce page reference this extension as below

<apex:page standardController="Property__c" showHeader="true" sidebar="false" extensions="PropertyExtensionController">

Hope this helps.
 

All Answers

srlawr uksrlawr uk
Your save button is just using the standardcontroller action "save" which after completion returns a pageReference to the detail view of the object just updated.

I would say the easiest way to interupt this behaviour is to stick a reRender on the button. When the button has a reRender - it just redraws a specific part of the page instead of returning the full pageReference (I hope!) change you save button to be
 
<apex:commandButton action="{!save}" id="saveButton" value="Save" rerender="property" />

this should cause it to just reload the property tab with the new data, but not do any redirecting.


(thank you for being a good developer and putting IDs on all your elements!)
Krishna SambarajuKrishna Sambaraju
You can create an extension to standard controller and override the save method and return null after the update. Then it will remain on the same page instead of redirecting to the standard detail page.
 
public class PropertyExtensionController{
    Property__c property;
    
	public PropertyExtensionController(ApexPages.StandardController controller)
    {
        property = (Property__c)controller.getRecord();
    }
    
    public PageReference save()
    {
        update property;
        return null;
    }
}
And in your visualforce page reference this extension as below

<apex:page standardController="Property__c" showHeader="true" sidebar="false" extensions="PropertyExtensionController">

Hope this helps.
 
This was selected as the best answer
srlawr uksrlawr uk
that solution definitely would also work, but requires a whole Apex controller, and thus test coverage etc.etc. as well! 

Heavy lifting for such a simple problem!
uHaveOptionsuHaveOptions
Hello srlawr uk -  that solution didnt work. But I did learn something on rerender

Hello Krishna Sambaraju - that solution worked well but anyway that it will also reload the whole entire page?

Thanks you guys