9

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 ? – Samuel De Rycke Nov 26 '15 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. – David A. Gray Jan 29 at 9:32
22

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
  • 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
19

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 
3
  • 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
  • @Omkar More offered a solution to this. Scroll down. – David A. Gray Jan 29 at 9:33
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? – 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
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. – David A. Gray Jan 29 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. – David A. Gray Jan 29 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. – Jason Clark Feb 21 at 18:20
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() );   
}
1
  • 2
    How does this provide more information than the answers already given ? – Samuel De Rycke Dec 8 '15 at 20:07
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!

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

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.