2

I will need to create my own "convert lead" button because I need to ensure that certain fields are updated when the button is clicked.

However, I was wondering if there is a quick way of doing this, because usually when converting a lead, we have the option of creating an opportunity, contact and account. Can I maybe create a button that, when clicked, populates the fields that I need to populate and then reroutes to the standard lead conversion process?

Tia.

1
  • Lily if either of the answers are correct, please mark them as accepted
    – Robs
    Commented Nov 20, 2018 at 13:02

2 Answers 2

3

There is a Database.leadConvert method available documented in LeadConvert Class.

So yes, you could create e.g. a JavaScript button that calls your own WebService method that does whatever population you need and then calls Database.leadConvert.

3

Of course you can. But I doubt if this is a quick way as you are looking for. This is what I have.

Create a custom button of Content source type URL under Lead object.
Set the URL as /apex/LeadConvertView?id={!Lead.Id} (LeadConvertView is just a blank page with Lead StandardController and controller extension to redirect user into real conversion page).
Have below code in LeadConvertView .

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

And have the below code in the ControllerLeadConvirtView

public class ControllerLeadConvertView {
    public Id leadId;

    public ControllerLeadConvertView(ApexPages.StandardController stdController){
        leadId = ApexPages.CurrentPage().getParameters().get('id');
        //You can retrieve the Lead object and do whatever the populating here
       //populateFields(); if you need 
    }

    public void populateFields(){
        //You can do your stuffs here may be by taking as an action of UI events
    }

    //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;
        }
        String sServerName = ApexPages.currentPage().getHeaders().get('Host');
        sServerName = 'https://'+sServerName+'/';
        PageReference retPage = new PageReference(sServerName+convertedAccountId); 
        retPage.setRedirect(true);

        return retPage;
    }       
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .