I have an extension class that I want to share between tasks and events to route the user to the right page. In my contstructor, I want to determine the sObject type of the controller how do to i do that?

up vote 12 down vote accepted

try:

public class myControllerExtension {

    public myControllerExtension(ApexPages.StandardController stdController) {
        String type = stdController.getRecord().getSObjectType().getDescribe().getName();

    }
}

You can't get the object type from sObjectType as an string. Try by using getDescribe().getName();

Apex provides two data structures for sObject and field describe information:

  • Token—a lightweight, serializable reference to an sObject or a field that is validated at compile time.

  • Describe result—an object that contains all the describe properties for the sObject or field. Describe result objects are not serializable, and are validated at runtime.

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic_describe_objects_understanding.htm

  • I don't think you need that cast; getRecord() returns an SObject and getSObjectType returns SObjectType. – Mike Chale Jun 7 '13 at 17:53
  • Thx for notice that, fixed – Martin Borthiry Jun 7 '13 at 17:56
  • 2
    You can also just get it from the Id if the record exists Id.getSObjectType() – techtrekker Jun 7 '13 at 18:27

There are a couple of approaches you can use for this, one is using SObjectType, the other is using the 'instanceof' feature in Apex. Using 'instanceof' is more natural from an OOP perspective, and thus Java and C# developers will understand it more than the SObjectType approach. The first depends if you plan to do any Apex Describe type work after, thus the SObjectType is the place to start if so.

     SObjectType sObjectType = controller.getRecord().getSObjectType();
     if(sObjectType == Opportunity.sObjectType)
     {
        // This is a Opportunity record
        Opportunity opp = (Opportunity) controller.getRecord();
     }
     else if(sObjectType == Account.sObjectType)
     {
        // This is a Account record             
        Account acc = (Account) controller.getRecord();
     }


     SObject record = controller.getRecord();
     if(record instanceof Opportunity)
     {
        // This is a Opportunity record
        Opportunity opp = (Opportunity) controller.getRecord();             
     }
     else if(record instanceof Account)
     {
        // This is a Account record             
        Account acc = (Account) controller.getRecord();             
     }
  • Is there any reason to prefer one over the other? – Mike Chale Jun 7 '13 at 17:56
  • Using 'instanceof' is more natural from an OO perspective, and thus Jave and C# developers will understand it more than the SObjectType approach. The first depends if you plan to do any Apex Describe type work after, thus the SObjectType is the place to start if so. – Andrew Fawcett Jun 7 '13 at 18:00

Easy!!

public YourExtension(ApexPages.StandardController controller) {
  String sObjectType = String.valueOf(controller.getRecord().getSObjectType());
}
  • This will not compile, to make it compile, you need to use the SObjectType variable. SObjectType sObjectType = controller.getRecord().getSObjectType(); – Andrew Fawcett Jun 7 '13 at 17:57
  • getSObjectType is returning a Schema.SObjectType not a n String – Martin Borthiry Jun 7 '13 at 17:58
  • indeed, thanks for the note – Ralph Callaway Jun 7 '13 at 23:00

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.