+ Start a Discussion
DevSFDevSF 

Change Fields on the VF Page Depending on picklist Values

Hello Everyone,

 I Have a scenario :

I am creating a visualforce page on Invoice custom object, and a custom button which generates the PDF of the detail page.
and I have a Picklist field called Currency of values (USD,RIAL).

Now there is a change in taxes for these two currencies. and Tax will be calculated on the detail  page itself.

So, My task is when the picklist field is changed to USD then the tax fields related to USD should be displayed on the VF page.
and the same with the RIAL when picklist is changed to RIAL then the taxes related to RIAL should be displayed on the VF page.
and  There should be only one button on the detail page to achieve this.

Do anyone has an Idea how to achieve this scenario.

Any help would be appreciated....!
Anand_VAnand_V
I suggest that you have a pageBlock for displaying the Tax related fields alone. And if it suits your business scenario, default the Currency picklist to USD so that you can display the USD related fields on the page(on load). If you don't want to default it to anything...You can set both USD & Related fields to NOT be rendered.
For simplicity I have the boolean values hard coded here but you can control it from Controller.
    <apex:pageblock title="TAX Related">
        //Section to display USD related tax fields.
<apex:pageblockSection title="USD Tax Fields" columns="3" rendered=”True”>
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="USD-A"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="USD-B"/>
            </apex:pageBlockSectionItem>           
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="USD-C"/>
            </apex:pageBlockSectionItem>           
        </apex:pageblockSection>
//Below is NOT rendered as this section is for RIAL
        <apex:pageblockSection title="RIAL Tax fields" columns="3" rendered="False">
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="RIAL-A"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="RIAL-B"/>
            </apex:pageBlockSectionItem>           
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="RIAL-C"/>
            </apex:pageBlockSectionItem>           
        </apex:pageblockSection>       
     </apex:pageBlock>

 
DevSFDevSF
Thank alot Anand.

But i have a question for you. Actually I am generating an invoice PDF which changes depending on the Currency field.
So how to make changes in my code where i could hide fields depending on the currency picklist.
 
DevSFDevSF
@Anand .
I am Sharing my VF page and Controller. Do Mention any changes to be made.
I mean where should i define the  picklist criteria (Currency__c) (USD,RIAL).





