Identify if a Salesforce Object is Custom or Standard

Identify if a Salesforce Object is Custom or Standard
Identify if a Salesforce Object is Custom or Standard using isCustom in apex

Overview

Here you will learn how to Identify if a Salesforce Object is Custom or Standard using isCustom in apex. 

Code snippets

Here is the code to loop through all salesforce objects and check if the object is standard or custom object.

				
					Boolean isCustomObject = true;
for(Schema.SObjectType objTyp : Schema.getGlobalDescribe().Values())
{
    SObjectType objInfo = Schema.getGlobalDescribe().get(objTyp.getDescribe().getName());
    if(objInfo != null) { 
        isCustomObject = objInfo.getDescribe().isCustom();
        System.debug('Object Name =  ' +  objTyp.getDescribe().getLabel() + ' === Is Custom = ' + isCustomObject);  
    } 
}
				
			

The code given below check if specific object is Custom or Standard Object.  In my case cbugs__Student__c  is a Custom object.

				
					Boolean isCustomObject = true;
for(Schema.SObjectType objTyp : Schema.getGlobalDescribe().Values()) {
    if(objTyp.getDescribe().getName() == 'cbugs__Student__c') {
        SObjectType objInfo = Schema.getGlobalDescribe().get(objTyp.getDescribe().getName());
        if(objInfo != null) { 
            isCustomObject = objInfo.getDescribe().isCustom();        
        }
        if(isCustomObject) { 
            System.debug(objTyp.getDescribe().getName() + ' Custom object');
        } else {
             System.debug(objTyp.getDescribe().getName() + ' Standard object');
        }
    }
}
				
			

getName() used to get API NAME of Salesforce object. By using getLabel() method we can find label of any Salesforce object.

See in action - How to identify Standard & Custom Salesforce Objects

How the code works

The first snippet loops through all objects and prints whether each one is custom, using isCustom() from the object describe. The second snippet does the same check for one object, cbugs__Student__c, which is a custom object in a namespace.

Problem: getGlobalDescribe inside the loop

Both snippets call Schema.getGlobalDescribe() again for every object. That method is expensive, and calling it hundreds of times can cause a CPU time limit error. The loop already holds each object token, so the second call is not needed.

Simpler and faster

// Check one object
Boolean isCustom = Schema.getGlobalDescribe().get('cbugs__Student__c').getDescribe().isCustom();
System.debug('Is custom: ' + isCustom);

// Check all objects
for (Schema.SObjectType objTyp : Schema.getGlobalDescribe().values()) {
    Schema.DescribeSObjectResult d = objTyp.getDescribe();
    System.debug(d.getLabel() + ' | custom = ' + d.isCustom());
}

If you only need one object, use the first part and skip the loop completely.

FAQ

Is an object ending in __mdt custom?
isCustom() returns true for custom metadata types, platform events and other custom object types, not only for __c.

What if the object name is wrong?
The get call returns null and the next call fails with a null pointer error, so check for null first.