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
Rick MacGuiganRick MacGuigan 

How render apex column based on query results as true or false

I do not want to show a column in a data table if the respective field for all records returned in a query are empty . 
For example, if the field PAID_LOSS__c in the result set is empty how can I render that column false ? 

I render the page block if the entire result set is empty but need to also test each discrete field in order to not render a particular column within the data table. 
<apex:pageBlockSection columns="1" id="section2c" title="* Expiring Contract Experience" showHeader="true" rendered="{!NOT(!PriorYRcontractSectionList.empty)}" >
 
 
             <apex:outputPanel id="out2c">
                <apex:actionstatus startText="loading...">
                    <apex:facet name="stop" >
                        <apex:outputPanel >
                                                                                
                             <apex:dataTable style="text-align:right;" value="{!PriorYRcontractSectionList}" var="pyr" rules="all" cellpadding="5"  >
                                <apex:column value="{!pyr.Name}" headerValue="Contracts"/>
                                <apex:column value="{!pyr.ULTIMATE_PREMIUM__c}" headerValue="Ult Prem"/>                       
                                <apex:column value="{!pyr.PAID_LOSS__c}" headerValue="Ttl Rprd Loss" />  
                                 <apex:column value="{!pyr.ACR__c}" headerValue="Ttl ACR"/>  
                                <apex:column value="{!pyr.ULTIMATE_LOSS__c}" headerValue="Ttl Ult Loss Incl ACR"/>                               
                                <apex:column value="{!pyr.Ultimate_Loss_Ratio__c}" headerValue="Ult LR%"/>  
                                <apex:column value="{!pyr.F_DETAIL_COMMISSION__c}" headerValue="Ult Comm%"/>  
                                <apex:column value="{!pyr.F_DETAIL_BROKERAGE__c}" headerValue="Ult Brok%"/>     
                                <apex:column value="{!pyr.ULT_OVERHEAD_EXCL_CATS__c}" headerValue="Ult OH%"/>
                                <apex:column value="{!pyr.ULT_CR_ACCT_OH__c}" headerValue="Ult CR%"/> 
                                <apex:column value="{!pyr.ULT_CR_ACCT_EXCL_OH__c}" headerValue="Ult CR x OH%"/>                               
                                <apex:column value="{!pyr.ITD_CR_EXCL_ACCT_OH__c}" headerValue="ITD CR x OH%"/>                                                              
                            </apex:dataTable>
                            
                       </apex:outputPanel>
                    </apex:facet>
                </apex:actionstatus>
            </apex:outputPanel>
 
 
 </apex:pageBlockSection>
 
 
 public List<MR_Contracts__c> BoundcontractQSSummaryList {get;set;}
 this.BoundcontractQSSummaryList = queryBoundQScontractsummaryById(contractNumbers, this.ContractYear);   
  
	   
  private List<MR_Contracts__c> queryBoundQScontractsummaryById(List<String> contractIds, string ContractYear) {
      return [
      SELECT
        Id
        ,Name
        ,Renewal_Date__c
        ,Effective_Date__c
        ,SPD_RTC_CODE__c 
        ,SUBJECT_PREMIUM_PS__c
        ,QUOTA_SHARE_PART_OF__c
        ,Limit__c
        ,Event_Limit__c
        ,Participation__c
        ,Estimated_Premium_PS__c
        ,Maiden_Re_Limit__c
        ,Attachment_Type__c
        ,RSO_LAE_TYPE__c
        ,RSO_ECO_PCNT__c
        ,RSO_XPL_PCNT__c
        ,F_DETAIL_COMMISSION__c
        ,PROFIT_COMM_RATE__c
        ,PROFIT_COMM_PNCT__c
        ,Ceding_Comm_Min__c
        ,Ceding_Comm_Prov__c
        ,Ceding_Comm_Max__c    
        ,CEDING_COMM_INT__c
        ,CEDING_COMM_MIN_LR__c
        ,CEDING_COMM_PROV_LR__c
        ,CEDING_COMM_MAX_LR__c
        ,CEDING_COMM_INT_LR__c
        ,BROKERAGE__c
        ,CAT_LOAD_PCT__c
        ,PRICED_ULT_OVERHEAD_EXCL_CATS__c
        ,ROE_COMBINED_TARGET__c
        ,COMBINED_RATIO__c
        ,Priced_C_R_Excl_OH__c
        ,TARGET_ROE__c
        ,ROE__c
  
       FROM
        MR_Contracts__c
      WHERE
        ContractSubmissionRef__c IN :contractIds
      AND
        SPD_RTC_CODE__c LIKE '%QS%'   
      ORDER BY Name DESC
    ];
  }

 
Best Answer chosen by Rick MacGuigan
UC InnovationUC Innovation
I appologize. You will need to declare allNulls in the following way
 
String allNulls {get; set;}

and add this line just before begining to loop through the list of MRAccounts
 
allNulls = false;

for (MR_Contracts__c MRContracts : BoundcontractQSSummaryList) {
    if (MRContracts.Event_Limit__c  != null) {
        allNulls = false;
    }
}

