2

In regards to my previous question, my actual implementation is to Search contacts and display in the form of Wrapper records (have a checkbox next to each contact to select a few for further processing).

I am trying to convert this List of Wrapper records to a map, so that I can overcome the 1000 rows in List collection variable for <pageBlockTable>.

Apex:

if(!contWrapperList.isEmpty()){
  contWrapperMap = new Map<Boolean, Contact>();
  for(contWrapperList conWrapItem: contWrapperList){
    conWrapperMap.put(conWrapItem.selected, conWrapItem.con);
  }
}

This new map would be of waste of time as I cannot get all the items into it. Thanks to no-duplicate-record policy from Map. So now, how can I get my list contWrapperList records into a Map, and use at <pageBlockTable>. So I am not using this map at all.

Visualforce:

<apex:pageBlockTable value="{!contWrapperList}" var="c" id="table">
  <apex:column >
    <apex:inputCheckbox value="{!c.selected}" id="inputId"/>
  </apex:column>
  <apex:column headerValue="Contact Id"> 
    <apex:outputLink value="/{!c.con.Id}"><apex:outputField value="{!c.con.Id}" /></apex:outputLink>
  </apex:column>
</apex:pageBlockTable>

1 Answer 1

2

Try this:

  public class ContactWrapper {
    public Boolean isSelected {get; set;}
    public Contact c {get; set;}

    public ContactWrapper(Boolean isSelected, Contact c) {
      this.isSelected = isSelected; this.c = c;
  }

  Map<Id,ContactWrapper> contactWrappersById = new Map<Id,ContactWrapper>();

  for (Contact c: [SELECT Id, Name, ... FROM Contact WHERE ...]) {
     contactWrappersById.put(c.Id,new ContactWrapper(false,c));
  }

VF

<apex:pageBlockTable value="{!contactWrappersById}" var="cw">
  <apex:column >
    <apex:inputCheckbox value="{!cw.isSelected}" id="inputId"/>
  </apex:column>
  <apex:column headerValue="Contact Id"> 
    <apex:outputLink value="/{!cw.c.Id}"><apex:outputField value="{!cw.c.Id}" /></apex:outputLink>
  </apex:column>
  ...
 </apex:pageBlockTable>

This is essentially the solution defined here that you already found except adapted for wrappers

That said, displaying a list with more than 1000 elements isn't the best UX and a pagination solution using standardSetControllers might be better or offloading all the work to the client side

1
  • I would definitely try this approach. I really appreciate your time Cropredy. I will update you again with my questions (or marking this as an answer) by Monday. Happy weekend to you. Commented Nov 8, 2019 at 22:56

You must log in to answer this question.

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