Identify component type from ID
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
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
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:
- Schema.getGlobalDescribe
- SObjectType.getDescribe()
- Schema.DescribeSObjectResult.getKeyPrefix()
- Schema.DescribeSObjectResult.getName()