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.
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.
Reblogged this on Sutoprise Avenue, A SutoCom Source.
Very cool – for the non-coders like myself, note that you need to have jQuery installed as a static resource for this to function.
Why we need some custom apex and VF to extract this list is the real quesion!
I missed mentioning the static resource part. Thanks for pointing it out, I will add that in the post.
For us non developer types, can you tell me how to setup that jQuery static resource? I tried this out by getting the .zip and creating the static resource but when I execute my page render just the headers and no data shows up.
To save the jquery file in the static resource you can follow the below steps:
1) Go to https://code.jquery.com/jquery-1.11.0.min.js.
2) Save the page as jquery.js in your local machine
3) Go to the static resource page in the salesfroce and create a new static resource with the name jquery and upload the file that you just saved.
Hope this helps!
Great solution to an option that should be available out of the box. Can you append your code to allow it to add the “CreatedBy” standard field on the results list?
Awesome, this worked like a charm.
Thank you!
Doesn’t appear to be loading the objects, are there any security preferences that need to be applied for this to work?
Hi,
I want to fetch only those objects whose allow reports checkbox in enabled.
Thanks,
Sandy
Really useful stuff.
Hi,
Thanks a lot for he code.
However, it is not loading any objects for me.
I assume this should also be working with newer jquery version (v2.1.4)?
Thanks, Detti