7

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.

2 Answers 2

1

You've it said how var is defined in the expression var="{!var}".... I suspect you meant to have some other variable name there and the use of the name var is causing some craziness inside the compiler by conflicting with the var attribute... Out on a limb with this but maybe try another value there. It doesn't appear to be used elsewhere so perhaps you don't even need it?

If you want to facilitate communication between the controller and component you can follow a design pattern where both controllers are sub-classes of a master class. Using that structure you can give each a reference to the other so that they can call methods and set variables.

1
  • 1
    I've just found out that the error that i linked is caused by using the tooling api in Mavensmate, when saving using the metadata api the error is no longer given. The issue i have been having is trying to pull the choice from the selectList out of the component to be passed as a variable on the page without using a custom controller on the VF page, however looking at the link you provided this does seem like a good solution. Oct 21, 2013 at 9:15
0

On a side note to the original question:

The error below showed its head again for a colleague of mine recently when using the Dev console to save a VF page with a custom component in it.

(DynamicPicklist) common.apex.runtime.bytecode.BytecodeExecutionContext cannot be cast to common.apex.runtime.impl.Execution (Line: 1)

The error sometimes appears when saving pages using the Tooling API, to get around this you can switch to saving the file with the Metadata API or edit / save the page directly in the org.

I hope this helps anyone else encountering this error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.