[SalesForce] How to override salesforce standard object controller save method to intercept it in a visual force page (Inline Edit)

Hi there I created a visual force page to with a standard lead controller as the controller and extend it. I want to do this so i can still utilize the apex:detail control and not reinvent the wheel in dealing with the standard lead info, related list etc.

I added an apex:commandbutton and make it call save. When I click on this button I can clearly see that my function is being called. However, all changes that is done to the lead info via inline editing is not captured.

For example, If I edited LastName using the inline editing and i click on the apex:commandbutton the new LastName value is not being saved. It's almost like the save function that is being called by apex:commandbutton is not aware of the data changes.

The following is code to my visual force page.

    <apex:page standardController="Lead" extensions="LeadTestExtensionController">
    <apex:form >
        <apex:commandButton action="{!save}" value="Save" id="btnSave"/>
        <apex:detail subject="{!Lead.Id}" relatedList="true" showchatter="true" inlineEdit="true" />
    </apex:form>
    </apex:page>

the following is code to my controller

public with sharing class LeadTestExtensionController { 
    private Apexpages.StandardController controller; 
    private PageReference page; 
    private string id; 
    private final Lead myLead; 
    public String positions {get; set;}

    public LeadTestExtensionController(ApexPages.StandardController stdController) {
        this.controller = stdController;
        this.myLead = (Lead)stdController.getrecord();
        this.page = ApexPages.currentPage();
        this.id = page.getParameters().get('id');
        List<Lead> myLeads = [select Opportunity_Stage__c from lead where id = :id];
        if(myLeads.size() > 0) {
            positions = myLeads[0].Opportunity_Stage__c;
        }
    }

    public PageReference save() {
        this.controller.save();
        PageReference newPage = New PageReference('/apex/RCS');
        newPage.getParameters().put('id',ApexPages.currentPage().getParameters().get('id'));
        newPage.setRedirect(true);
        return newPage;
    }
}

Once I click on the apex:command button, the page is being redirected to apex/RCS so i know its being called. However, if i return to the same lead, the last name doesn't change. I was under the impression that the following line would've called the standard controller's save function that should've taken care of the updating of the Last Name.

this.controller.save();

What am I doing wrong and how can I accomplish this. The above code is heavily simplified version of my actual code. What I am trying to do in my actual code is to check the value of certain field and if it meets certain criteria I want it to do something. However, I can't seems to see the new value entered.

Best Answer

When you double click to inline edit, that doesn't create an input that is bound to the record in your controller via the viewstate, rather it creates a decoupled input on the fly and renders a save button that can save the record via a javascript call to sfdcPage.save().

You could attach some javascript to your Visualforce save button that locates the inline edit save button and if that exists, clicks it. You'd need to be able to detect that the save has completed and then complete your own save though, which I can only see happening by periodically checking for the inline edit save button disappearing. Also, if you were carrying out a save on the same record with other changes from your visualforce page, you'd need to retrieve the record anew from the database and merge your changes in. Add the fact that if the way that inline editing works were to change then your solution would suddenly stop working, and it doesn't look like a robust solution.

If you need the apex:detail behaviour, I'd be inclined to try to move the additional functionality based on field values to a trigger or scheduled apex.

Related Topic