+ Start a Discussion
EyalEyal 

How to override a standard controller action using an extension ?

Hello,

 

In order to let my user return to the same page after saving , I added an extension to the standard controller that changes the save action to return null.  This is my extension class:

public  class ExstensionControlerUserVideo {

    private final User_Video__c UserVideo;

     public ExstensionControlerUserVideo(

ApexPages.StandardSetController Controller) {
    this.UserVideo = (User_Video__c)Controller.getRecord();
    }
               
     public PageReference Save() {
           Update  UserVideo;
           return null; // redirect to same page
           }
}

However, my page still redirects the user to another page.

Do I need to change the save action tag in the visual force page also ?

Currently it is like this:

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

How do I override the standared save action ?

Thanks a lot,
Eyal
Best Answer chosen by Admin (Salesforce Developers) 
Devendra@SFDCDevendra@SFDC

 

Hi,

 

The below is the workin code: 

Apex Code:

// Extension Class for Edit Holiday 
public with sharing class EditHolidayExtension 
{
    //Global Variables
        private final Holiday__c objHoliday;
      //End of Global Variables
    
    // Standard Controller Constructor
    public EditHolidayExtension(ApexPages.StandardController controller) 
    {
         this.objHoliday=(Holiday__c)controller.getrecord();
    }
    // End of Standard Controller Constructor 

    // Method Called on to Update Holiday
     Public PageReference save()
    {
        try
        {
            update objHoliday;
        }
        catch(System.DMLException e)
        {
            return null;
        }
	Return NULL;
    }
    // End of Method Update Holiday
    }    

 

Visualforce page:

 

<apex:page standardController="Holiday__c" showHeader="true" sidebar="true" extensions="EditHolidayExtension" tabStyle="MyAdmin__tab">
<apex:sectionHeader title="{!Holiday__c.Name}">
  <apex:form >
        <apex:pageBlock mode="edit">
        <apex:pageMessages />
            <apex:pageBlockButtons >
                    <apex:commandButton action="{!save}" value=" Save "></apex:commandButton>
                 </apex:pageBlockButtons>
              
              <apex:pageBlockSection title="Holiday Detail" columns="2" collapsible="false"> 
                  
                  <apex:inputField value="{!Holiday__c.Name}" required="true" />
                 
                   </apex:pageBlockSection>
            
             </apex:pageBlock>
    </apex:form>
    </apex:sectionHeader>
</apex:page>

 return NULL will not navigate user to another page.

 

Hope this helps.

 

Thanks,

Devendra

All Answers

Devendra@SFDCDevendra@SFDC

 

Have you specified extension class in <apex:page> tag?

 

Please check for the same.

 

Thanks,

Devendra

EyalEyal

Yes,

 

I did like this:

 

<apex:page standardController="User_Video__c"  extensions="ExstensionControlerUserVideo" recordSetvar="User_Videos__c"> 
Devendra@SFDCDevendra@SFDC

 

Then it should have worked for you.

 

Try removing recordsetVar attribute from <apex:page> tag.

 

Hope this helps.

 

Thanks,

Devendra

EyalEyal

When I try to remove it and save I get the following error :

 

Error: Unknown constructor 'ExstensionControlerUserVideo.ExstensionControlerUserVideo(ApexPages.StandardController controller)'

 

Why ?

 

Thanks,

Eyal

Devendra@SFDCDevendra@SFDC

 

Hi,

 

The below is the workin code: 

Apex Code:

// Extension Class for Edit Holiday 
public with sharing class EditHolidayExtension 
{
    //Global Variables
        private final Holiday__c objHoliday;
      //End of Global Variables
    
    // Standard Controller Constructor
    public EditHolidayExtension(ApexPages.StandardController controller) 
    {
         this.objHoliday=(Holiday__c)controller.getrecord();
    }
    // End of Standard Controller Constructor 

    // Method Called on to Update Holiday
     Public PageReference save()
    {
        try
        {
            update objHoliday;
        }
        catch(System.DMLException e)
        {
            return null;
        }
	Return NULL;
    }
    // End of Method Update Holiday
    }    

 

Visualforce page:

 

<apex:page standardController="Holiday__c" showHeader="true" sidebar="true" extensions="EditHolidayExtension" tabStyle="MyAdmin__tab">
<apex:sectionHeader title="{!Holiday__c.Name}">
  <apex:form >
        <apex:pageBlock mode="edit">
        <apex:pageMessages />
            <apex:pageBlockButtons >
                    <apex:commandButton action="{!save}" value=" Save "></apex:commandButton>
                 </apex:pageBlockButtons>
              
              <apex:pageBlockSection title="Holiday Detail" columns="2" collapsible="false"> 
                  
                  <apex:inputField value="{!Holiday__c.Name}" required="true" />
                 
                   </apex:pageBlockSection>
            
             </apex:pageBlock>
    </apex:form>
    </apex:sectionHeader>
</apex:page>

 return NULL will not navigate user to another page.

 

Hope this helps.

 

Thanks,

Devendra

This was selected as the best answer
EyalEyal

Thanks a lot !!

 

it worked