• Home
  • About
    • Leadership
    • Partners
    • Blogroll
  • Force-Squared Blog
    • Tips and Tricks
    • Configuration
    • Development
  • Support
    • Knowledge Base
    • Submit a Case
  • Is It Dreamforce Yet?

X-Squared On Demand

Salesforce solutions delivered

  • Home
  • About
    • Leadership
    • Partners
    • Blogroll
  • Force-Squared Blog
    • Tips and Tricks
    • Configuration
    • Development
  • Support
    • Knowledge Base
    • Submit a Case
  • Is It Dreamforce Yet?
  • Tips and Tricks
  • Configuration
  • Development
You are here: Home / Salesforce CRM / Development / Apex / PageReference Best Practice

PageReference Best Practice

May 5, 2011 by David Schach 2 Comments

I've seen a lot of coders put the following into their custom Visualforce controllers:

public PageReference customsave() {
    try{ 
        insert acct; 
    } catch (DMLException e) { 
        /*do stuff here*/ 
    }
    PageReference acctPage = new PageReference ('/' + acct.id};
    acctPage.setRedirect(true);
    return acctPage;
}

I've decided that I don't like this approach. It feels too much like a URL hack, and though I'm sure that it will always work (meaning that salesforce.com will never change its way of referring to a record by /<recordID>), I'd like to suggest a different method that may use more resources, but will leverage standard controllers, possibly future-proofing the application:

public PageReference customsave() {
    insert acct; //Error handling removed for brevity. ALWAYS try/catch!
    ApexPages.StandardController sc = new ApexPages.StandardController(acct);
    PageReference acctPage = sc.view();
    acctPage.setRedirect(true);
    return acctPage;
}

What do you think? Does anyone have coding-style tips to share?

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to email a link to a friend (Opens in new window) Email

Related

Filed Under: Apex, Development, Salesforce, Visualforce Tagged With: Apex

← Content Latest Version Flag Next Birthday Formula →

Comments

  1. Jeremy Ross says

    May 5, 2011 at 09:53

    I agree it’s good to do this where you can. Unfortunately, there are many cases where the URL you need isn’t available from a controller method. In these cases, it’s probably best to have an interface responsible for handing out URLs so that at least you’ve contained URL hacking to one class.

    Salesforce’s own PS team hard codes to URLs all the time, esp for things like passing form values in the URL. If they ever decide to change their URL structure, they’re going to have a lot of unhappy customers and/or a lot of work to do.

    Reply
  2. Sebastian Wagner says

    May 5, 2011 at 23:57

    Some time ago I stumbled over Ian Zepp’s Agile Process Management (OSS) CodeShare Project (http://developer.force.com/projectpage?id=a06300000060Zd3AAE), where he made extensive use of a virtual controller class, containing common methods for visualforce, allowing him to keep his code DRY. I was so excited about that class, I decided to follow his lead, and modified it to meet our requirements.

    Today our code base is literally based on 5 abstract / virtual classes, we couldn’t live without anymore

    * CommonAbstractController
    one of the methods is saveAndView() upserts the standard controller record and redirects to the Controller view

    # Code: http://snipt.org/xtmh
    # removed all of our org specified code and included the unitTests, feel free to use

    if you are interested in source code of the classes below, let me know, I’am happy to share it.

    * CommonAbstractObject
    our core class almost every value class (SalesOrder, ServiceContractLine, …) extends, makes ’em selectable, maintains a rowPosition, generates unique Ids even for unsaved objects, allows to define dynamic child relationships ….

    * CommonAbstractContainer
    custom collection class for bulk handling CommonAbstractObjects, coulded live without it

    * LineItemAbstract
    in addition to the CommonAbstractObject LineItem clases extend it, for value rollups and other line item specific methods.

    Reply

Share Your ThoughtsCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Is it Dreamforce Yet?

Find out!

Find us on the AppExchange

Mass Update Contacts 2.0

Get It Now

Recent Posts

  • Prevent Duplicate Emails on Leads
  • Duplicate Record Item Enrichment and Auto-Deletion Code
  • Lightning Component With Running User Information
  • ChatterBINGO is Now Open-Source
  • Display Only My Role’s Records on a Report

Post Categories

Popular Tags

#df09 #df10 Akismet Apex AppBuilder AppExchange Appirio Astadia Blogs Chatter Configuration Content DreamFactory Dreamforce Eclipse IDE Enterprise Force.com Builder Force.com Platform Google Infowelders Integration Just for fun Lightning New Developments New Features Partner Program PersonAccount Projects Publisher Salesforce Salesforce1 Salesforce for Google Apps sfdcverse Sites Visualforce Web-to X-Squared On Demand

Find Us Online

  • Twitter
  • Facebook
  • LinkedIn
  • RSS

Subscribe

RSS Feed Comments

Subscribe via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 164 other subscribers

Copyright © 2008–2025 X-Squared On Demand · Genesis Framework by StudioPress · WordPress · Log in