Hope this helps!

AM

All Answers

UC InnovationUC Innovation
Hi Rick,

My suggestion is add this rendered logic to the column for PAID_LOSS__c:
 
<apex:column value="{!pyr.PAID_LOSS__c}" headerValue="Ttl Rprd Loss" rendered="{!!allNulls}"/>

This will render the column if at least one of the values for field is not null (populated). Then in the controller add logic to determine if this variable is true or false. Something like this:
 
public Boolean allNulls = true;

verify that the list actually doesnt contain only null values for this field right after you query for your data set. You can loop through the returned list of MR_Contracts__c to determine this instead of returning it right away.
private List<MR_Contracts__c> queryBoundQScontractsummaryById(List<String> contractIds, string ContractYear) {
    List<MR_Contracts__c> MRContractsList = [SELECT Id,
                                                    Name,
                                                    //... All other fields you queried for here
                                             FROM MR_Contracts__c 
                                             WHERE ContractSubmissionRef__c IN :contractIds AND 
                                                   SPD_RTC_CODE__c LIKE '%QS%' 
                                             ORDER BY Name DESC];

    for (MR_Contracts__c MRContracts : MRContractsList ) {
        if (MRContracts.PAID_LOSS__c  != null) {
            allNulls = false;
        }
    }

    return MRContractsList;
}

This should get you the behavior you are looking for.

Hope this helps!

AM

 
Rick MacGuiganRick MacGuigan
Thanks AM for the response. I used your suggestion in the following way. Currently I am calling the queries thru assignment . Was trying to maintain this approach and loop thru the results but getting an error on the VF page. Will I need to change the way I'm calling the queries by instantiating the list inside the query ? Using my approach I'm getting an error. your thoughts woudl be appreciated. 

The controller saves but when I add the tag rendering I get this error: 
   Error: Unknown property 'Account_Summary__cStandardController.allNulls'

<apex:column value="{!bqs.Event_Limit__c}" headerValue="Event Limit" rendered="{!allNulls}" />


public Boolean allNulls = true;
.....
this.BoundcontractQSSummaryList = queryBoundQScontractsummaryById(contractNumbers, this.ContractYear);   

      for (MR_Contracts__c MRContracts : BoundcontractQSSummaryList) {
                if (MRContracts.Event_Limit__c  != null) {
            allNulls = false;
        }
    }       

//******************* query ********************************


  private List<MR_Contracts__c> queryBoundQScontractsummaryById(List<String> contractIds, string ContractYear) {

    return [
      SELECT
        Id
        ,Name  
        ,Event_Limit__c
        ,Participation__c  
       FROM
        MR_Contracts__c
      WHERE
        ContractSubmissionRef__c IN :contractIds
      AND
        SPD_RTC_CODE__c LIKE '%QS%'   
      ORDER BY Name DESC
    ];
  }
UC InnovationUC Innovation
I appologize. You will need to declare allNulls in the following way
 
String allNulls {get; set;}

and add this line just before begining to loop through the list of MRAccounts
 
allNulls = false;

for (MR_Contracts__c MRContracts : BoundcontractQSSummaryList) {
    if (MRContracts.Event_Limit__c  != null) {
        allNulls = false;
    }
}

Hope this helps!

AM
This was selected as the best answer
Rick MacGuiganRick MacGuigan
Thanks Ken. Here is what I wound up doing. 
Some columns may have a null or “0” values so I tested out both. The trick is to assume rendering is false unless I find at least one record that has a value in the column . In this case rendering for the entire column gets set to true. If I have to do this for every table and column can I just manually add the field names to a map and loop thru it to get each field name and append a unique value to allNulls for applicable coumn rendering ? (i.e  allNulls_8_1, allNulls_8_2,…) 

<apex:column value="{!bcs.ROE__c}" headerValue="ROE" rendered="{!allNulls_8_1}" /> 
<apex:column value="{!bcs.ADD__c}" headerValue="ROE" rendered="{!allNulls_8_2}" /> 
 
public boolean AllNulls {get; set;}
	  ......
      this.BoundcontractSummaryList = queryBoundcontractsummaryById(contractNumbers, this.ContractYear);  
	  
	  this.allNulls = false;  //always reset before looping. Assume there are no values in a column initially.
      for (MR_Contracts__c MRContracts : BoundcontractSummaryList) {     
                if (MRContracts.ANNUAL_AGGREGATE__c > 0) {allNulls = true;}
                //if (MRContracts.AAD__c != null) {allNulls = true;}  //  testing diff scenarios. 
				} 
	...       
	private List<MR_Contracts__c> queryBoundcontractsummaryById(List<String> contractIds, string ContractYear) {   
    return [
      SELECT
        Id
        ,ANNUAL_AGGREGATE__c
        ,AAD__c
		// more fields..... 
       FROM
        MR_Contracts__c
      WHERE
        ContractSubmissionRef__c IN :contractIds
      AND
        SPD_RTC_CODE__c LIKE '%X%' 
      ORDER BY Name DESC
    ];
    }