Find your content:

Search form

You are here

Visualforce - Overriding "Save" button behavior

 
Share

Currently in my VF Page (inline editing support enabled) after clicking "Save" button, I am being taken to the default detail page layout.

I want "Save" operation to be executed upon clicking the "Save" button but make the user stay on the same page.

Can someone tell what should I do ?

My Code :

<apex:page standardcontroller="Expense__c" sidebar="false" showHeader="true" showChat="false"  >
<apex:form >

<apex:inlineEditSupport />
<apex:commandButton action="{!save}" value="Save" id="theButton"/>
<apex:pageBlock title="List of Expenses">
<apex:pageBlockTable value="{!Expense__c}" var="item" >
<apex:column value="{!item.Date__c}"/>

<apex:column value="{!item.Type__c}"/>
<apex:column value="{!item.Amount__c}"/>
<apex:column value="{!item.Comments__c}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

Attribution to: Varun

Possible Suggestion/Solution #1

It looks like your page invokes the standard controller's default save method that returns the default detail page URL. To change that URL you can write your own save method that first invokes the standard controller's save to do most of the work and then returns a page URL of your choosing:

public with sharing class ExpenseController {
    private ApexPages.StandardController sc;
    public ExpenseController(ApexPages.StandardController sc) {
        this.sc = sc;
    }
    public PageReference save() {
        PageReference detailPage = sc.save();
        if (detailPage != null) {
            // Construct URL of edit page or whatever other page you want
            PageReference editPage = new PageReference(detailPage.getUrl() + '/e');
            return editPage;
        } else {
            return detailPage;
        }
    }
}

Include the name of the controller extension in the page:

<apex:page standardcontroller="Expense__c" extensions="ExpenseController" ...

Attribution to: Keith C
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/32593

My Block Status

My Block Content