+ Start a Discussion
shan141shan141 

PageReference & Action Class

what is the advantage of Action class over PageReference class. PageReference class provides you the functionality what Action class provides and even more. So when we go for Action class and PageReference class. Please provide some practical explanation.

bob_buzzardbob_buzzard

An action class is a different proposition to a page reference class.  

 

Action allows you to create custom action methods for use in pages/controllers, while pagereference is, as the name implies, a reference to a page with all the associated attributes (URL, parameters etc).

 

I've only needed to use Action classes when passing action methods into components.  I can't see that you would use these when you wanted something page specific.

shan141shan141

Hi,

 

If we wants to insert or update a record from VF , we can do that with Pagereference class or Action class. In this case how to differentiate both the classes.

 

Below I am specifying to types of methods for inserting record, one is using Action class and other without Action class.

Please let me know when we use these two types.

 

First type================================================================

public PageReference save() {

      // Add the account to the database.  
   
      insert account;

        }

 

Second type=================================================================

ApexPages.Action saveAction = new ApexPages.Action('{!Save}');

 

public void Save() {
    try{ insert acct;
    }
    catch(DmlException ex){ApexPages.addMessages(ex);
    }
  }
====================================================================

 

Thanks

Shan

 

bob_buzzardbob_buzzard

Both of these are action methods. The first one returns a PageReference, it doesn't use the PageReference to handle any part of the saving.  The void return type of the second method tells salesforce to simply redraw the current page after the action has taken place.

 

Use of the ApexPages.Action class in the second example appears to be allowing you to alias the action method to a different name for use on the page. 

 

I always go the first route - an action method returning a PageReference.  I'm sure there are use cases for the second, but I've not had to go that way yet.