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
waterwater 

Custom type as Map key fails when more than one key is returned - error "...keys cannot be sorted"

Hello all -

 

I am trying to render a multi-level Map consisting of a single custom type (ProjectView objects) in my visualforce page.  The multi-level Map's structure is as follows:  Map<ProjectView, Map<ProjectView, List<ProjectView>>>

 

My visualforce page renders the multi-level Map successfully when only one key is present at each level (i.e., one key at Map<ProjectView, ...>>> and one key at Map<..., Map<ProjectView, ...>>>.  I receive an error message when I introduce more than one key at either level in the Map.  The visualforce error is as follows:  "This map cannot be used in an iteration because the keys cannot be sorted."

 

I provide an implementation of the equals method and the hashCode method, as instructed in the salesforce documentation article "Using Custom Types in Map Keys and Sets" (link: Using Custom Types in Map Keys and Sets).

 

My implementation of the equals method:

public Boolean equals(Object obj) {
        System.debug('=== entered the equals method...');
        // Your implementation
        if (obj instanceof ProjectView) {
            ProjectView p = (ProjectView)obj;
            System.debug('=== equals method: (projViewId == p.projViewId) ' + (projViewId == p.projViewId));
            System.debug('=== equals method: (p.projViewId == projViewId) ' + (p.projViewId == projViewId));
            System.debug('=== equals method: projViewId = ' + projViewId + ', p.projViewId = ' + p.projViewId);
            return (projViewId == p.projViewId);
        }
        System.debug('=== equals method: obj is NOT an instance of ProjectView...');
        return false;
    }

 My implementation of the hashCode method:

public Integer hashCode() {
        // Your implementation
        System.debug('=== this.projViewId.hashCode(): ' + this.projViewId.hashCode());
        System.debug('=== projViewId.hashCode(): ' + projViewId.hashCode());
        System.debug('=== *this* keyword in the hashCode() method: ' + this);
        return this.projViewId.hashCode(); 
/**
each ProjectView object corresponds to a single source custom object record in the salesforce database; projViewId corresponds to the source record's unique 18-digit salesforce Id, so I assume this is guaranteed to be unique for every ProjectView object
**/ }

 

According to the debug logs, it appears multiple keys at either level in the Map are added to the Map successfully and are accessible/retrievable when I run the following lines of code (projectSubProjectMap.keySet() corresponds to all of the keys in the second-level Map corresponding to a single key in the first-level/outer Map):

 

Set<ProjectView> keysInSubMap = new Set<ProjectView>();

// the following variable assignment line prints out the multiple keys to the debug log, as I expect

 

keysInSubMap = projectSubProjectMap.keySet();

// the debug log printout of the above line's variable_assignment (notice two keys are present-- Proj-12483, Proj-12480):

 

VARIABLE_ASSIGNMENT|[206]|keysInSubMap|{"serId":1,"value":[{"projId":"a1LR00000002NX9MAM","projName":"Proj-12483","projProjectTitle":"PROJECT 2","projViewId":"a1LR00000002NX9MAM"},{"projId":"a1LR00000002NM0MAM","projName":"Proj-12480","projProjectTitle":"testing","projViewId":"a1LR00000002NM0MAM"}]}|0x3a31daa0

// the following line, however, prints out only one of the multiple keys to the debug log, whereas I expect it to print out all of the multiple keys...

 

System.debug('=== List of all the keys in the sub Map: ' + keysInSubMap);

// the debug log printout of the above line (notice only one key is present-- Proj-12483 --where I expect two keys to be present):

 

USER_DEBUG|[207]|DEBUG|=== List of all the keys in the sub Map: {ProjectView:[compType=null, componentDueDate=null, componentGcEndDate=null, componentGcStartDate=null, componentId=null, componentName=null, gcActive=null, gcConsultantType=null, gcCurrentlyEmployed=null, gcId=null, gcName=null, gcProjCount=null, gcSubProjCount=null, projCdGrants=null, projCdResearch=null, projId=a1LR00000002NX9MAM, projInternalDueDate=null, projMcd=null, projName=Proj-12483, projProjectTitle=PROJECT 2, projViewId=a1LR00000002NX9MAM, projectCount=null, userId=null, userName=null]}

 
Interestingly, when I comment out my visualforce page's html responsible for rendering the returned multi-level Map, and simply reference the multi-level Map using {!ConsultantsX} (the method in my controller class responsible for returning the multi-level Map is getConsultantsX()), the visualforce page renders the following--

 

{ProjectView=common.apex.runtime.impl.MapValue@81a39192, ProjectView=common.apex.runtime.impl.MapValue@764147e4, ProjectView=common.apex.runtime.impl.MapValue@320bcc0e}

 

--which I assume corresponds to the three unique keys that I expect to be present at the main-level/outermost returned Map (the eight-character MapValue@ changes with every refresh of the visualforce page; the three MapValue@s rendered are always unique, so I'm thinking the visualforce page recognizes to some extent the outermost Map's multiple unique keys)...

 

I think I may (though I could be completely wrong) have run into a possible bug which would require salesforce.com to address via a patch or something...  I came across a somewhat similar, known issue that salesforce.com recently addressed (involved Maps with Id used as the key):  Maps with ID used as the key cannot be used in an iteration and fail with error 'This map cannot be used in an iteration because the keys cannot be so...

 

I would appreciate some guidance.  Please let me know if there is anything else I should provide in order to present a clearer picture of this matter.  Thank you in advance!

 

Satish_SFDCSatish_SFDC

Is this related to this known Issue:

 

https://success.salesforce.com/issues_view?id=a1p30000000SxZUAA0

 

 

 

Regards.

Satish Kumar

waterwater

Satish-

 

Thank you for your response.

 

I actually provided a link to the same known issue in the penultimate paragraph of the original post.  I feel my issue is at least somewhat related to that known issue (i.e., resultant error message is the same upon attempting to render a Map with non-primitive data type as the Map Key).

 


Satish_SFDC wrote:

Is this related to this known Issue:

 

https://success.salesforce.com/issues_view?id=a1p30000000SxZUAA0

 

 

 

Regards.

Satish Kumar




waterwater

All-

 

I still do not know what is causing the problem and am still thinking it might be a critical salesforce bug that requires a salesforce patch to fix, as was the case with the following similar, known issue I previously linked to:  https://success.salesforce.com/issues_view?id=a1p30000000SxZUAA0 ("Maps with ID used as the key cannot be used in an iteration and fail with error 'This map cannot be used in an iteration because the keys cannot be so[rted]").

 

I would appreciate someone's attempt to replicate this issue I am experiencing.  Please tell me what additional code, if any, I should provide in order for you to be able to do so. 

 

Thank you in advance!