Quick Tip – Getting Prefix of sObject


I had a requirement today to provide two buttons on a Visualforce page which redirected the user to two different list views of sObjects.  This solution also had to go through a number of org migrations so I knew that the sObject prefix would be changing.  Problem?  Nope…getDescribe to the rescue.

Quick and dirty schema query:

public PageReference manageSObjectOne() {

Schema.DescribeSObjectResult dsr = <<API Name of your sObject>>.SObjectType.getDescribe();

PageReference prRef = new PageReference(‘/’ + dsr.getKeyPrefix());
prRef.setRedirect(true);
return prRef;

}

No SOQL hit, nothing except for script statements counting against your governor limits.  Slick.

Advertisements

5 thoughts on “Quick Tip – Getting Prefix of sObject

  1. Pingback: Force.com Design Patterns: Retrieve the Prefix of an sObject Using Describe Methods | Delivered Innovation Blog

  2. Thanks Andrew. While I have a long history of technology (like you) and have strong SFDC admin experience, I am new to SFDC development. What book do you recommend that I read so that I can understand what you created and learn to create it myself?

    • Hello Eric!

      Dan Appleman has authored an amazing book called “Advanced Apex Programming”, which I couldn’t recommend higher. It has great examples and methodologies on APEX / Visualforce Development.

      For jQuery and other web technologies, the best advice I can give you is to just start trying the demos out. (jqueryui.com)

      Good luck!

      -Andy

      • …in follow up…Dan’s book is titled “Advanced Apex Programming” – is this where an Apex newbie should start? Again, I have SFDC admin experience but not much in the development side of things. Thanks for your further clarification.

        Best,

        Eric

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s