3

In my apex code I am adding some value into map. And I am trying to loop through the map on VF page using apex:repeat but I am getting an error that

This map cannot be used in an iteration because the keys cannot be sorted

Is anyone here can tell me what might be the issue. The versions of both the Apex code and VF page is same 31. my code is bellow

VF page :

    <apex:repeat value="{!sObjectFieldMap}" var="sobj">

    </apex:repeat>

controller :

public with sharing class survey_ResultV2Ctrl{

    public Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    public Map<Schema.SObjectType, List<Schema.SObjectField>> sObjectFieldMap {get; set;}
    public List<Schema.SObjectField> sObjectFieldLst = new List<Schema.SObjectField>();
    public List<wrapperClass> wcls {get; set;}
    public set<Schema.SObjectType> objNameSet =  new set<Schema.SObjectType>();

    public survey_ResultV2Ctrl(){
        sObjectFieldMap = new  Map<Schema.SObjectType, List<Schema.SObjectField>>();
        wcls =  new List<wrapperClass>();
        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 = 'a0F9000000R5Fau']){
            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));

                if(sObjectFieldMap.containskey(schemaMap.get(ffs.Name))){
                    sObjectFieldLst.add(fieldMap.get(ffs.Object_Fields_Name__c));
                    sObjectFieldMap.put(schemaMap.get(ffs.Name), sObjectFieldLst.clone());
                    //objNameSet.add(schemaMap.get(ffs.Name));
                    //wcls.add(new wrapperClass(schemaMap.get(ffs.Name), sObjectFieldLst));
                    sObjectFieldLst.clear();
                }
                else{
                    sObjectFieldLst.add(fieldMap.get(ffs.Object_Fields_Name__c));
                    sObjectFieldMap.put(schemaMap.get(ffs.Name), sObjectFieldLst.clone());
                    //wcls.add(new wrapperClass(schemaMap.get(ffs.Name), sObjectFieldLst));
                    //objNameSet.add(schemaMap.get(ffs.Name));
                }
            }

        }


        system.debug('The wapper class after loop ' + wcls);
        system.debug('The Map after loop ' + sObjectFieldMap);

    }


    public with sharing class wrapperClass{

        public list<Schema.SObjectField> fieldname {get; set;}
        public Schema.SObjectType objectName {get; set;}

        public wrapperClass(Schema.SObjectType objectName, list<Schema.SObjectField> fieldname){
             objectName = objectName;
             fieldname = fieldname;
        }
    }    
}

Please help me if anyone have any idea on this issue.

  • 3
    Even though it's allowed, try not to use complex objects as Map keys. Instead of using the SObjectType, get the type's object name, which is a unique String, and use that as the key. – ScottW Sep 27 '14 at 17:32
  • It is perfectly safe to use complex objects as keys, as long as they are implementing hashCode() and equals(). This might not be true for Schema.SObjectType and result in the error mentioned. – Alexander Johannes Sep 29 '14 at 10:12
1

Instead of using complex objects as keys for the map, you can use wrapper class and comparable interface for sorting the values.

For Salesforce Documentation: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_comparable.htm

|improve this answer|||||

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.