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
Rob Koch 1Rob Koch 1 

Display list of fields as checkbox choices on VF page

What I would like to do is display as many as 50 fields from a Contract detail page (i.e. Terms_and_Conditions__c) as a list in a single vertical column, next to a checkbox. Users would be able to choose the fields they want by checking the box.
So far I’m able to set the column headers in the VF page, and I’ve added a summary list class in my controller, but I’m stuck on how to generate the list of fields, and to render that list in the VF page. Any ideas?

Desired output:

 
Select:Contract Field
Terms_and_Conditions__c
Return_Provisions__c

Apex controller:
public class AccountContractPickerControllerExt{

public String currentContractRecordId {get;set;}
// public String parameterValue {get;set;}
public Contract currentContract{get;set;}

public String selectedContract {get;set;}
public String contractLeaseStatusFilter = 'Lease In force'; 

public PageReference pageRef = new PageReference('/' + 'recordID');
// public Account acc {get;set;}
//public String AccountID {get;set;}  
//public String AccountName {get;set;}

public List<Contract> contracts = new List<Contract>();

// Obtain list of Contract Fields for Build SOQL statements 
        Map<String, Schema.SObjectField> fieldMap = Contract.sObjectType.getDescribe().fields.getMap();
        Set<String> fieldNames = fieldMap.keySet();
        String soqlCurrentContract = 'Select' + string.join((Iterable<String>)fieldNames, ',') + ' From Contract Where id =: currentContractRecordID';
        String soqlSelectedContract = 'SELECT' + string.join ((Iterable<String>)fieldNames, ',') + 'FROM Contract WHERE AccountID =:currentContract.AccountID and Lease_Status__c =:contractLeaseStatusFilter ORDER BY name';

       public AccountContractPickerControllerExt(ApexPages.StandardController controller) {
        currentContractRecordId  = ApexPages.CurrentPage().getparameters().get('id');
        currentContract=[select ID, Name, AccountID from Contract where id =: currentContractRecordId];
//parameterValue = ApexPages.CurrentPage().getparameters().get('nameParam');
    } 
    
    private ApexPages.StandardSetController stdController;

    /**
     * Retrieves Contracts by the lease status set on
     * contractLeaseStatusFilter.
     * 
     * @return List of Contracts
     */
    public List<Contract> getContracts() {
        getContractsByLeaseStatus();
        return contracts;
    }

    /**
     * Executes query to retrieve Contracts by the lease status set on contractLeaseStatusFilter.
     * Retrieve the results using <code>getContracts</code>
     * This is typically called externally via JavaScript (AJAX).
     *
     * @return PageReference (null)
     */

    public PageReference getContractsByLeaseStatus() {
        // Contracts=database.query(soqlSelectedContract);
        contracts = [SELECT 
                        Name, Account.Name, 
                        ContractNumber, Lease_Status__C, 
                        Rent_Monthly_PC__C, 
                        Lease_Type__C, Cars_on_Billing__C
                    FROM Contract 
                    WHERE AccountID = :currentContract.AccountID and Lease_Status__c = :contractLeaseStatusFilter  
                    ORDER BY Name];
        return null;
    }

    public String getContractLeaseStatusFilter() {
        return contractLeaseStatusFilter;
    }

    public void setContractLeaseStatusFilter(String filter) {
        contractLeaseStatusFilter = filter;
    }

    /**
     * Generates the SelectOptions necessary to create a dropdown menu containing the
     * available Contracts.
     *
     * @return List of SelectOptions for the available Contracts
     */
    public List<SelectOption> getContractSelectOptions() {
        List<SelectOption> opts = new List<SelectOption>();
        
        for (Contract contract : getContracts()) {
            opts.add(new SelectOption(contract.Id, 
            contract.Account.Name + '   :  ' 
            + contract.Name + '  -  ' +
            + contract.Lease_Status__C + '  -  '+
             + contract.Rent_Monthly_PC__C + '  -  ' +  
            + contract.Lease_Type__C + '  -  ' 
            + Contract.Cars_on_Billing__C));
        }
        return opts;
    }
    
   /* Associates the Contracts with the Account by setting the contracts on the related Account.
    * 
    * @return PageReference to the related contracts view if the save is successful, otherwise
    *         returns the PageReference back to the ContractImport page.
    */
    
   public ApexPages.PageReference save() {
    System.debug('Entering AccountContractPickerControllerExt.save');

      Boolean hasError = false;
        
      List<Contract> contracts = (List<Contract>)stdController.getRecords();

      System.assert(contracts.isEmpty(), 'At least one contract should be passed to the controller.');

      return ApexPages.currentPage();
   }
   
   //retrieve list of Terms fields for import selection
   
    public class Summary{
            public List<String> summary_list {get; set;}

            public Summary(List<String> s){
                summary_list = s;
            }
        }

        public List<Summary> summary_list_wrap {get; set;}
        public List<String> items_in_column {get; set;}


public void build_Table(){
     //populating the data here - which is working as per the screenshot below
}  
   
   
   
   
   

}

VF page:
<apex:page standardController="Contract" extensions="AccountContractPickerControllerExt" sidebar="False" lightningStylesheets="true" docType="html-5.0">
  <apex:form >
     <script>
        function getSelectedLeaseStatus() {
            var leaseStatusSelect = document.getElementById("leaseStatusFilter");
            return leaseStatusSelect.options[leaseStatusSelect.selectedIndex].value;
        }
    </script>

    <apex:pageMessages />
    
    <apex:outputPanel layout="block" rendered="{!contract != null}">
               <apex:pageBlock title="Assign Lease to Account">
                <apex:pageBlock title="Selected Lease to Import Term To">
                    <apex:pageBlockTable value="{!contract}" var="contracts">
                        <apex:column headerValue="Lease">
                        <apex:outputField value="{!contract.Name}"/>
                        </apex:column>
                        <apex:column headerValue="Order Number">
                            <apex:outputField value="{!contract.ContractNumber}"/>
                        </apex:column>
                        <apex:column headerValue="Lease Status">
                            <apex:outputField value="{!contract.Lease_Status__c}"/>
                        </apex:column>
                        <apex:column headerValue="Account ID">
                            <apex:outputField value="{!contract.AccountID}"/>
                        </apex:column>
                        <apex:column headerValue="Start Date">
                            <apex:outputField value="{!contract.StartDate}"/>
                        </apex:column>
                        <apex:column headerValue="End Date">
                            <apex:outputField value="{!contract.EndDate}"/>
                        </apex:column>
                        <apex:column headerValue="Rent Monthly PC">
                            <apex:outputField value="{!contract.Rent_Monthly_PC__c}"/>
                        </apex:column>                    
                         <apex:column headerValue="Lease Type">
                            <apex:outputField value="{!contract.Lease_Type__c}"/>
                        </apex:column>                            
                        <apex:column headerValue="Cars On Billing">
                            <apex:outputField value="{!contract.Cars_On_Billing__c}"/>
                        </apex:column>   
 
                    </apex:pageBlockTable>
                </apex:pageBlock>

                <apex:pageBlock title="Select a Lease to Import Terms From">
                    <label for="leaseStatusFilter">Select a lease status to filter on: </label>
                    <select id="leaseStatusFilter" onchange="getContractsByLeaseStatusJS(getSelectedLeaseStatus())">
                        <option>Lease In Force</option>
                        <option>Order</option>
                        <option>Expiring-Available</option>
                        <option>Expired</option>
                        <option>Return</option>
                        <option>Closed - Renewed</option>
                        <option>Closed - Reassigned</option>
                        <option>Closed - Sold</option>
                    </select>
                    <br/>
                    <label for="contractSelector">Select a contract: </label>
                    <apex:selectList value="{!selectedContract}" size="1" id="contractSelector">
                        <apex:selectOptions value="{!contractSelectOptions}"/>
                    </apex:selectList>
                </apex:pageBlock>
                <apex:pageBlockButtons >
                    <apex:commandButton value="Save-Import Terms" action="{!save}"/>
                    <apex:commandButton value="Cancel-No Import Performed" action="{!cancel}"/>
                </apex:pageBlockButtons>
            </apex:pageBlock>
            <apex:pageBlock title="Term">
           
            <apex:pageblockTable value="{!summary_list_wrap}" var="slw">
                        <apex:column value="{!slw}"/>
                        <apex:column headerValue="Field">
                        
                        
                        
                        
                            </apex:column>
                        
                         Summary:[fieldNames (Abatement_General_Text__c,Abatement_Gen_Rep_Start_Days__c, ...)];
                         <tr > 
                        <th>Contract Name</th>
                        <td>{!contract.Abatement_General_Text__c}</td>
                        </tr>
                        <apex:repeat var="slw" value="{!contract}">
                           <tr>
                            <td>{!contract.Abatement_General_Text__c}</td>
                            </tr>
                        </apex:repeat>  
                    </apex:pageblockTable>
                   </apex:pageBlock>
                    <apex:pageBlock title="Import From">
           

           </apex:pageBlock>
            <apex:pageBlock title="Import To">
           </apex:pageBlock>
           
            <apex:actionFunction action="{!getContractsByLeaseStatus}" name="getContractsByLeaseStatusJS" reRender="contractSelector">
                <apex:param name="leaseStatus" assignTo="{!contractLeaseStatusFilter}" value="contractId"/>
            </apex:actionFunction>
        </apex:outputPanel>
  </apex:form>
</apex:page>

   Page rendering: