6

I am curious to know, is there any way exists from where we can know that a particular id is of type VF/class/trigger/components etc.

The way I think/know currently is, to query with "ApexPage" to check whether it is a page, "ApexClass" to check for class.

Is there any single/more prominent way to know this?

Thanks in advance.!

2

5 Answers 5

4

You can use Id.getSObjectType() to know what type an Id is.

Id someId = ...;
System.debug(someId.getSObjectType());

You can refer to it literally:

if(someId.getSobjectType() == ApexClass.sObjectType) {
    // is a apex class

Or you check as a string:

if(String.someValueOf(someId.getSObjectType()) == 'ApexClass') {
    // is an apex class
2

You can get to know it from the first three digit key prefix whether its a Apex Class, VF Page or Trigger

Apex Class - 01p
VF Page - 066
Apex Trigger - 01q
1
  • Does these prefix are common across all salesforce orgs? For my org as well, there are same.
    – Ysr Shk
    Nov 23, 2016 at 6:48
2

There is a better way to do this such that it is dynamic i.e. works for any component and is reusable. The reusable apex method is available as an official salesforce knowledge article -- How to find Object Type from Record ID Prefix

Also, it is highly recommended to go through the apex doc for the following methods and understand their return types:

1
  • Searching the entire global describe can easily consume several seconds of CPU time, though.
    – sfdcfox
    Nov 23, 2016 at 6:47
0

You can find out the Id type in the following way:

Account a = new Account(Name = 'account');
insert a;
Id myId = a.id;
System.assertEquals(Schema.Account.SObjectType, myId.getSobjectType());

This would work for all sObject Types It would return Class as ApexClass, VF Page as ApexPage etc.

0

Small code snippet of how to get all object prefixes in the org.

 for ( Schema.SObjectType ot : (Schema.SObjectType[])Schema.getGlobalDescribe().values() ) {
        Schema.DescribeSObjectResult dor = ot.getDescribe();
        System.debug('--->>> Prefix: ' + dor.getPrefix() + '; Object: ' + dor.getName() + ';');
    }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .