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