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
thekid12345thekid12345 

How to create a trigger to automatically convert a lead which creates an account, contact, and opportunity using the LeadConvert() function ?

sfdcFanBoysfdcFanBoy
Use the below code to convert a single lead.  You can extend it to your trigger accordingly to handle multiple lead conversions
 
        Lead myLead = [SELECT Id FROM Lead WHERE FirstName = 'John'];
        Database.LeadConvert lc = new Database.LeadConvert();
        lc.setLeadId(myLead.id);

        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
        lc.setConvertedStatus(convertStatus.MasterLabel);   
        Database.LeadConvertResult lcr = Database.convertLead(lc);

        System.assert(lcr.isSuccess());