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
vyshvysh 

write apex classes in visual force page

Hi all,

 

I am  very new to Salesforce.  I have created visual force pages with the help of documents and have written apex classes in standard objects.  Now I am trying to write apex classes in visual force pages.  For example in my visual force page I have a standard 'Save' button which functions normally. If I have to override this normal functionality, how do I do this? Can anyone guide me.

Best Answer chosen by Admin (Salesforce Developers) 
mhamberg1mhamberg1

I figured it out with a little help from the Apex manual:

 

Here's what my extension looks like:

 

public class MAPP_Idea_Extension { MAPP_Idea__c idea; // The extension constructor initializes the private member // variable idea by using the getRecord method from the standard controller. public MAPP_Idea_Extension(ApexPages.StandardController stdController) { this.idea = (MAPP_Idea__c)stdController.getRecord(); } public MAPP_Idea__c getMAPP_Idea() { if(idea == null) idea = new MAPP_Idea__c(); return idea; } public Pagereference save() { //save insert idea; //redirect to the thank you page PageReference thankyou = new PageReference('/apex/MAPP_thankyou'); thankyou.setRedirect(true); return thankyou; } }

 

 Then make sure you include the extension on your visualforce page.

 

All Answers

AlsoDougAlsoDoug

Vysh not sure I am 100% on what your doing with out a code sample.

 

Do you have a Visual Force page with a custom controller and you want to save or do you have a Visual Force page with a standardController and you want to save?

 

If you're using a standard controller on your page and you don't have any special save logic you should be able to just call save on your button.

 

IE

 

 

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

 

If you have a custom controller you would need to write your own save method (or what ever you want to call it ).

 

I guess the third option you would have would be using an extension for a custom controller. In that case if you have custom save logic you need to do you can do the following in the Apex code:

 

 

public Pagereference save() { //your custom logic this.standardController.save(); //this is my logic for where I want to go after stuff is saved PageReference nextPage = new PageReference('/apex/nextPage'); nextPage .setRedirect(true); return searchResultsPage; }

 

 You will need to have declared a StandardController in the class that is the extension and have set it up.

 

 

 

 

mhamberg1mhamberg1

Can you explain "You will need to have declared a StandardController in the class that is the extension and have set it up?"

 

I'm trying to do this exact thing where the user saves something and then gets redirected on to another page.

 

Also, in your code, shouldn't the line be "return nextPage;"  ?

mhamberg1mhamberg1

I figured it out with a little help from the Apex manual:

 

Here's what my extension looks like:

 

public class MAPP_Idea_Extension { MAPP_Idea__c idea; // The extension constructor initializes the private member // variable idea by using the getRecord method from the standard controller. public MAPP_Idea_Extension(ApexPages.StandardController stdController) { this.idea = (MAPP_Idea__c)stdController.getRecord(); } public MAPP_Idea__c getMAPP_Idea() { if(idea == null) idea = new MAPP_Idea__c(); return idea; } public Pagereference save() { //save insert idea; //redirect to the thank you page PageReference thankyou = new PageReference('/apex/MAPP_thankyou'); thankyou.setRedirect(true); return thankyou; } }

 

 Then make sure you include the extension on your visualforce page.

 

This was selected as the best answer