+ Start a Discussion
Kr SatyamKr Satyam 

How to get api name and label of All objects in org Without Global Describe and Asynchronous apex

Can Someone help me getting only label and api name of all objects in org .

I can not use Asynchronous apex  and Global Describe is hitting CPU Time Limit.

For GllobalDescribe I am using the below code : 
Map<String, String> mapApiAndLabel = new Map<String, String>();
for(Schema.SObjectType thisObj : Schema.getGlobalDescribe().values()) {
    Schema.DescribeSobjectResult res = thisObj.getDescribe();
    if( res.isAccessible() && res.isQueryable() && (! res.isCustomSetting()) && res.isUpdateable() && res.isCreateable() ) { 
        mapApiAndLabel.put(res.getName(), res.getLabel());
    }
}

I am using an alternate method by querying on Entity Defination but it is giving error  =
'System.QueryException: EntityDefinition does not support queryMore(), use LIMIT to restrict the results to a single batch'
 
Map<String, String> mapApiAndLabel = new Map<String, String>();
for(EntityDefinition entDef : [SELECT QualifiedApiName, IsWorkflowEnabled , Label FROM EntityDefinition Where IsApexTriggerable = true AND IsCustomizable = true 
                               AND IsCompactLayoutable = TRUE AND IsCustomSetting = False AND IsDeprecatedAndHidden = FALSE And IsEverUpdatable = TRUE 
                               And IsQueryable = TRUE AND IsReplicateable = TRUE AND IsWorkflowEnabled = TRUE 
                               Limit 2000 ] 
   ){
       mapApiAndLabel.put(entDef.QualifiedApiName, entDef.Label);
   }

Can someone hellp me getting object label and api name using Metadata Services
SRKSRK

1) Tooling API do not support limit in query, which means that "Limit 2000" that will be ignorred when query will be processed
https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/reference_objects_soql_limits.htm#topic-title

Either you figure out more critria for to be put in where clause which reduse the result or 
i think it good to use Schema.getGlobalDescribe()

Now to avoid CPU time limit here is few thing you can do

1) one thing you can do is if in your UI you are showing list of all objects in a drop down or a table then you can only show 1st 20-30 values with a load more option

other thign you can do is on load just run this and crate your map

public map<string, SObjectType> AllObjectMap  = new map<string, SObjectType>();
public sizeofmap = 0;
AllObjectMap  = Schema.getGlobalDescribe();
sizeofmap  = AllObjectMap.size();

then if its VF page then after page from javascript you can call the  apex class method muliple time and method will return 10-20 record name each time and you can mak a complete list in JS funcation
 

3) In place of for loop try using while loop it is much faster something like this
List<Vechile__c> accs = [SELECT Id, Name FROM Vechile__c LIMIT 5000];
Integer size = accs.size();
Integer i = 0;
while(i < size)
{
     accs[i].Name += TEST_STR; ++i;
}

 

Kr SatyamKr Satyam
i am using custom multiselect componet in lightning where user will see all objects in his org and can select  multiple objects and futher processing will be done ,
i need to show api and label both.
SRKSRK

can you set the size of multiselct picklist to 10 and just put 20 values in it and as user scroll down when the scroll hit the end

you can call the JS method to load next 20 objects names until list not end 




something like this
 

Map<String, String> mapApiAndLabel = new Map<String, String>();

List<Schema.SObjectType> ListOfObjs = Schema.getGlobalDescribe().values());

My Method(ListOfObjs,0,20) 

My Method( List<Schema.SObjectType> ListOfObjs, Startnumber, Endnumber)
integer i = 0;
integer j =Startnumber;
while(i<Endnumber)

{

Schema.DescribeSobjectResult res = ListOfObjs[j].getDescribe();

j++;

if( res.isAccessible() && res.isQueryable() && (! res.isCustomSetting()) && res.isUpdateable() && res.isCreateable() )

{

mapApiAndLabel.put(res.getName(), res.getLabel());
i++;
}

}

Kr SatyamKr Satyam
Thanks for the reply , but i am looking for a solution where user will be able to see all objects(api name and label) at once.
SRKSRK

BUT salesforce always going to have some or other governer limits

even somehow we optamize the code and figure out a way to show all existing objects without hitting the CPU limit time there can be always a risk that in future as the size of org and number of objects increse it may come again 

I suggest it better to build a soluction which can work with any number of objects just by scoping, (it like infinite scrooling salesforce foloow the same in lighitng exprince) if you goto object manager and try to see all the fields in object it won't show you all the field you have to scroll down to load more feilds 

i belive design wise it better approch 

but it just my suggention my friend :)

 

have a good day ahaed 

Kr SatyamKr Satyam
Thanks for your suggestion , now i also think lazy loading will be a better sloution.