I am trying to make a picklist to be able to select a field on an object and pass this field out of the component. I have created the following VF page to display this picklist however i am having issues with creating an Apex:Variable within the ComponentBody
<apex:page>
<apex:form>
<c:dynamicpicklist var="SelectedField" objectType="Opportunity">
<!--{!SelectedField}-->
</c:dynamicpicklist>
</apex:form>
</apex:page>
The component created is as follows:
<apex:component controller="DynamicPicklistController" access="global">
<!-- component variable -->
<apex:attribute name="var" access="global" type="String" description="TODO: Describe me"/>
<!-- component attributes -->
<apex:attribute name="objectType" access="global" type="String" description="desc" required="true" assignTo="{!objectToCheck}"/>
<apex:attribute name="fieldType" access="global" type="String" description="desc" required="false" />
<apex:componentBody >
<!--The Following line is failing -->
<apex:variable var="{!var}" value="{!SelectedField}"/>
<apex:selectList value="{!SelectedField}" multiselect="false" size="1">
<apex:selectOptions value="{!picklistValues}" />
</apex:selectList>
</apex:componentBody>
The controller used with this class is the following:
global class DynamicPicklistController {
global String objectToCheck {get; set;}
global String selectedField { get; set; }
global List<SelectOption> getPicklistValues()
{
List<SelectOption> pickListVals = new List<SelectOption>();
//Loop through each field on the object and create a selection option.
for(String fieldName: Schema.getGlobalDescribe().get(objectToCheck).getDescribe().Fields.getMap().keySet())
{
pickListVals.add(new SelectOption(fieldName,fieldName));
}
Return pickListVals;
}
}
The error i am receiving when compiling the component is
(DynamicPicklist) common.apex.runtime.bytecode.BytecodeExecutionContext cannot be cast to common.apex.runtime.impl.Execution (Line: 1)
When i remove the <apex:variable var="{!var}" value="{!SelectedField}"/>
line the code compiles and the picklist populates on the VF page with the fields on the given object. I have a feeling the issue may be related to the fact the picklist value is dynamically allocated and when setting the variable this is causing an issue.