Recipes by Category

App Distribution (2) Bundle logic, interface and services for distribution. App Logic (37) The Apex programming language, workflow and formulas for logic. Collaboration (6) The Salesforce Chatter collaboration platform. Database (29) Data persistence, reporting and analytics. Integration (33) Web Service APIs and toolkits for integration. Security (9) Platform, application and data security. Tools (4) Force.com tooling User Interface (36) Visualforce MVC and metadata-drive user interfaces. Web Sites (12) Public web sites and apps with optional user registration and login.
Beta Feedback
Cookbook Home » Retrieve a List of Objects using Apex

Retrieve a List of Objects using Apex

Post by Osama Nasir  (2010-10-18)

Status: Unverified
Level: intermediate

Problem

In many scenarios, there is a requirement to retrieve a list of objects in an organization to process them. Apex has a class called Schema. It contains the entire schema of your organization. Objects, their fields, field type etc. can all be retrieved using this class.

This can be very useful. Suppose you want to create a dropdown menu and bind the list of objects in your organizations to it. You may also want to generate a list of fields when you select an object in the first dropdown.

Solution

The following code shows how to generate a list of objects. First create a Visualforce page as shown:

<apex:page controller="objectList" >
  <apex:form >
    <apex:SelectList value="{!val}" size="1">
      <apex:selectOptions value="{!Name}"></apex:selectOptions>
    </apex:SelectList>
  </apex:form>
</apex:page>

The Visualforce page uses the following Apex class as its controller:

  public class objectList{
    public String val {get;set;}
   
    public List<SelectOption> getName()
    {
      List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();     
      List<SelectOption> options = new List<SelectOption>();
      
      for(Schema.SObjectType f : gd)
      {
         options.add(new SelectOption(f.getDescribe().getLabel(),f.getDescribe().getLabel()));
      }
      return options;
     }
  }
 

Discussion

  • This will generate all the objects in your organization. You can restrict them. For example if you want to retrieve only custom objects, you can place a check to ensure that only objects that contain '__c' should be added in the list.

Share

Recipe Activity - Please Log in to write a comment

Also if you want to remove the system object from this list you can use this code:

http://forceguru.blogspot.in/2012/07/please-remove-system-objects-from-my.html

by Ankit Arora (forceguru)  (2012-10-07)

Use the following code to show only the filtered objects


 if(filter ==null){
        filter ='User';
    }
 
    for(Schema.SObjectType f : gd)
      {
        if(f.getDescribe().getLabel().contains(filter)){
            options.add(new SelectOption(f.getDescribe().getLabel(),f.getDescribe().getLabel()));
        }
      }

by Rajendran L  (2012-02-01)

Hi Osama,

Thanks for the above code.
Can u pls tell me the code for finding only the list of custom objects that we created.

by Rohit Sharma  (2012-01-02)

Good work..!!

voted as verified by Prathamesh Joshi  (2011-06-10)

HI,

Thanxs for the code this is what i am looking for...

will you please give me the code of getting the fields of the selected object as i am not able to get them.

thanxs in advance

by divyanshu

by divyanshu tiwari  (2011-03-01)

Hi,

The same code would run for all the asked scenarios. All you need to do is reRendering Fields picklist on the change in the Parent picklist.

Osama

by Osama Nasir  (2011-02-08)

Hi Osama,

  The code that you have given work fine. However I want to display a list of all objects to the user on my VF page and based on the object that he selects, I would display the fields accordingly for that object. Could you please help me with this problem.

Thanks in advance !!

by Jigar Shah  (2010-12-24)

Hi,

 I think the same code metioned above should work for you. It gives you all the objects  in the org. You can use the getName() method to get the API names of the objects.  Could you please explain your scenario i detail if this does not provide a solution to your problem.

by Jigar Shah  (2010-12-24)

We'd like to create a page (similar to the "All Tabs" page) that displays all of our Salesforce.com objects, whether they've been assigned to a tab or not (both custom and standard objects). I thought perhaps your code would lead me down this path but I didn't find much success. Any idea how much effort it would take to modify this code to create such an object list?

by a0930000007N4EU  (2010-11-24)

X

Vote to Verify a Recipe

Verifying a recipe is a way to give feedback to others and broaden your own understanding of the capabilities on Force.com. When you verify a recipe, please make sure the code runs, and the functionality solves the articulated problem as expected.

Please make sure:
  • All the necessary pieces are mentioned
  • You have tested the recipe in practice
  • Have sent any suggestions for improvements to the author

Please Log in to verify a recipe

You have voted to verify this recipe.