function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Heena.Heena. 

I don't think there is need of visual force page for this requirement. Please can anyone of you tell me how to achieve by using custom button and apex class and don't use standard conversion method.

 
<--------------------------------------------------------------------------------- APEX CODE ------------------------------------------------------------------>

public class confirmButtonCtrl {
  
    public confirmButtonCtrl(){
        
    }
    
    public void converLeadMethod(){
       string recId=ApexPages.currentPage().getParameters().get('id');
      lead l=[select id,status from lead where id=:recId ];
        if(l.status!='Ready to Convert'){
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error,'You cant covert lead until the status is Ready to Convert');
            ApexPages.addMessage(msg);
        }else{
            Database.LeadConvert lc = new database.LeadConvert();
            lc.setLeadId(l.Id);
            lc.setDoNotCreateOpportunity(false);
            LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
            lc.setConvertedStatus(convertStatus.MasterLabel);
          
            Database.LeadConvertResult lcr = Database.convertLead(lc);
            if(lcr.isSuccess()){
                ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Confirm,'Lead Converted Successfully');
                ApexPages.addMessage(msg);
            }else{
                String[] messages = new String[0];
                for(Database.Error error: lcr.getErrors()) {
                    messages.add(error.getMessage());
                }
              
               
                    ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error,messages[0]);
                    ApexPages.addMessage(msg);
                
                
            }
            
           
            
           
        } 
    }

}




<------------------------------------------------------------------------------------------ VISUALFORCE PAGE --------------------------------------------------------------------------------->


<apex:page Controller="confirmButtonCtrl" action="{!converLeadMethod}">
    <apex:pageBlock >                                            
    <apex:pageMessages id="msgId"/>
</apex:pageBlock>
</apex:page>
Khan AnasKhan Anas (Salesforce Developers) 
Hi Heena,

I trust you are doing very well.

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page standardController="Lead" action="{!convertLead}" extensions="LeadConvertC">
</apex:page>

Controller:
public class LeadConvertC {
    
    public Id leadId;
    public Id convertedAccountId;
    
    public LeadConvertC(ApexPages.StandardController stdController){
        leadId = ApexPages.CurrentPage().getParameters().get('id');
    }
    
    //This will convert the Lead and redirect the user into newly created Account's detail page
    public PageReference convertLead(){
        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];            
        Database.LeadConvert lc = new Database.LeadConvert();
        lc.setLeadId(leadId);
        lc.setConvertedStatus(convertStatus.MasterLabel);
        
        lc.setDoNotCreateOpportunity(true);
        try{
            Database.LeadConvertResult lcr = Database.convertLead(lc);
            convertedAccountId = lcr.getAccountId();    
        }
        catch(Exception e){
            System.Debug('Error - ControllerLeadConvertView.convertLead - Exception [' + e.getMessage() + ']');
            return null;
        }
        PageReference retPage = new PageReference('/' + convertedAccountId); 
        retPage.setRedirect(true);
        
        return retPage;
    }       
}

Create a custom button on a Lead object of Content source type URL.
Set the URL as /apex/LeadConvert?id={!Lead.Id}

Here LeadConvert is the name of a Visualforce page (It is a blank page which redirect the user to conversion process).


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas