9

How to write SOQL query to retrieve all the object Name and API names of Sobject in Salesforce

  • 2
    Why do you need to have this by soql ? Have you already looked into schema describe ? – Samuel De Rycke Nov 26 '15 at 13:43
21

You cannot do it with SOQL.

You need describe call

for ( Schema.SObjectType o : Schema.getGlobalDescribe().values() )
{
    Schema.DescribeSObjectResult objResult = o.getDescribe();
    system.debug( 'Sobject: ' + objResult );
    system.debug( 'Sobject API Name: ' + objResult.getName() );
    system.debug( 'Sobject Label Name: ' + objResult.getLabel() );   
}

Run this code in Execute anonymous and check debug. You will get all the sobject name(API Name), label and other properties.

| improve this answer | |
  • Is there a way to determine which are custom objects vs standard objects? thanks – Andy Hitchings Feb 7 '17 at 11:50
  • 1
    @AndyHitchings you can check if the api name ends with __c or not to identify the custom and standard objects. – Ratan Paul Feb 8 '17 at 5:38
15

Here's a method I've found. It's more explicit than using the permissions object.

Fields

SELECT  QualifiedApiName FROM FieldDefinition WHERE EntityDefinition.QualifiedApiName = 'Account' 

Objects

SELECT  QualifiedApiName FROM EntityDefinition order by QualifiedApiName 
| improve this answer | |
  • I was searching for this for a very long time. Thank you so much it helped me to solve my issue. – Varatharaj Jan 4 '19 at 11:19
  • this approach appears to be limited to 200 results as EntityDefinition thows a EXCEEDED_ID_LIMIT error if run without adding LIMIT 200 – gorav Jan 7 '19 at 19:21
4
String sObjectName = 'Project__c';
System.debug('=========  '+ sObjectName);
List<Schema.SObjectField> sss=new list<Schema.SObjectField>();

if(sObjectName != null && sObjectName != '' )

Schema.SObjectType gd = Schema.getGlobalDescribe().get(sObjectName); 
Schema.DescribeSobjectResult a11=gd.getDescribe();
Map<String, Schema.SObjectField> M = a11.fields.getMap();        
for(Schema.SObjectField s1:m.values()){                            
    Schema.DescribeFieldResult f = M.get(s1.getDescribe().getName()).getDescribe();
}

Thanks !!!

| improve this answer | |
  • 2
    Thanks for providing an answer. You might want to add some information to the question asker describing how this solves his/her problem? – Guy Clairbois Nov 26 '15 at 10:46
2

I haven't compared the list to the describe list but you can find a list of presumably all SObjects within the ObjectPermissions table listed under the SobjectType field. This field is weird though and doesn't allow some filtering (no LIKE operator for example). There is also a possibility the list is generated only from SObjects that are not public read/write, but I haven't checked to see if that is the case.

The proper way is definitely the describe, but this table might provide some insight as well in a quick query. I wouldn't use this route in production code though because of those uncertainties.

SELECT SobjectType FROM ObjectPermissions
| improve this answer | |
1

The Best Ways to retrieve list of all Objects using Apex, you can use Schema.getGlobalDescribe() method.

Its simple use below code:

for( Schema.SObjectType o: Schema.getGlobalDescribe().values())
    System.debug(o.getDescribe().getName());

For more details check : The Best Ways to Retrieve Names of All Objects Using Apex

| improve this answer | |
0
for ( Schema.SObjectType obj : Schema.getGlobalDescribe().values() )
{
    Schema.DescribeSObjectResult objResult = obj.getDescribe();
    system.debug( 'Sobject: ' + objResult );
    system.debug( 'Sobject API Name: ' + objResult.getName() );
    system.debug( 'Sobject Label Name: ' + objResult.getLabel() );   
}
| improve this answer | |
  • How does this provide more information than the answers already given ? – Samuel De Rycke Dec 8 '15 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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