Find your content:

Search form

You are here

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

 
Share

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.


Attribution to: Prafulla Patil

Possible Suggestion/Solution #1

Displaying the <apex:detail> component changes the way the Visualforce page interacts with the form elements on the page and the controller.

You will have an easier overriding the standard controller action by removing <apex:detail> and using Visualforce form elements instead:

<apex:page standardController="Lead" extensions="LeadTestExtensionController">
    <apex:form >
        <apex:commandButton action="{!save}" value="Save" id="btnSave"/>
           <apex:inputField value="{!Lead.Name}" />
           <apex:inputField value="{!Lead.Email}" />
           ...
    </apex:form>
    </apex:page>

Attribution to: jordan.baucke

Possible Suggestion/Solution #2

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.


Attribution to: Bob Buzzard

Possible Suggestion/Solution #3

Try using saveURL.

I have somewhat of a similar requirement. In my case it's to redirect the user to a page after save is clicked (either inline edit.. forgot about that until I read this post) or save on the edit page. My solution is to utilize the saveURL to redirect the user after a save is performed. Basically I created a custom edit button that adds the saveURL onto the edit page and then when the VF page is called, I do some logic to redirect the user to an external page based on criteria of the record. It's convoluted, but worth checking out.


Attribution to: James Loghry

Possible Suggestion/Solution #4

If you're trying to do a redirect post-save I've had success using an inline visualforce page that changes url with javascript on load.

<apex:page standardController="Account">
  <apex:outputPanel rendered="{!account.do_redirect__c}">
    <script>
      window.top.location.href = "/apex/my_redirect?id={!account.id}";
    </script>
  </apex:outputPanel>
</apex:page>

You can have a trigger determine if the redirect is needed and set your flag. If you only want to do the redirect once, you can puts some logic in the redirect page to uncheck the flag so the next time a user comes back to it they don't get redirected again.


Attribution to: Ralph Callaway
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/3946

My Block Status

My Block Content