2

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(){
        survey_ResultDisplay();
    }

    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();
            if(schemaMap.containsKey(ffs.Name)){
                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));
                sObjectFieldLst.add(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 Sep 27 '14 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. – rael_kid Sep 27 '14 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 Sep 27 '14 at 8:21
  • Yes @Sunny your are right. – Anu Sep 27 '14 at 8:23
2

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();
            FieldsList.add(dfield.getname());

        }
        ObjectWithFieldsMap.put(ObjectName,FieldsList);
    }


 } }
  • this one is very helpful – user59759 Mar 3 '16 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.