<apex:page standardController="Invoice__c" extensions="InvoiceController"  showHeader="false" sidebar="false" renderAs="pdf"  standardStylesheets="false">
  <apex:form >
  <table width="100%">
          <tr width="100%">
            <td width="50%" align="left">
              <apex:image url="{!$Resource.lgo}" width="160" height="50" /> 
                  <br/><div style="font-size:14px;Font-family:Verdana,Arial;"><b>Company Name</b><br/>
                 Company Address
                  </div>
            <td width="30%" align="left" style="vertical-align:text-top;"><br></br><br></br>
            
                <apex:panelGrid columns="2" width="100%" title="Invoice" style=""> <br></br><br></br>
                <apex:image url="{!$Resource.logo}" width="80" height="40" />
                 <apex:outputLabel />
                <apex:outputLabel value="Invoice" style="font-size:25px;Font-family:Verdana,Arial;"/> 
                <apex:outputLabel />  <apex:outputLabel /> <apex:outputLabel />  <apex:outputLabel />
                <apex:outputLabel />  <apex:outputLabel /><apex:outputLabel />  <apex:outputLabel />
                      <apex:outputLabel value="Date:" style="font-size:14px;Font-family:Verdana,Arial;"/>
                     
                     <apex:outputLabel dir="LTR" value="{!InvoiceDate}"/>
                     <apex:outputLabel value="Invoice No.:" style="font-size:14px;Font-family:Verdana,Arial;font-weight:bold;"/>
                       <apex:outputLabel value="{!invoiceData.Invoice_Number__c}" style="font-size:15px;Font-family:Verdana,Arial;font-weight:bold;"/>
                      
                     <apex:outputLabel value="Due Date:" style="font-size:14px;Font-family:Verdana,Arial;"/>
                      <apex:outputLabel value="{!invoiceDueDate}"/>
                      
                      <apex:outputLabel value="Account Id:" style="font-size:14px;Font-family:Verdana,Arial;"/>
                      <apex:outputLabel value="{!invoiceData.Invoice_Number__c}" style="font-size:15px;Font-family:Verdana,Arial;font-weight:bold;"/>
                </apex:panelGrid>
               </td>
               </td>
             </tr>
         <td width="50%" align="left">                 
                             
              <br/>
               <div style="font-size:15px;Font-family:Verdana,Arial;">
                 <b>{!invoiceData.Company_Name__c} 
                 {!invoiceData.Billing_City__c}
                 {!invoiceData.Billing_Country__c}
                 </b><br/>
                 <b>{!invoiceData.Customer_First_Name__c} {!invoiceData.Customer_Last_Name__c}</b><br/>
                 <apex:outputText value="{!customerAddress}" escape="false"/>
                 <apex:outputLabel value="{!if(invoiceData.Customer_Phone__c ==null,'','Phone')}" >
                 </apex:outputLabel>
                 
                 <apex:outputText label="Phone" value="{!invoiceData.Customer_Phone__c}"/>
                 
                 <br/>              
               </div>   
        
       </td>
      
     <tr>
         
         <td> 
           <br/>
           
             
               <table cellpadding="2" cellspacing="0" style="width:100%;">
                    <tr style="font-size:16px;height:32px;Font-family:Verdana,Arial;" >  

                        <td style="width:7%;background-color:#707070;border-left:1px solid;border-top:1px solid;border-right:none;border-bottom:1px solid;text-align:center;">Quantity</td>
                        <td style="width:15%;background-color:#707070;border-left:1px solid;border-top:1px solid;border-right:1px solid;border-bottom:1px solid;text-align:center;">Item</td>                        
                        <td style="width:27%;background-color:#707070;border-left:none;border-top:1px solid;border-right:1px solid;border-bottom:1px solid;text-align:center;">Description</td> 
                        <td style="width:12%;background-color:#707070;border-left:none;border-top:1px solid;border-right:1px solid;border-bottom:1px solid;text-align:center;">Unit Price</td>
                        <td style="width:12%;;background-color:#707070;border-left:none;border-top:1px solid;border-right:1px solid;border-bottom:1px solid;text-align:center;">Total</td>   

                    </tr> 
                    <apex:repeat value="{!retriveInvoiceInfo}" var="inv" id="theRepeat"> 
                
                    <tr style="font-size:16px;height:300px;vertical-align:text-top;Font-family:Verdana,Arial;"> 
                        <td style="border-left:1px solid;border-top:none;border-right:none;border-bottom:none;text-align:center;"><apex:outputText value="{!inv.Product_Name__c}"/></td>
                        <td style="border-left:1px solid;border-top:none;border-right:1px solid;border-bottom:none;text-align:center;"><apex:outputText value="${!unitprice}" rendered="{!IF(unitprice==NUll || unitprice==0,false,true)}"/></td> 
                        <td style="border-left:none;border-top:none;border-right:1px solid;border-bottom:none;text-align:center;"><apex:outputText value="{!inv.Quantity__c}"/></td>           
                        <td style="border-left:none;border-top:none;border-right:1px solid;border-bottom:none;text-align:center;"><apex:outputText value="{!orderDiscount}" /></td> 
                        <td style="border-left:0.1px solid;border-top:none;border-right:1px solid;border-bottom:none;text-align:center;"><apex:outputText value="${!inv.Order_Total__c}" rendered="{!IF(inv.Order_Total__c==NUll || inv.Order_Total__c==0,false,true)}"/></td> 
                    </tr> 
                        
                    <tr style="font-size:16px;height:18px;Font-family:Verdana,Arial;">
                        <td style="border-left:none;border-top:1px solid;border-right:none;border-bottom:none"></td>                        
                        <td style="border-left:none;border-top:1px solid;border-right:none;border-bottom:none"> </td> 
                        <td style="border-left:none;border-top:1px solid;border-right:1px solid;border-bottom:none"> </td>           
                        <td style="border-left:none;border-top:1px solid;border-right:1px solid;border-bottom:1px solid;text-align:right;" >Subtotal</td> 
                        <td style="border-left:none;border-top:1px solid;border-right:1px solid;border-bottom:1px solid;text-align:center;"><apex:outputText value="${!inv.Order_Total__c}" rendered="{!IF(inv.Order_Total__c==NUll || inv.Order_Total__c==0,false,true)}"/></td> 
                    </tr>
                   
                   <tr style="font-size:14px;height:14px;Font-family:Verdana,Arial;">
                        <td style="border-left:none;border-top:none;border-right:none;border-bottom:none"></td>                         
                        <td style="border-left:none;border-top:none;border-right:none;border-bottom:none"></td> 
                        <td style="border-left:none;border-top:none;border-right:1px solid;border-bottom:none"></td>             
                        <td style="border-left:none;border-top:none;border-right:1px solid;border-bottom:1px solid;text-align:right;">ServiceTax 14%</td> 
                        <td style="border-left:none;border-top:none;border-right:1px solid;border-bottom:1px solid;text-align:center;"><apex:outputText value="${!inv.Order_Total__c}" rendered="{!IF(inv.Order_Total__c==NUll || inv.Order_Total__c==0,false,true)}"/></td> 
                    </tr>
                        
                        <tr style="font-size:14px;height:14px;Font-family:Verdana,Arial;">
                        <td style="border-left:none;border-top:none;border-right:none;border-bottom:none"></td>                         
                        <td style="border-left:none;border-top:none;border-right:none;border-bottom:none"></td> 
                        <td style="border-left:none;border-top:none;border-right:1px solid;border-bottom:none"></td>             
                        <td style="border-left:none;border-top:none;border-right:1px solid;border-bottom:1px solid;text-align:right;">Swachh BharatCess 0.5%</td> 
                        <td style="border-left:none;border-top:none;border-right:1px solid;border-bottom:1px solid;text-align:center;"><apex:outputText value="${!inv.Order_Total__c}" rendered="{!IF(inv.Order_Total__c==NUll || inv.Order_Total__c==0,false,true)}"/></td> 
                    </tr>
                        
                    <tr style="font-size:14px;height:14px;Font-family:Verdana,Arial;">
                        <td style="border-left:none;border-top:none;border-right:none;border-bottom:none"></td>                         
                        <td style="border-left:none;border-top:none;border-right:none;border-bottom:none"> </td> 
                        <td style="border-left:none;border-top:none;border-right:1px solid;border-bottom:none"></td>             
                        <td style="border-left:none;border-top:none;border-right:1px solid;border-bottom:1px solid;text-align:right;"><b>Total Amount</b></td> 
                        <td style="border-left:none;border-top:none;border-right:1px solid;border-bottom:1px solid;text-align:center;"><apex:outputText value="${!inv.Order_Total__c}" rendered="{!IF(inv.Order_Total__c==NUll || inv.Order_Total__c==0,false,true)}"/></td> 
                    </tr>
                        
                    <tr style="font-size:14px;height:14px;Font-family:Verdana,Arial;">
                        <td style="border-left:none;border-top:none;border-right:none;border-bottom:none"></td>                         
                        <td style="border-left:none;border-top:none;border-right:none;border-bottom:none"> </td> 
                        <td style="border-left:none;border-top:none;border-right:1px solid;border-bottom:none"></td>             
                        <td style="border-left:none;border-top:none;border-right:1px solid;border-bottom:1px solid;text-align:right;"><b>Paid</b></td> 
                        <td style="border-left:none;border-top:none;border-right:1px solid;border-bottom:1px solid;text-align:center;"><apex:outputText value="${!inv.Order_Total__c}" rendered="{!IF(inv.Order_Total__c==NUll || inv.Order_Total__c==0,false,true)}"/></td> 
                    </tr>
                    
                    </apex:repeat> 
                   
                </table>
                
             
        </td>
       </tr>
       <td style='padding:0px 0px 0px 10px'>
          
        <div style="font-size:14px;Font-family:Verdana,Arial;">      

            </div>
            <div style="font-size:15px;Font-family:Verdana,Arial;font-weight:bold">Please contact us for more information about payment options.</div><br/>
            <div style="font-size:15px;Font-family:Verdana,Arial;font-weight:bold;text-align:center">
                    Do quote the Invoice Number with payment.<br/>
                   
                    </div>
             <br/>
            
                    </td>
     </table>
     
  </apex:form>
</apex:page>








CONTROLLER::


public class InvoiceController {

    public InvoiceController(ApexPages.StandardController controller) {

    }
 public Invoice__c invoiceData{get;set;}
 
    public string invoiceDate{get;set;}
    public string invoiceDueDate{get;set;}
    public decimal unitprice{get;set;}
    public string orderDiscount{get;set;}
    public string customerAddress{get;set;}
    
    public Invoice__c getRetriveInvoiceInfo(){
    
        String inviceId=ApexPages.currentPage().getParameters().get('id');
        invoiceData=new Invoice__c ();
        customerAddress = '';
  invoiceData = [Select i.VAccount_Number__c,i.Billing_City__c,i.Invoice_Number__c,i.Invoice_Number__c,i.Product_Name__c,i.Quantity__c,i.Invoice_ID_of_Payment_Processor__c,i.Order_Total__c,i.Billing_Country__c,i.Customer_First_Name__c,i.Company_Name__c, i.Billing_Street__c, i.Currency__c, i.Customer_Email__c, i.Customer_Last_Name__c, i.Customer_Phone__c, i.Customer_Title__c, i.Discount__c, i.Invoice_Date__c From Invoice__c i where id=:inviceId];
        
        if(invoiceData<>null){
                     
           if(invoiceData.Order_Discount__c==NUll || invoiceData.Order_Discount__c==0){  
                orderDiscount = 'N.A.';   
            }else{
                orderDiscount = '$'+string.ValueOf(invoiceData.Order_Discount__c);
            }
            if(invoiceData.Order_Total__c!=Null && invoiceData.Order_Discount__c!=Null){
                unitprice= invoiceData.Order_Total__c + invoiceData.Order_Discount__c;
            }else if(invoiceData.Order_Total__c!=Null && invoiceData.Order_Discount__c==Null){
                unitprice= invoiceData.Order_Total__c;
            }else{
                unitprice = 0;
            }*/
        }     
      
      return invoiceData;
    }
  
}

 
Anand_VAnand_V
Hi there,

Are you trying to render multiple invoice records in a single PDF document(but then as per your query, you will get only one record at any time)? I see a repeat Statement in VF page?
Anand_VAnand_V
Here is the most basic version of what you need. I hope this helps.
VF Page
<apex:page standardController="Invoice__c" Extensions="invoiceController" renderAs="pdf">
     <apex:pageBlock rendered="{!InvoiceData.typeRIAL}" title="Rendering RIAL Tax Info">
          <apex:outputField value="{!InvoiceData.inv.Name}"/><br/>
          <apex:outputField value="{!InvoiceData.inv.RIAL_Tax_Amount__c}"/>
     </apex:pageBlock>
     <apex:pageBlock rendered="{!InvoiceData.typeUSD}" title="Rendering USD Tax Info">
          <apex:outputField value="{!InvoiceData.inv.Name}"/><br/>
          <apex:outputField value="{!InvoiceData.inv.USD_Tax_Amount__c}"/>
     </apex:pageBlock>
</apex:page>
 
Controller:
public class invoiceController{
 
    public InvoiceController(ApexPages.StandardController controller) {
 
    }
            public Invoice__c getinvoice{get;set;}
 
    public string invoiceDate{get;set;}
    public Boolean TypeUSD{get;set;}
    public Boolean TypeRIAL{get;set;}
    public invoiceWrapper getInvoiceData(){
        Id inviceId=ApexPages.currentPage().getParameters().get('id');
        invoiceWrapper invW = new invoiceWrapper();
                        invW.inv = [Select Name,Invoice_Amount__c,RIAL_Tax_Amount__c,USD_Tax_Amount__c,Currency_Type__c  from Invoice__c where
                   id = : inviceId];
        if(invW.inv.Currency_Type__c == 'USD'){
            invW.TypeUSD = True;
        }
        else{
            invW.TypeRIAL = True;
        }
           
            return invW;
    }
    public class invoiceWrapper {
        public Invoice__c inv {get; set;}
        public Boolean typeUSD {get; set;}
        public Boolean typeRIAL{get;set;}
}
}

As you can see based on the currency pick list value, I am setting which section of the VF page must be displayed. If I update invoice object with USD as currency type, It would render USD section. Same goes with RIAL.
Anand_VAnand_V
Please mark it as best answer if it helped you with what you need. Thank you!