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
ZamieZamie 

overriding save function

 

 

Can we override the save function of any standard object like Account? If we can please suggest me some examples

Bhawani SharmaBhawani Sharma

Yes we can. In that case you need to override the new and Edit button  with a VF page and then in the extension class you can write your own save method.

 

Like :

 

<apex:page standardController="Account" extensions="MyExtension">

<apex:form>

<apex:commandButton action="{!save}" value="Save" />

</apex:form>

</apex:page>

 

public class MyExtension

{

public MyExtension(ApexPages.StandardController controller){}

 

public PageReference save()

{

System.debug('Save button overrided');

}

}

 

 

WesNolte__cWesNolte__c

Hello,

 

Mr T-Force is correct. Be careful though as using a standardController together with an extension method called "save()" can cause problems since the standardController also includes a method called "save()". If you're seeing strange behaviour rather call your save method some like "saveAccount()".

 

Wes

Bhawani SharmaBhawani Sharma

Hi Wes,

 

I don't think so that it will really happen.Once you defined a method in the extension having same name as your standard controller have . In that case this will be called from the extension class.

 

like:

 

<apex:page standardController="Account" extensions="MyExtension">

<apex:form>

<apex:commandButton action="{!save}" value="Save" />
<apex:commandButton action="{!cancel}" value="Cancel" />
</apex:form> </apex:page> public class MyExtension { public MyExtension(ApexPages.StandardController controller){} public PageReference save() { System.debug('Save button overrided'); } }

 

Now in this scenerio :

Save will be called from the class, But the cancel button will call the StandardController cancel method, as class doesn't have any cancel method.

 

 

WesNolte__cWesNolte__c

Indeed :) This was a bug for a long time but it might be resolved now - thought I'd flag it up just in case.