I'm fairly new to developing on the Salesforce platform and I'm running into a bit of a problem. I have an Apex class with a map of Opportunity Stages and total values, and I'm trying to render a list item only if that map contains the related key - however I'm getting "Unknown function keyExists" error on the Visualforce page.
Apex Class:
Public Map<string,decimal> rawMap{get;set;}
public boolean keyExists(String checkKey) {
return rawMap.containsKey(checkKey);
}
Visualforce Attempt 1:
<apex:outputPanel rendered="{!keyExists('Closed Lost')}">
<-- Some Text -->
</apex:outputPanel>
Visualforce Attempt 2:
<apex:form>
<apex:actionFunction action="{!keyExists}" name="test">
<apex:param value="Closed Lost" />
</apex:actionFunction>
</apex:form>
Is there something obvious I'm missing? Or any ideas on how I can get around this?
UPDATE: I found a messy solution...
- Put the Map into a Javascript string variable on the Visualforce page.
- Create a Javascript method that checks the strings contents and sets a hidden div to contain the output boolean.
- Set your <apex:outputPanel> to render on that hidden components value
Apex Class:
Public Map<string,decimal> rawMap{get;set;}
Visualforce:
<script type='text/javascript'>
var rawMap = '{!rawMap}';
/* Checks if map contains a certain Opportunity Stage */
function containsKey(findKey) {
document.getElementById('containsKeyVar').innerHTML = rawMap.includes(findKey);
}
</script>
<!-- This element is being used as a global variable: does Opportunity map contain -->
<div id="containsKeyVar" style="display: none;">false</div>
<!-- Check if Stage Exists, and only then render -->
<script type="text/javascript">
containsKey('Closed Lost');
</script>
<apex:outputPanel rendered="{!$Component.containsKeyVar}">
<!-- Some Text -->
</apex:outputPanel>
Anyone have a better solution?