Monday, 24 June 2013

Conditioning in Visualforce Page

If you want to override Opportunity detail page and show OpportunityLineItem's detail page underneath the Opportunity page, you can do it easily by using <apex:detail>,<apex:repeat> and <apex:variable> with some conditioning in VF page.


<apex:detail> shows the standard detail page for a particular object, as defined by the associated page layout for the object in Setup. So in order to show Opportunity detail page, we can use <apex:detail> and bind VF page by Opportunity standard controller.

Use custom controller as an extension to get the related OpportunityLineItems of that particular Opportunity in list and bind it with <apex:detail>.But here is a tricky part, We have a list of OpportunityLineItems and <apex:detail> includes 'subject' attribute which takes ID of the record that should provide data to the component. To set ID of record in <apex:detail> we can give the name of the list with the element's index position in square brackets
<apex:detail subject="{!oppLineItemList[cnt]}"/>.

Iterate the list with incrementing the list index(local variable 'cnt').<apex:variable> is a local variable that can be used as a replacement for a specified expression within the body of the component.
<apex:variable var="cnt" value="{!0}"/>.

To iterate and increment local variable value we can use <apex:repeat>. It is an iteration component that allows you to output the contents of a collection according to a structure that you specify and in our case it is iterating to size of OpportunityLineItem list.
<apex:repeat value="{!oppLineItemList}">.

If we increment list index(local variable) like <apex:variable var="cnt" value="{!cnt+1}"/> then and it will throw us an error "List index out of bounds" because in last iteration list index(local variable) will be equal to list size and we know list index is zero based and it should be less than the size of the collection. To avoid this error, we can give condition that when [list index(local variable) value] is less than [one less than list size] then increment list index(local variable) otherwise keep it as it is.
value="{!IF(cnt < oppLineItemList.size-1,cnt+1,cnt)}".

Below is the code snippet of Controller and Page. 

Controller:


 public class contOppLineItem{  
  public list<OpportunityLineItem> oppLineItemList{get;set;}  
  public Opportunity opp;  
  public contOppLineItem(ApexPages.StandardController stdController){  
     //Constructor  
     this.opp = (Opportunity)stdController.getRecord();  
     oppLineItemList = [Select Id,OpportunityId From OpportunityLineItem Where OpportunityId =: opp.Id];  
  }  
 }  


VisualForce Page:


 <apex:page standardController="Opportunity" extensions="contOppLineItem">  
   <apex:detail relatedList="false" title="true"/>  
   <apex:pageBlock mode="detail">  
     <apex:pageBlockSection title="OpportunityLineItem">  
       <div align="center">  
         <apex:variable var="cnt" value="{!0}" />   
         <apex:repeat value="{!oppLineItemList}">  
              <apex:detail subject="{!oppLineItemList[cnt]}"/>    
              <apex:variable var="cnt" value="{!IF(cnt<oppLineItemList.size-1,cnt+1,cnt)}"/>  
         </apex:repeat>   
       </div>    
     </apex:pageBlockSection>  
   </apex:pageBlock>  
 </apex:page>  

No comments:

Post a Comment