2

I'm trying to write controller which will get all the standard and custom objects from org and display them in picklist; after selecting the Object name from this Picklist - one PageblockTable should appear with related records below.

6

To get the list of all objects in your org, you can use something like this:

Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();

for(Schema.SObjectType thisObj : gd.values()) {
    System.debug(thisObj);
}

Run that in the Developer Console and you will see a long list of Object names:

Objects

Note this includes every object in your schema, including Custom objects and even things like Knowledge article types and field_history tracking objects...

Custom Objects

To put that into a picklist, in the Apex controller of your Visualforce page, instead of System.debug - build a list of Options:

public List<SelectOption> getObjectOptions(){
   List<SelectOption> options = new List<SelectOption>();
   Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();

    for(Schema.SObjectType thisObj : gd.values()) {
        options.add(new SelectOption(thisObj, thisObj));
    }

   return options;
}

and on your page:

<apex:selectList >
      <apex:selectOptions value="{!options}"/>
</apex:selectList>

Now you have a picklist of all your options.

Use actionFunction to run an "onclick" on that, or perhaps just some javascript, or a normal apex:button to submit the value back to the controller, and then use Schema again to get all the related lists for that object:

Map<string,string> childMap = new Map<String, String>();
List<Schema.Childrelationship> childList = Schema.getGlobalDescribe().get(selectedOption).getdescribe().getChildRelationships();

(where selectedOption is what was picked in the picklist).

Now you have a List of all the related object names! Use it as you will

(and don't forget test coverage!)

  • 1
    No issue .. I was stuck in the logic.. Thanks for the help :) ... I'll do the remaining – Atul Rakate Jan 6 '17 at 12:20
  • I'm not able to get the custom objects in map – Atul Rakate Jan 6 '17 at 12:51
  • The first map? Schema.getGlobalDescribe() definitely returns the custom objects. I have added a second screenshot to my answer to show this... note: there is no "order" to how they come back. so they could be anywhere in the list. – Simon Lawrence Jan 6 '17 at 14:26
0

If you would prefer to use javascript for your solution you can use this tool: https://github.com/bvellacott/salesforce-schema-reader

Upload the tool as a staticresource called schemaReader and this should work:

<apex:page docType="html-5.0" showHeader="false"> 

  <label for='picklist'>Object: </label>
  <select id="picklist"></select>

  <apex:includeScript value="{!URLFOR($Resource.schemaReader)}" />
  <script src="/soap/ajax/39.0/connection.js"></script>
  <script>
  // setup a connection instance
  sforce.connection.sessionId = "{!$Api.Session_ID}";  
  var connection = sforce.connection;

  // callbacks and batch size for loading the schema
  var batchSize = 50;
  function failure() { console.log('An error occured while trying to load the schema'); };
  function success(reader) { 
    console.log('Schema loaded successfully'); 
    // visit the id field of each type and log the metadata
    var list = document.getElementById('picklist');
    reader.shallowReadFields(function(field, object, path, reader){
      if(field.name === 'Id') {
        var e = document.createElement('option');
        e.innerHTML = object.label;
        e.setAttribute('value', object.name);
        list.appendChild(e);
      }
    });
  };

  // load the schema into the schema reader
  var reader = new SchemaReader(connection, batchSize, success, failure /*['Account', 'Contact']*/); // optionally you can limit the types that are fetched

  </script>
</apex:page>
  • 1
    Your answer is not relevant to the question. – Raul Feb 23 '17 at 12:16
  • 1
    Can you please demonstrate how this tool can be used to perform the task that was originally asked about? That would be getting objects (not fields) and putting them in a picklist. – Adrian Larson Feb 23 '17 at 14:22
  • The tool is designed to walk all fields in the objects that have been loaded. The visitor is passed the field, the object, path and the reader instance. It is meant to be an easy and fast way to read detailed information about the schema. You have an option of a shallow and a deep walk. Where the shallow walk wont follow relationships and the deep walk will. – bungee Feb 25 '17 at 0:30
0

Try this below mentioned code:

public class GetObjectPicklist{

   public Map<String, Schema.SObjectType> ObjNames{get; set;}
   public list<string> ObjStr {get; set;}
   public list<selectoption> ObjOpt {get;set;}

   public GetObjectPicklist(){
   ObjNames = Schema.getGlobalDescribe();
   ObjStr = new list<string>();
   ObjStr.addAll(ObjNames.keyset());
   ObjStr.sort();
   ObjOpt = new list<selectoption>();
   ObjOpt.add(new selectoption('--NONE--','--NONE--'));
   for(string s:ObjStr){
   ObjOpt.add(new selectoption(s,s));   
   }      
   }


}

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.