Pages

22 November 2012

Tips on Custom Lead conversion Functionality in Salesforce

Apex gives methods to perform custom lead functionality. This is an amazing feature 
that can be leveraged to override the "convert lead" button in salesforce with a VF
page and perform intermediate validations. Here is what i have done:

VF Page action method is leveraged to perform the Validations: 
 
 <apex:page standardController="lead" cache="true" action="{!autorun}"  
 extensions="leadController" >  
 <br/><br/>  
 <apex:messages style="color:red; font-weight:bold; text-align:center;"/>  
 <apex:form >  
 <center>  
 <apex:commandLink value="Redirect to Lead" style="color:blue; font-weight:bold;"   
 action="{!RedirecttoLead}"/>  
 </center>  
 </apex:form>  
 </apex:page>  
 
Controller Code:
---------------- 
 
1:  public class leadController  
2:  {  
3:    Public lead lObj;  
4:    Public Id leadId;  
5:    public leadController(ApexPages.StandardController stdController)  
6:    {  
7:      leadId = ApexPages.currentPage().getParameters().get('id');  
8:      lObj = (leadid == null) ? new Lead():[SELECT Money_Order__c,   
9:      Money_Transfer__c from lead where id =: leadid];  
10:    }  
11:    public PageReference autoRun()  
12:    {  
13:      Database.LeadConvert lc = new database.LeadConvert();  
14:      lc.setLeadId(leadId);  
15:      LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus   
16:      where IsConverted=true limit 1];   
17:      lc.setConvertedStatus(convertStatus.MasterLabel);  
18:      if(lObj.Money_Order__c == true || lObj.Money_Transfer__c == true){  
19:      Database.LeadConvertResult lcr = Database.convertLead(lc);  
20:      System.assert(lcr.isSuccess());  
21:      Id oppId = lcr.getOpportunityId();  
22:      PageReference Page = new PageReference('/'+oppId);  
23:      Page.setRedirect(true);  
24:      return Page;  
25:      }  
26:      else{  
27:      lObj.addError('Lead cannot be converted without Product Selection !');  
28:      lObj.addError('Please Select Atleast One Product');  
29:      }  
30:      return null;  
31:    }  
32:    Public PageReference RedirecttoLead(){  
33:      PageReference Page = new PageReference('/'+leadId);  
34:      Page.setRedirect(true);  
35:      return Page;  
36:    }  
37:  }   

Hope this helps. :)

No comments:

Post a Comment