You need to sign in to do that
Don't have an account?
URGENT! Need exception handling for map with no key on VFP
I have a problem with usings maps on a visual force page.
I have a static visual force page with a fixed number of fields. This visual force page is like a spreadsheet with many rows. On each row there is a specific 'Type' string value, then it is followed by many more fields on the same row.
What i would like to do, rather than use a ton of variables for this page is use a map collection. I would like to use the Type field as the key, and then the values to fill in the rest of the fields for that row. This works great, but only if the Type exists for a specific Account record. Every Type will not and cannot exist for every account record.
The only thing i need to do now is be able to handle the error: "Map key not found in map" which is showing up on the visual force page.
Is there any way i can handle this exception? Can i do it inside of the method? How do i know what is entering the method?
for(Table__c p : [Select Type__c, value, value, value, value From Table__c]) { mapOfValues.put(p.Type__c, p); } public Map<String, Table__c> getMapOfValues() { //WHEN THIS METHOD IS ENTERED, I WANT TO KNOW WHAT IS ENTERING IT SO I CAN HANDLE EXCEPTIONS return mapOfValues; }
Can i somehow handle the error here on the component?
<apex:outputText value="{!mapOfValues['Type'].Value}"/>
Please help if you can. Thanks for looking it over.
Without seeing the full scope of what you need...
Can you just check the map, after you load it, for any missing types and if there are any, load the map with nulls for those values. Then your map won't be sparse, but will have null values which you can deal with using 'rendered=...' in the VF. This might be cleaner than dealing with exceptions.
// Make a list of all the Types you're going to have.
List<String> myTypes = new List<String>('type1','type2','type3',.....,'typeN');
// Load your types into the map
for (Table__c p: [select....]) mapOfValues.put(p.Type__c, p);
// If there are any missing types, add them with null values
for (String s: myTypes) if ( !mapOfValues.hasKey(s)) mapOfValues.put(s,null);
Best, Steve.
I can't be a 100%, but i think the ternary operator should be usable in VF pages, so you could
(A ternary operator is an if then else in one line , so <boolean condition> ? value if true : value if false)
I tried using conditional statements, but i don't think it's possible to determine if a map key is available on the Visual Force page.
Thanks steve, i'll try this and report back.
Help, lol.
I don't know why but the visual force page is still not finding the key. I ran debugging test to lookup that map, and yes the key is in the map. Why is the page still not finding it? Could it be an issue with quotes?
Frustrating. I literally spent all day on this issue. Just to learn that i'm going to have to assign a variable to every field on the entire page because a Type/Map Key may not exist.
Either that or create records for every type.
Very frustrating.
Perhaps I don't fully understand what you're trying to do... care to post more code? Best, Steve.