11

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

2
  • 2
    Why do you need to have this by soql ? Have you already looked into schema describe ? Nov 26, 2015 at 13:43
  • Maybe because SOQL queries can be executed in the Developer Console, while APEX seems to require a sandbox to be set up, not to mention that you are stuck building a page and a class to get something so much more readily available via SOQL query. That's why I landed on this gold mine page, anyway. Jan 29, 2021 at 9:32

9 Answers 9

27

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 
4
  • I was searching for this for a very long time. Thank you so much it helped me to solve my issue.
    – Varatharaj
    Jan 4, 2019 at 11:19
  • 1
    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, 2019 at 19:21
  • @Omkar More offered a solution to this. Scroll down. Jan 29, 2021 at 9:33
  • I was easily able to return 700 results.
    – Jake
    Nov 16, 2021 at 3:40
25

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.

2
  • 1
    Is there a way to determine which are custom objects vs standard objects? thanks Feb 7, 2017 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, 2017 at 5:38
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 !!!

1
  • 3
    Thanks for providing an answer. You might want to add some information to the question asker describing how this solves his/her problem? Nov 26, 2015 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
1
  • 1
    Though Describe may be the officially sanctioned way to get the information, the SOQL way suggested by @oleg is a lot less work. Jan 29, 2021 at 9:28
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

1

To get list of All Objects run below query from workbench:

SELECT QualifiedApiName FROM EntityDefinition ORDER BY QualifiedApiName LIMIT 200 OFFSET 200

Please know that there is a limit of max 200 records will be returned in list per retrieval. Hence add 'LIMIT 200' to restrict the result for first 200 records. Otherwise you will get below error

"EXCEEDED_ID_LIMIT: EntityDefinition does not support queryMore(), use LIMIT to restrict the results to a single batch"

If you need to get next results from record no 201 , 202, ... add 'OFFSET 200' to your query. this will show records from 201 to 400. Sililarly use 'OFFSET 400' for records from 401 to 600 and so on.

2
  • The explanation of OFFSET opens the door to querying more than 200 objects. This is also hugely useful when you really need it. Jan 29, 2021 at 9:27
  • It should be noted that the SOQL OFFSET clause has a maximum offset value of 2000, limiting the total number of record you can retrieve in this fashion to 2200. You probably don't have 2200 SObjects in your org (but with enough managed packages, you could) - but for other uses of this solution, it is important to keep in mind. Feb 21, 2021 at 18:20
0

Using the list of fields names at FieldDefinition Fields and the basic query given by @oleg, I got exactly what nobody seems to have figured out how to get, easily, from a developer console window. In my case, I needed a list of all the fields on our Lead object, along with their key attributes (Type, Length, Precision, Label, etc.).

My SOQL query is as follows.

SELECT QualifiedApiName, Label, DeveloperName, Description, DataType, ExtraTypeInfo,  Length, Precision, Scale FROM FieldDefinition WHERE EntityDefinition.QualifiedApiName = 'Lead'

Getting the data from the console is a snap with the ColumnCopy extension for Chrome, about which you can learn more at https://chrome.google.com/webstore/detail/columncopy/lapbbfoohlcmlbdaakldmmallcbcbpjb. It copies everything except the label row in one swipe. That’s easy enough to hand copy. I am thrilled over the moon!

1
  • This is excellent, easy and exactly what I needed. I ran it in Workbench using Bulk CSV and was able to download the query results right into a spreadsheet. Perfect for my use case. Thanks!
    – joaniej
    Aug 10, 2021 at 18:44
0

With the following SOQL, you can get Customizable object without using limit and offset

SELECT NamespacePrefix,  IsCustomizable, QualifiedApiName FROM EntityDefinition where IsCustomizable = true  order by QualifiedApiName 

For more field can be used in where clause to avoid EXCEEDED_ID_LIMIT, you can refer to EntityDefinition Fields

0

The below code snippet might help you to get you the list of CUSTOM OBJECTS you have in your Salesforce Org. You need to execute this on 'Anonymous Window' and check for the Debug log for the result.

    List<Schema.SObjectType> getSObjectDetail = Schema.getGlobalDescribe().Values();

    for(SObjectType st : getSObjectDetail)
    {
        //Check the object is Custom Object ?
        if(st.getDescribe().getName().contains('__c')) 
            System.debug('Object API Name : '+st.getDescribe().getName()+'   Object Label : '+st.getDescribe().getLabel());
     }
1
  • 1
    Although you are correct in that it is possible to fetch the SObject information this way, OP asked for a way to get those with a SOQL query, not with the Describe API. Consider this: what if they want to issue a query through the REST API instead of using Apex? Then your solution does not work. Jul 6, 2022 at 11:34

You must log in to answer this question.

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