2

I have a PDF attachment created via visualforce email template. I have a controller grabbing all quote line items, but would like to section off the line items based on a field (which I was able to do).

Now, if there are no line items that fall under the second table's criteria, I don't want to show the table's headers.

If the number of rows with

QLI.Trade_In_Discount_Grouping__c == 'Additional Discounts'

is equal to zero, then don't show the below table at all (so the headers are not just visible).

<div id="contentToToggle">    
  <table border="1" cellpadding="1" cellspacing="1" 
         style="border-collapse:collapse;border-color:black;font-family:Helvetica;">
  <tr>
    <th width="6%" style="font-size:9pt;"><b>PART #</b></th> 
    <th width="20%" style="font-size:9pt;"><b>DESCRIPTION</b></th>
    <th width="10%" style="text-align:right;font-size:9pt;"><b>DISCOUNT AMOUNT</b></th>
  </tr>   
  <apex:repeat value="{!QuoteLines}" var="QLI"> 
    <apex:variable id="tritemlines" value="" var="tritemlines" rendered="{!QLI.Trade_In_Discount_Grouping__c == 'Additional Discounts'}">  
      <tr>
        <td style="font-size:9pt;">{!QLI.SBQQ__ProductCode__c}</td> 
        <td style="font-size:9pt;">{!QLI.SBQQ__Quantity__c}</td> 
        <td >{!QLI.SBQQ__NetTotal__c}" </td> 
      </tr>    
    </apex:variable>
  </apex:repeat>
  </table>
</div>    
1

Basic idea to adjust your template sections is to use standard Visualforce tags with property rendered.

Result of that property evaluation will define if component is shown at all. In this case, it is possible to wrap entire section that needs to be hidden, into <apex:outputPanel>, and set property rendered based on condition.

In most situations, logic about what should be rendered, is calculated in controller side (in apex) In this case, example can be next:

Controller (or extension):

class NiceApexController {
  public Boolean showSection {
    get {
      List<QLI__c> all_qli = getQuoteLines();
      Boolean result = false;
      for (QLI__C qli : all_qli) {
        if (qli.Trade_In_Discount_Grouping__c == 'Additional Discounts') {
          result = true;
          break
        }
      }
      return result;
    }
    private set;
  }
  public List<QLI__c> getQuoteLines() {
    List<QLI__c> result;
    // fill result;
    return result;
  }
}

Template:

<apex:outputPanel layout="none" rendered = "{!showSection}" >
  <div id="contentToToggle">    
    <table border="1" cellpadding="1" cellspacing="1" 
           style="border-collapse:collapse;border-color:black;font-family:Helvetica;">
    <tr>
      <th width="6%" style="font-size:9pt;"><b>PART #</b></th> 
      <th width="20%" style="font-size:9pt;"><b>DESCRIPTION</b></th>
      <th width="10%" style="text-align:right;font-size:9pt;"><b>DISCOUNT AMOUNT</b></th>
    </tr>   
    <apex:repeat value="{!QuoteLines}" var="QLI"> 
      <tr>
        <td style="font-size:9pt;">{!QLI.SBQQ__ProductCode__c}</td> 
        <td style="font-size:9pt;">{!QLI.SBQQ__Quantity__c}</td> 
        <td >{!QLI.SBQQ__NetTotal__c} </td> 
      </tr>    
    </apex:repeat>
    </table>
  </div> 
</apex:outputPanel> 

(It also make sense to return already filtered list of QLI__c records directly from apex controller/extension)

In case if apex controller/extension modifications are not possible, then entire calculation can be done in pure Visualforce syntax.

Template:

<apex:variable  var="additional_discounts_count"  value="{!0}" />  

<apex:repeat var="QLI" value="{!QuoteLines}">
  <apex:variable var="additional_discounts_count" value="{!additional_discounts_count + 1}"  rendered="{!QLI.Trade_In_Discount_Grouping__c == 'Additional Discounts'}"/>
</apex:repeat>

<apex:variable var="table_empty" value="{!additional_discounts_count == 0}" />

<apex:outputPanel layout="none" rendered = "{!NOT(table_empty)}" >
  <!-- component -->
</apex:outputPanel>

Basically, in visualforce code above we are iterating over QuoteLines and increase additional_discounts_count for concrete condition only. As a result, counter can be uses to determine if entire table should be shown.

  • Controller/VF Component coding works :) – dannycheeko Apr 13 '17 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.