1

I was originally building lists using dynamic query which works great, like so:

extension:

// initialize lead variables and columns
string leadFields = [SELECT lead_fields__c FROM demandramp_abm_config__c WHERE Name = 'Master' LIMIT 1].lead_fields__c; 
leadCols = leadFields.split(',');
leadList = Database.query('SELECT id,' + leadFields + ' FROM Lead');

vf page:

<apex:pageBlockTable value="{!leadList}" var="rec" id="leadTable">
   <apex:repeat value="{!leadCols}" var="FieldLabel">
      <apex:column value="{!rec[FieldLabel]}"/>
   </apex:repeat>
</apex:pageBlockTable>

I have run into collection size issues and now need to convert this to use a map but I'm having trouble and receiving error:

new extension:

 // initialize lead variables and columns
 string leadFields = [SELECT lead_fields__c FROM demandramp_abm_config__c WHERE Name = 'Master' LIMIT 1].lead_fields__c; 
leadCols = leadFields.split(',');
LeadMap = new Map<id,lead>((List<lead>)Database.query('SELECT id,' + leadFields + ' FROM Lead'));

new vf page:

<apex:pageBlockTable value="{!leadMap}" var="rec" id="leadTable">
   <apex:repeat value="{!leadCols}" var="FieldLabel">
      <apex:column value="{!rec[FieldLabel]}"/>
   </apex:repeat>
</apex:pageBlockTable>

The error:

vf error

Do I need to turn the list of column headers (leadCols) into a Map? How do I go about doing this and match it up with the columns?

1
  • @sfdcfox, I'm only getting an error from this: Content cannot be displayed: Map key 00Q3600000eSPoWEAW not found in map. I went though that doc earlier and got my map to work in this way referencing the field name but it's obviously not dynamic: <apex:column value="{!leadMap[recId].Company}"/>
    – jdallas
    Commented Sep 6, 2017 at 4:44

1 Answer 1

6

When you iterate over a map, you're actually getting the key, not the value. That means you need to access the value from record map:

<apex:pageBlockTable value="{!leadMap}" var="key" id="leadTable">
   <apex:repeat value="{!leadCols}" var="FieldLabel">
      <apex:column value="{!leadMap[key][FieldLabel]}"/>
   </apex:repeat>
</apex:pageBlockTable>

You can read more at Referencing Apex Maps and Lists.


Edit: After mocking this in my developer org, it looks like there's a bug referencing fields dynamically from a map. Using a normal record list appears to be the only way to get this to work:

public class q191106 {
    public SObject[] records { get; set; }
    public String[] fields { get; set; }
    public q191106() {
        fields = new String[] { 'Name', 'Industry' };
        records = Database.query('SELECT Id,'+String.join(fields,',')+' FROM Account LIMIT 10');
    }
}

<apex:page controller="q191106">
    <apex:pageBlock>
        <apex:pageBlockTable value="{!records}" var="record">
            <apex:repeat value="{!fields}" var="field">
                <apex:column value="{!record[field]}" />
            </apex:repeat>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

You can use the Map internally in your controller, but you must iterate over a record list to access fields dynamically.

1
  • 1
    You are really fast. I was just posting the same :-/ :P Commented Sep 6, 2017 at 4:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .