Extracting list of all the objects (Standard + Custom) in Salesforce

Adding another category in blog today : “Just for kicks!!”, which is the reason for this post.

Today as I was whiling away hours, a thought struck my mind ‘Why not list out all the objects(which is quite simple) and their related objects(this was challenging) as well in a VF page’?

The reason listing the child object for each object was challenging is because salesforce(governer limits) only allows 100 childrelationship describes per apex invocation, as we already have more 100 objects in our org, doing child describes for each objects will result in more than 100 child relationship describe per invocation. As a workaround: I added a apex:actionFunction which will be fired every 2.5 seconds triggering a method in the class, so each time it is triggered it is considered as one invocation.

allObjectList

So here’s the code (save the class 1st and then the Page):

Apex Class:

/********
Copyright (c) <2013> <junglee Force(jungleeforce@gmail.com)>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:


THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
**********/

public class DisplayObjectDetail{
    
    public list<objectClass> standardObjList{get;set;}
    public list<objectClass> customObjList{get;set;}   
    public FINAL integer numberOfObject;
    public set<string> objectSet{get;set;}
    //This is a variable to check if we're done with the Iteration.
    public string isIterationDone{get;set;}
    
    //Constructor.
    public displayObjectDetail(){
        
        //initializing the lists.

       customObjList  = new list<objectClass> ();
       standardObjList = new list<objectClass> ();
       objectSet= new set<string>();
       list<schema.sObjectType> allObjects = Schema.getGlobalDescribe().Values();
        //get number of objects available for iteration.
       numberOfObject = allObjects.size();
       isIterationDone = 'No';
       
   }
   
    public void ObjectList(){
        // check if we have all the objects added in the list
        if((customObjList.size() + standardObjList.size())<numberOfObject){
            integer i=0;
            for(Schema.SObjectType objTyp : Schema.getGlobalDescribe().Values()){
                //We will iterate over this list only 99 times as there is governer limit of 100 childrelationship describes per invocations.
                if(i<99){
                    //adding the object name to the set, so that we do not add duplicates to the list.
                    if(!objectSet.contains(objTyp.getDescribe().getName())){
                        i++;
                        objectSet.add(objTyp.getDescribe().getName());
                        //check if its a custom or standard object.
                        if(objTyp.getDescribe().isCustom()){
                            customObjList.add(new objectClass(objTyp.getDescribe().getLabel(), objTyp.getDescribe().getName(), objTyp.getDescribe().getKeyPrefix(), objTyp.getDescribe().getChildRelationships(), objTyp.getDescribe().isCustomSetting(), objTyp.getDescribe().getRecordTypeInfos()));
                        }else{
                            standardObjList.add(new objectClass(objTyp.getDescribe().getLabel(), objTyp.getDescribe().getName(), objTyp.getDescribe().getKeyPrefix(), objTyp.getDescribe().getChildRelationships(), objTyp.getDescribe().isCustomSetting(), objTyp.getDescribe().getRecordTypeInfos()));
                        }
                    }
                }else{
                    break;
                }
            }
        }else{
            isIterationDone = 'Yes';
        }
    }
   
   //This is a wrapper class. 
   public class objectClass{
        public string label{get;set;}
        public string apiName{get;set;}
        public string keyPrefix{get;set;}
        public boolean isCustomSet{get;set;}
        public list<Schema.ChildRelationship> childObjects;
        public list<string> childObjectArray{get;set;}
        public list<schema.RecordTypeInfo> recordTypes;
        public list<string> recordTypeArray{get;set;}
        public objectClass(string lab, string api, string prefix, list<Schema.ChildRelationship> childObj, boolean isCustomSetting, list<schema.RecordTypeInfo> recordTypeList){
            this.label = lab;
            this.apiName = api;
            this.KeyPrefix = prefix;
            this.isCustomSet = isCustomSetting;
            this.childObjectArray = new list<string>();
            this.recordTypeArray = new list<string>();
            if(childObj!=null){
                for(schema.childRelationShip ch : childObj){
                    this.childObjectArray.add(ch.getChildSObject().getDescribe().getLabel()+' ('+ch.getChildSObject().getDescribe().getName()+')');
                }
            }
            if(recordTypeList!= null){
                for(schema.RecordTypeInfo rt: recordTypeList){
                    this.recordTypeArray.add(rt.getName());
                }
            }
        }
    }
}

VF page:

<!--
Copyright (c) <2013> <junglee Force(jungleeforce@gmail.com)>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:


THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<apex:page controller="DisplayObjectDetail" readOnly="true">
    <script type="text/javascript" src="/resource/jQuery"></script>
    <script type="text/javascript">
    
        var j$ = jQuery.noConflict();
        var timeInter =setInterval(function(){iterateObjectMethod()},2500);

        function iterateObjectMethod(){
            getObjectList();
            var iterationDone = j$("#isIterationDoneDiv").text();
            if(iterationDone  == 'Yes'){
                j$("#isIterationDoneDiv").hide();
                clearInterval(timeInter);
            }
        }
    </script>   

    <apex:actionStatus id="myStatus" startText="Fetching the Data..." stopText=""/><br/><br/>
    <apex:form >
        <apex:actionFunction action="{!ObjectList}" status="myStatus" name="getObjectList" reRender="thePageBlock"/>
    </apex:form>
    <h1>Here's the List of Standard and Custom Objects:</h1>
    <apex:pageBlock id="thePageBlock">
        <b>Standard Objects:</b>
        <apex:pageBlockTable value="{!standardObjList}" var="std">
            <apex:column headerValue="Name">{!std.label}</apex:column>
            <apex:column headerValue="API Name">{!std.apiName}</apex:column>
            <apex:column headerValue="Key Prefix">{!std.keyPrefix}</apex:column>
            <apex:column headerValue="ChildObjects">
                <apex:repeat value="{!std.childObjectArray}" var="ch">
                    {!ch}<br/>
                </apex:repeat>
            </apex:column>
            <apex:column headerValue="Record Types">
                <apex:repeat value="{!std.recordTypeArray}" var="rt">
                    {!rt}<br/>
                </apex:repeat>
            </apex:column>
        </apex:pageBlockTable>
        <br/><br/><br/>
        <b>Custom Objects:</b>
        <apex:pageBlockTable value="{!customObjList}" var="cust">
            <apex:column headerValue="Name">{!cust.label}</apex:column>
            <apex:column headerValue="API Name">{!cust.apiName}</apex:column>
            <apex:column headerValue="Key Prefix">{!cust.keyPrefix}</apex:column>
            <apex:column headerValue="Custom Setting">{!cust.isCustomSet}</apex:column>
            <apex:column headerValue="ChildObjects">
                <apex:repeat value="{!cust.childObjectArray}" var="ch">
                    {!ch}<br/>
                </apex:repeat>
            </apex:column>
            <apex:column headerValue="Record Types">
                <apex:repeat value="{!cust.recordTypeArray}" var="rt">
                    {!rt}<br/>
                </apex:repeat>
            </apex:column>
        </apex:pageBlockTable>
        <div id="isIterationDoneDiv">{!isIterationDone}</div>
     </apex:pageBlock>
</apex:page>

If you think there’s a better way of doing this, please do share.

In the coming days I will post about doing the same thing The pure javascript way.

Advertisements