function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PulPul 

How to get all fields of all objects from a sandbox in apex class?

Hi I tried teh below code to get all fields of all objects.. i'm getting too many field describes error as the field describe is above 100. Please suggest how to resolve this issue?

 

 Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
   Map<String,Schema.SObjectField> theFieldmap;
       for(Schema.SObjectType s:gd.values()){
     Schema.DescribeSObjectResult r = s.getDescribe();
           theFieldmap= r.fields.getMap();
            }

souvik9086souvik9086

You will get that error if the request goes beyond 100. As getDescribe method can be maximum 100.

What you can do for this requirement is instead od fetching all the fields of all the objects. You can add all your objects in a picklist. From there by selecting one object you can see all the fields in that particular object. It will reduce the no of requests.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

PulPul

I am not fetching all fields to show in VF page. I want to do some backend operations. Is there anyway to get all the picjklist fields alone without getting all type of fields?

souvik9086souvik9086

Yes you can.

 

Check like this

 

fields = gd.get(objectName).getDescribe().fields.getMap();
List<SObjectField> fieldtokens = new List<SObjectField>();
fieldtokens = fields.values();
for(SObjectField fieldtoken : fieldtokens) {
DescribeFieldResult dfr = fieldtoken.getDescribe();
if(dfr.getType().name()== 'Picklist'){
fieldParentObjMap.put(dfr.getName(),dfr.getReferenceTo());
}
}

 

Note this is for a particular object "objectName".

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

DY DharamDY Dharam
Hi Pul,

I hope this will help you to get the fileds name from the Object name:

String SobjectApiName = 'Account';
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();
for(String fieldName : fieldMap.keyset() )
    {
        system.debug('fieldName->>'+fieldName); // This will give you the api name of the field name.
    }
Thanks