Multiple Leads Conversion with Lightning Component

Simple code to convert multiple Leads at a time.

Apex Class 

public class LeadConversionAuraController {
    //1. The @AuraEnabled annotation enables client- and server-side access to an Apex controller method. Providing this annotation makes your methods available to your Lightning components.
    @AuraEnabled
    public static list<leadConversionWrapper> getLeadWithCheckbox(){
        //2. Performing query on lead.
        list<Lead> leadObj =[SELECT id,Name,Email,phone,company,status FROM Lead WHERE IsConverted=false];
        list<leadConversionWrapper> leadWrapper= new list<leadConversionWrapper>();
        for(Lead i:leadObj){
            //3. Adding lead in wrapper class.
            leadWrapper.add(new leadConversionWrapper(i)) ; 
        }
        //4. Returning wrapper class.
        return leadWrapper;
    }
    //5. The @AuraEnabled annotation enables client- and server-side access to an Apex controller method. Providing this annotation makes your methods available to your Lightning components.
    @AuraEnabled
    //6. This method used to convert lead.
    public static void convertSelectedLead(List<Lead> selectedLead){
        for(lead leadObj: selectedLead){
            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setLeadId(leadObj.id);
            lc.setopportunityname(leadObj.Company);
            LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
            lc.setConvertedStatus(convertStatus.MasterLabel);
            //7. Lead conversion.
            Database.LeadConvertResult lcr = Database.convertLead(lc);
        }     
    }
    // 8. Wrapper class to hold lead with checkbox.
    Public class leadConversionWrapper{
        @AuraEnabled
        Public Lead leadObj;
        @AuraEnabled
        Public Boolean isBoolean;
        public leadConversionWrapper(Lead leadObj){
            this.leadObj = leadObj;
            this.isBoolean = isBoolean;
        }
    }
    
}

 

Lightning Component 

<aura:component controller="LeadConversionAuraController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    <!-- 2.handler to call method "doInit" of jsCOntroller on loading of component. -->
    <aura:handler name="init" value="{!this}" action="{!c.leadwithcheckbox}"/>
    <!-- 1.attribute to store return value and show on component. -->
    <aura:attribute name="leadWrapper" type="LeadConversionAuraController.LeadConversionAuraController[]" />
    <br/><div class="slds-grid">
    <div class="slds-col slds-size--6-of-6">
        <center><ui:button class="detilbuton" label="Convert Lead" press="{!c.convertLead}"/></center>
    </div>
    </div><br/>
    <div class="slds-p-horizontal--small slds-size--1-of-1 slds-medium-size--1-of-1 slds-large-size--1-of-1">
        <div class="table-responsive">
            <table id="tableI" class="slds-table slds-table--bordered table" >
                <!-- Table header -->
                <thead>
                    <tr class="slds-text-title--caps header">
                        <td width="100"> Select </td>
                        <td> Name </td>
                        <td> Company </td>
                        <td> Email  </td>
                        <td> Phone </td>
                        <td> Status</td>
                    </tr>
                </thead>
                <!-- Table body -->
                <tbody>
                    <aura:iteration items="{!v.leadWrapper}" var="item">
                        <tr>
                            <td>
                                <div class="checkbox">
                                    <ui:inputCheckbox aura:id="checkbox" value="{!item.isBoolean}"/>
                                </div>
                            </td>
                            <td>{!item.leadObj.Name}</td>
                            <td>{!item.leadObj.Company}</td>
                            <td>{!item.leadObj.Email}</td>
                            <td>{!item.leadObj.Phone}</td>
                            <td>{!item.leadObj.Status}</td>
                        </tr>
                    </aura:iteration>
                </tbody>
            </table>
        </div>
    </div>
</aura:component>
({
    // 1. This is called on loding of component.
    leadwithcheckbox : function(component, event, helper) {
        //2. Calling method "getLeadWithCheckbox" of apex class "LeadConversionAuraController".
        var action =component.get("c.getLeadWithCheckbox");
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                //3.Setting return value of method "getLeadWithCheckbox" of apex class "LeadConversionAuraController" to attribute "leadWrapper".
                component.set("v.leadWrapper", response.getReturnValue());
            }
        });
        //4. $A.enqueueAction adds the server-side action to the queue.
        $A.enqueueAction(action);
    },
    // 5. This is called by Conver Lead button.
    convertLead : function(component, event, helper){
        // 6. Getting wrapper list.
        var leadWrapper = component.get("v.leadWrapper");
        //7. Calling method "convertSelectedLead" of apex class "LeadConversionAuraController".
        var action = component.get("c.convertSelectedLead");
        // 8. Array decleration.
        var selectedLead = new Array();
        for(var i=0; i < leadWrapper.length; i++){
            if(leadWrapper[i].isBoolean){
                // 9. Hold selected lead to convert.
                selectedLead.push(leadWrapper[i].leadObj);
            }
        }
        action.setParams({
            "selectedLead":selectedLead
        })
        if(selectedLead.length > 0){
            action.setCallback(this, function(response){
                // 10. Checking status of response.
                var state = response.getState();
                if (state === "SUCCESS") {
                    alert('Lead isconverted sucessfully.');
                    location.reload();
                }
                else{
                    alert('Error occurred while converting lead.');
                }
            });
        }else{
            alert('Please select any Lead');
        }
        $A.enqueueAction(action);
    }
    
})

 

MultipleLeadConversionApp

<aura:application extends="force:slds">
    <!-- Calling component "MultipleLeadConversion" -->
    <c:MultipleLeadConversion/>
</aura:application>