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

Bizarre "Invalid field" error using wrapper class! Help?

Hey elite devs!


I've used wrapper classes before, but I have to say I'm absolutely stumped why I'm having problems with a wrapper class I've implemented recently for a Visualforce page.  I'd really appreciate another set of eyes on this issue.


The class displays a list of OpportunityContactRoles attached to an Opportunity Id in the URL, and displays them with checkboxes next to them for user-selection.


Stripped down class:


public class FulfillmentEmailController {
    public List <utilityContact> opportunityContactRoleList {get; set;}
    //Returning a list of Contact Roles related to this Opp
    public List <utilityContact> getRelevantOpportunityContactRoles() { 
        if (opportunityContactRoleList == null){
           opportunityContactRoleList = new List <utilityContact> ();
           for (OpportunityContactRole eachOpportunityContactRole : [SELECT Id, ContactId, Role, Contact.Name, Contact.Email FROM OpportunityContactRole WHERE OpportunityId = :ApexPages.currentPage().getParameters().get('oppId')]){
           	opportunityContactRoleList.add(new utilityContact(eachOpportunityContactRole));
        return opportunityContactRoleList;
    public class utilityContact {
        public OpportunityContactRole oppCon {get; set;}
        public Boolean selected {get; set;}
        public utilityContact(OpportunityContactRole thisOpportunityContactRole) {
            oppCon = thisOpportunityContactRole;
            selected = false;



Visualforce example:


<apex:page controller="FulfillmentEmailController" tabStyle="Opportunity">
    <apex:form >
    	<apex:pageBlock title="Select Recipients">
            <p>Please check the contacts you would like to include.</p>
            <br />
            <apex:pageBlockTable value="{!RelevantOpportunityContactRoles}" var="c">
	            	<apex:inputCheckbox value="{!c.selected}" />
	            <apex:column value="{!c.oppCon.Contact.Name}"/>
	            <apex:column value="{!c.oppCon.Contact.Email}"/>
	            <apex:column value="{!c.oppCon.Role}"/>


I should make it clear that I've implemented code EXACTLY like this in another file-- just using different field names and such.  It works without a hitch.


However, when I try to run this, I get the following error:


     Invalid field selected for SObject Contact

     (or if I comment out the "selected" field....) Invalid field oppCon for SObject Contact


I've done some debugging on this, and it's clear the utility class is populating correctly.  If I change the Visualforce columns to simply {!c}, here's an example of what is displayed:


utilityContact:[oppCon=OpportunityContactRole:{Role=District Site Coordinator, ContactId=0035000000W0gGcAAJ, Id=00K50000009YJtFEAW}, selected=false]


This shows me that the wrapper class is working fine, and that it's correctly populating the fields I'm trying to query.


So why am I getting this error?!  Why "Contact" and not OpportunityContactRole?  Why does a different Visualforce page where I use this exact same logic (also querying OpportuntiyContactRoles) work, but this page doesn't?


Help me, elite devs!

Best Answer chosen by Admin (Salesforce Developers) 

just a wild guess.... could you try changing the name of the iteration variable from "c" to something else (something more distinctive....)

All Answers


just a wild guess.... could you try changing the name of the iteration variable from "c" to something else (something more distinctive....)

This was selected as the best answer

Oh my goodness.  That worked!


I have absolutely no clue why, though-- can you explain?


There is an issue with scoping of names that you may have run into....


Is there any documentation on the scoping issue?  I'd just like to be able to avoid this again in the future, but am not exactly sure what sorts of naming conventions to avoid.