I am getting all objects and fields now I need to add the object and field to a map where map is

public List sObjectFieldLst = new List();

How can add the values to the map from Schema.SObjectType When I tried to add I am getting an error :

Error: Compile Error: Incompatible key type Schema.SObjectType for MAP<SObject,LIST<Schema.SObjectField>>

My code :

public class survey_ResultV2Ctrl{
    public Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    public Map<SObject, List<Schema.SObjectField>> sObjectFieldMap = new  Map<SObject, List<Schema.SObjectField>>();
    public List<Schema.SObjectField> sObjectFieldLst = new List<Schema.SObjectField>();

    public survey_ResultV2Ctrl(){

    public void survey_ResultDisplay(){

        for(Filter_Field_Set__c ffs : [SELECT Name, Field_Set_Category__c, Object_Fields__c, Object_Fields_Name__c FROM Filter_Field_Set__c Where Field_Set_Category__c = 'a0F9000000R5FZr']){
            system.debug('All the result from Filter Field Set object ' + ffs.Name);
            system.debug('Schema Map contains Key as object name ' + schemaMap.containsKey(ffs.Name));
            Map <String, Schema.SObjectField> fieldMap = schemaMap.get(ffs.Name).getDescribe().fields.getMap();
                system.debug('Schema object Values ' + schemaMap.get(ffs.Name));
                system.debug('All the fields related to Schema object ' + fieldMap.get(ffs.Object_Fields_Name__c));
                sObjectFieldMap.put(schemaMap.get(ffs.Name), sObjectFieldLst);



The object and field names are saved in a object called Filter_Field_Set__c. Please help me out to get the solution on this issue.

  • U mean to say, key is nothing but object name and values will be fields?
    – Sunny
    Commented Sep 27, 2014 at 8:07
  • You should declare your map as public Map<Schema.SObjectType, List<Schema.SObjectField>> sObjectFieldMap = new Map<Schema.SObjectType, List<Schema.SObjectField>>(); That will give you a mapping of each type to a list of fields for that type. If you want a mapping from an instance of an object to a list of it's fields, use sObjectFieldMap.put(schemaMap.get(ffs.Name).newSObject(), sObjectFieldLst); when adding to the map. Commented Sep 27, 2014 at 8:13
  • Yes that I it works. But@Lex I future if I need to insert the records into object is it possible if I declare this.
    – Anu
    Commented Sep 27, 2014 at 8:21
  • Yes @Sunny your are right.
    – Anu
    Commented Sep 27, 2014 at 8:23

1 Answer 1


Here is the code you needed,

 public class GetAllSObjectWithFields {
 Public Map<String, Schema.SObjectType> AllObjmap;
 public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
 public GetAllSObjectWithFields()
    list<schema.sObjectType> allObjects = Schema.getGlobalDescribe().Values();

    AllObjmap = New Map<String, Schema.SObjectType>();
    AllObjmap = Schema.getGlobalDescribe();
    System.debug('******All object Names :'+ AllObjmap.keyset());

    map<String,List<String>> ObjectWithFieldsMap = new map<String,List<String>>();
    for(String ObjectName:AllObjmap.keyset())
        Map <String, Schema.SObjectField> fieldMap = schemaMap.get(ObjectName).getDescribe().fields.getMap();
        List<string> FieldsList = new List<string>();
        for(Schema.SObjectField sfield : fieldMap.Values())
            schema.describefieldresult dfield = sfield.getDescribe();


 } }
  • this one is very helpful
    – user59759
    Commented Mar 3, 2016 at 13:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .