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>  

Thursday, 3 May 2012

Inline scripting in Vf pages

If you want to get id of any element and set certain property to it you can do that by inline scripting. Below is the code snippet which is getting inputtext id and set the disabled property to it.
 <apex:inputText id="testid" style="width:40pt;text-align:center;" styleClass="testclass"></apex:inputText>  
 <script>document.getElementById('{!$Component.testid}').disabled = true; </script>  

Future Methods in Salesforce

Future annotation is used to identify methods that are executed asynchronously. When you specify future, the method executes when Salesforce has available resources.

For example, you can use the future annotation when making an asynchronous Web service callout to an external service. Without the annotation, the Web service callout is made from the same thread that is executing the Apex code, and no additional processing can occur until the callout is complete (synchronous processing).

 global class MyFutureClass {  
 @future   
   public static void myMethod() {  
     EmailClass.SendEmailNotification();  
     //do callout, other long running code  
   }  
 }  
Email Class
 global class EmailClass{  
   WebService static void SendEmailNotification() {  
     //create a mail object to send a single email.  
     Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();  
     //set the email properties  
     mail.setToAddresses(new string[] {'wahib.idris@xyz.com'});  
     mail.setSenderDisplayName('SF.com Email Agent');  
     mail.setSubject('A new reminder');  
     mail.setHtmlBody('Password');  
     //send the email  
     Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail } );  
   }  
 }  

Points to Remember about future method :

  1. No more than 10 method calls per Apex invocation.
  2. The specified parameters must be primitive data types, arrays of primitive data types, or collections of   primitive data types.
  3. Methods with the future annotation cannot take sObjects or objects as arguments.
  4. Methods with the future annotation cannot be used in Visualforce controllers in either getMethodName or setMethodName methods, nor in the constructor.
  5. You cannot call a method annotated with future from a method that also has the future annotation. Nor can you call a trigger from an annotated method that calls another annotated method.
  6. @future(callout = true) means that the method has ability to invoke external web services.

Wednesday, 25 April 2012

Timezone of Apex Data Loader


When we load data through apex data loader and timezone of dataloader is not set to GMT then salesforce shows one day less than it should be in all of date fields.

If your timezone is not set to GMT, its pretty simple just go into the settings of Apex Loader and change the timezone to GMT and your date fields will show as you want to.

Happy Data Loading !! :)

Thursday, 15 December 2011

Auto Number Field Formatting


If you want to append date in auto number field. See examples below:

Value – {00} {MM} {DD} {YYYY} : Value – 03 12 02 2013
PO # {0} {MM}-{DD}-{YYYY} : PO #12233 12 20 2013