Custom LEX component to convert lead

Following is the solution to convert lead using custom button in lightning:

1] Create a custom LEX button.

2] From front end controller invoke Apex Class.

3] Have custom apex code to do lead conversion.

// Form list of lead records to be converted.

// Get the converted status of already converted leads.

LeadStatus convertedStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1]; 

// Fetch the related records of lead into appropriate lists of objects.

// For each leads to be converted perform following steps.

Database.LeadConvert leadconvert = new Database.LeadConvert();

leadconvert.setLeadId(currentlead);               

leadconvert.setConvertedStatus(convertedStatus.MasterLabel);  // convertedStatus.MasterLabel = Closed - Converted

leadsToConvert.add(Leadconvert);

            // leadsToConvert would be a list of all leads to be converted.

           // by pass rule for duplicate checking on creation of account;

            Database.DMLOptions dml = new Database.DMLOptions(); 

            dml.DuplicateRuleHeader.allowSave = true;

            dml.DuplicateRuleHeader.runAsCurrentUser = true;     

 

// Perform lead conversion

List<Database.LeadConvertResult> lcr = Database.convertLead(leadsToConvert,dml); // lead conversion

 

4] Associate related objects of lead to account in apex code.

for(Database.LeadConvertResult result: lcr){ 

               // Loop through related objects of lead and assign them to converted account.

                        for(Related_Object__c cr : rec1){

                            if(cr.Lead__c == result.getLeadId()){

                                cr.Account__c= result.getAccountId();

                                rec.add(cr);

                            }

                        }

                    }

update rec;


Setup VSCode with Salesforce

Step by Step guide to setup VSCode with Salesforce Install vscode Install Salesforce CLI (developer.salesforce.com/tools/sfdxcli) Perform fo...