+ Start a Discussion
EyalEyal 

Extension Controller for a custom object

Hello,

 

I built a visualforce page which shows a view list on one of my custom objects called User_Videos__c

 

 I would like to change the "save" action of the standard controller  so the user will be redirected to the same page.

 

I heard that I need to write a new extension for the standard controller , so after saving it will return null.

 

This is my extension class:

 

public  class ExstensionControlerUserVideo {

    private final User_Video__c UserVideo;

        public ExstensionControlerUserVideo(ApexPages.StandardController Controller) {
        this.UserVideo = (User_Video__c)stdController.getRecord();
        }
        
     private ApexPages.StandardController stdController{get; set;}    
        
     public PageReference Save() {
           Update  UserVideo;
           return null; // redirect to same page
           }
}

 

 

When I had it to the page like this:

 

<apex:page standardController="User_Video__c"  extensions="ExstensionControlerUserVideo" recordSetvar="User_Videos__c">
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages />
            <apex:pageBlock >  .....

 

I get the following error:

 

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

 

What am I doing wrong ? How should my code be?

 

Do I need to do a custom controller ? ( I tried but than I get an error that I am using functions that don't exists in my custom controller .. )

 

Please help

Eyal

Starz26Starz26

Change:

 

public ExstensionControlerUserVideo(ApexPages.StandardController Controller) {
        this.UserVideo = (User_Video__c)stdController.getRecord();
        }
        
     private ApexPages.StandardController stdController{get; set;} 

 

TO

 

public ExstensionControlerUserVideo(ApexPages.StandardSetController Controller) {
        this.UserVideo = (User_Video__c)Controller.getRecord();
        }
        

//The following line may not be needed depending on what you are doing in your class

     private ApexPages.StandardSetController stdController{get; set;} 

 

 

Since you are using a RecordSetVar you are using a Set controller and not just the object controller. Also, the Controller holds the records for the page, not the stdController declared later. The use for your stdController would be to Set it = to the controller in your constructor to reference later if needed.

EyalEyal

Thank you !!

 

Now it works but the "save" action is still taken from the standard controller and not from the extension.

 

How can I use the extension one ?

 

My code is :

 

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

 

Thanks a lot,

Eyal

vineet_mvineet_m

Hi Elad,

 

There are some reserved keywords in Salesforce and Save method is one of them. So, In order to save the page from your extension, you need to rename the word  "save" to anything else say "saveme or save123" and then try saving the content.

 

i guess it would help !!

 

thanks

vineet

 

Starz26Starz26

@vineet

 

Your understanding of methods for extensions and Reserved Keywords appears to be incorrect...

 

You can overload the Save, Edit, Delete functions that are provided by default by salesforce.

 

If you have an extension, placing a method in the extension: Public void Save()

will ensure that salesforce uses that method for save and not the standard method. You can do the same with a controller, however salesforce will not fall back to the default Save method if it is not found in a custom controller

 

 

Starz26Starz26

@elad

 

Do you actually have a Save method in the extension? If so please post it here for review

EyalEyal

Hello,

 

This is my final Exstension class :

 

public  class ExstensionControlerUserVideo {

    //Global Variables    
    private final User_Video__c UserVideo;
   //End of Global Variables
     
    // Standard Controller Constructor  
     public ExstensionControlerUserVideo(ApexPages.StandardSetController Controller) {
    this.UserVideo = (User_Video__c)Controller.getRecord();
    }
    // End of Standard Controller Constructor
    
    
    // Method Called on to Update User Video            
     public PageReference save() {
        try
            {
            update UserVideo;
            }
        catch(System.DMLException e)
            {
            return null;
            }
    Return NULL;
    }
     // Method Called on to Update User Video

}

Starz26Starz26

I would say that instead of returning Null for the catch block, do a system.debug for the error.

 

I would be willing to bet that you are generating an error and that is mimicking the effect of not calling the function since all you do is return null.

 

Also, You can put a debug statement at the start of the method like system.debug('IN THE SVAE METHOD') if you would like to ensure that you are there...