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
Chance @ GranthamChance @ Grantham 

LeadConvert Troubleshooting

How can you diagnose errors with the LeadConvert class?

I built this class out in a scratch org and it worked perfectly fine. I pushed that to a sandbox and now I cannot get it to work. I deactivated all validation rules on the lead, just in case, but I still do not get any real clues as to what the problem might be.
The logs of the Debug in Developer Console just say FATAL_ERROR System.AssertException: Assertion Failed

Here is the class:
public class leadConvert {
    public void convert(Lead[] givenLeads){
        System.debug('leadConvert.convert class running...................................................');
        
        List<Lead> noOppList = new List<Lead>();
        List<Lead> withOppList = new List<Lead>();
        Set<string> emailSet = new Set<string>();
        //set<string> phoneSet = new Set<string>();
        Set<string> accountSet = new Set<string>();
        
        //Loop through and grab information to query on
        for(lead l: givenLeads){
            if(!l.IsConverted){
                if(l.Convert_No_Opp__c){
                    noOppList.add(l);
                    if(l.Email!=null && l.Email!=''){emailSet.add(l.Email);}
                    //if(l.Phone!=null && l.Phone!=''){phoneSet.add(l.Phone);}
                    accountSet.add(l.Company);
                }
                else if(l.Convert_with_Opp__c){
                    withOppList.add(l);
                    if(l.Email!=null && l.Email!=''){emailSet.add(l.Email);}
                    //if(l.Phone!=null && l.Phone!=''){phoneSet.add(l.Phone);}
                    accountSet.add(l.Company);
                }
            }
        }
        
        System.debug('noOppList = ' + noOppList);
        System.debug('withOppList = ' + withOppList);
        
        //Query contacts to see if a contact exists that should be used as the target for conversion
        //We also have to get the account of that contact for the conversion
        List<Contact> emailMatchList = [SELECT id, lastname, email, accountid FROM Contact WHERE email IN :emailSet];
        //List<Contact> phoneMatchList = [SELECT id, lastname FROM Contact WHERE phone IN :phoneSet];        
        Map<string,id> emailMatchMap = new Map<string,id>();
        Map<id,id> contactAccountMap = new Map<id,id>();
        for(Contact c: emailMatchList){
            emailMatchMap.put(c.email, c.Id);
            contactAccountMap.put(c.id,c.AccountId);
        }
        
        //Search for accounts that match company
        List<Account> accountMatchList = [SELECT id, name FROM Account WHERE name IN :accountSet];
        Map<string,id> accountMatchMap = new Map<string,id>();
        for(Account a: accountMatchList){
            accountMatchMap.put(a.name, a.id);
        }
        
        //List used to convert
        List<Database.LeadConvert> leadConvertList = new List<Database.LeadConvert>();
        
        //Add to convert list without an opportunity
        for(lead lno: noOppList){
            Database.LeadConvert lc = new database.LeadConvert();
            lc.setLeadId(lno.Id);
            lc.convertedstatus = 'Conditionally Enrolled';
            lc.setDoNotCreateOpportunity(true); //the only difference betweeen the two for loops
            if(lno.Email!=null && lno.Email!=''){
                if(emailMatchMap.containsKey(lno.Email) && emailMatchMap.get(lno.Email)!=null){
                    id contactID = emailMatchMap.get(lno.Email);
                    if(contactAccountMap.containsKey(contactID) && contactAccountMap.get(contactID)!=null){
                        lc.setAccountId(contactAccountMap.get(contactID));
                        lc.setContactId(contactID);
                    }
                }
            }
            if(lc.accountid == null){
                if(accountMatchMap.containsKey(lno.Company) && accountMatchMap.get(lno.Company)!=null){
                    lc.setAccountId(accountMatchMap.get(lno.Company));
                }
            }
            leadConvertList.add(lc);
        }
        
        //Add to convert list with an opportunity
        for(lead lyes: withOppList){
            Database.LeadConvert lc = new database.LeadConvert();
            lc.setLeadId(lyes.Id);
            lc.convertedstatus = 'Conditionally Enrolled';
            lc.setDoNotCreateOpportunity(false); //the only difference betweeen the two for loops
            if(lyes.Email!=null && lyes.Email!=''){
                if(emailMatchMap.containsKey(lyes.Email) && emailMatchMap.get(lyes.Email)!=null){
                    id contactID = emailMatchMap.get(lyes.Email);
                    if(contactAccountMap.containsKey(contactID) && contactAccountMap.get(contactID)!=null){
                        lc.setAccountId(contactAccountMap.get(contactID));
                        lc.setContactId(contactID);
                    }
                }
            }
            if(lc.accountid == null){
                if(accountMatchMap.containsKey(lyes.Company) && accountMatchMap.get(lyes.Company)!=null){
                    lc.setAccountId(accountMatchMap.get(lyes.Company));
                }
            }
            leadConvertList.add(lc);
        }
        
        system.debug('leadConvertList = ' + leadConvertList);
        
        //update the leads
        //LeadConvert can only handle 100 records at a time
        if(!leadConvertList.isEmpty()){
            //Update Opp close date with map of data
            Map<id,id> leadOppMap = new Map<id,id>();
            List<Opportunity> updateOppList = new List<Opportunity>();
            
            //convert the leads
            for(Integer i = 0; i <= leadConvertList.size()/100 ; i++){
                list<Database.LeadConvert> tempList = new list<Database.LeadConvert>();
                Integer startIndex = i*100;
                Integer endIndex = ((startIndex+100) < leadConvertList.size()) ? startIndex+100: leadConvertList.size();
                for(Integer j=startIndex;j<endIndex;j++){
                    tempList.add(leadConvertList[j]);
                }
                system.debug('tempList conversion = ' + tempList);
                Database.LeadConvertResult[] lcrList = Database.convertLead(tempList, false);                
                for(Database.LeadConvertResult lcr : lcrList){
                    System.assert(lcr.isSuccess());
                    leadOppMap.put(lcr.getLeadId(),lcr.getOpportunityId());
                }                    
            }
            
            //after convert, update any opportunities
            for(Lead l: givenLeads){
                if(leadOppMap.containsKey(l.Id) && leadOppMap.get(l.Id)!=null){
                    updateOppList.add(new Opportunity(id = leadOppMap.get(l.Id),
                                                      //CloseDate = l.ExpectedStartDate__c,
                                                      name = l.LastName + ' New Enrollement'
                                                      //type
                                                      //record type
                                                     ));                    
                }
            }
            
            //update opportunities
            if(updateOppList.size()>0){
                System.debug('Inserting updateOppList: ' + updateOppList);
            	Database.SaveResult[] oppDML = Database.update(updateOppList,false);
            }
            
        }
    }
}

 
Raj VakatiRaj Vakati
Try this 
 
public class leadConvert {
    public void convert(Lead[] givenLeads){
        System.debug('leadConvert.convert class running...................................................');
        
        List<Lead> noOppList = new List<Lead>();
        List<Lead> withOppList = new List<Lead>();
        Set<string> emailSet = new Set<string>();
        //set<string> phoneSet = new Set<string>();
        Set<string> accountSet = new Set<string>();
        
        //Loop through and grab information to query on
        for(lead l: givenLeads){
            if(!l.IsConverted){
                if(l.Convert_No_Opp__c){
                    noOppList.add(l);
                    if(l.Email!=null && l.Email!=''){emailSet.add(l.Email);}
                    //if(l.Phone!=null && l.Phone!=''){phoneSet.add(l.Phone);}
                    accountSet.add(l.Company);
                }
                else if(l.Convert_with_Opp__c){
                    withOppList.add(l);
                    if(l.Email!=null && l.Email!=''){emailSet.add(l.Email);}
                    //if(l.Phone!=null && l.Phone!=''){phoneSet.add(l.Phone);}
                    accountSet.add(l.Company);
                }
            }
        }
        
        System.debug('noOppList = ' + noOppList);
        System.debug('withOppList = ' + withOppList);
        
        //Query contacts to see if a contact exists that should be used as the target for conversion
        //We also have to get the account of that contact for the conversion
        List<Contact> emailMatchList = [SELECT id, lastname, email, accountid FROM Contact WHERE email IN :emailSet];
        //List<Contact> phoneMatchList = [SELECT id, lastname FROM Contact WHERE phone IN :phoneSet];        
        Map<string,id> emailMatchMap = new Map<string,id>();
        Map<id,id> contactAccountMap = new Map<id,id>();
        for(Contact c: emailMatchList){
            emailMatchMap.put(c.email, c.Id);
            contactAccountMap.put(c.id,c.AccountId);
        }
        
        //Search for accounts that match company
        List<Account> accountMatchList = [SELECT id, name FROM Account WHERE name IN :accountSet];
        Map<string,id> accountMatchMap = new Map<string,id>();
        for(Account a: accountMatchList){
            accountMatchMap.put(a.name, a.id);
        }
        
        //List used to convert
        List<Database.LeadConvert> leadConvertList = new List<Database.LeadConvert>();
        
        //Add to convert list without an opportunity
        for(lead lno: noOppList){
            Database.LeadConvert lc = new database.LeadConvert();
            lc.setLeadId(lno.Id);
            lc.convertedstatus = 'Conditionally Enrolled';
            lc.setDoNotCreateOpportunity(true); //the only difference betweeen the two for loops
            if(lno.Email!=null && lno.Email!=''){
                if(emailMatchMap.containsKey(lno.Email) && emailMatchMap.get(lno.Email)!=null){
                    id contactID = emailMatchMap.get(lno.Email);
                    if(contactAccountMap.containsKey(contactID) && contactAccountMap.get(contactID)!=null){
                        lc.setAccountId(contactAccountMap.get(contactID));
                        lc.setContactId(contactID);
                    }
                }
            }
            if(lc.accountid == null){
                if(accountMatchMap.containsKey(lno.Company) && accountMatchMap.get(lno.Company)!=null){
                    lc.setAccountId(accountMatchMap.get(lno.Company));
                }
            }
            leadConvertList.add(lc);
        }
        
        //Add to convert list with an opportunity
        for(lead lyes: withOppList){
            Database.LeadConvert lc = new database.LeadConvert();
            lc.setLeadId(lyes.Id);
            lc.convertedstatus = 'Conditionally Enrolled';
            lc.setDoNotCreateOpportunity(false); //the only difference betweeen the two for loops
            if(lyes.Email!=null && lyes.Email!=''){
                if(emailMatchMap.containsKey(lyes.Email) && emailMatchMap.get(lyes.Email)!=null){
                    id contactID = emailMatchMap.get(lyes.Email);
                    if(contactAccountMap.containsKey(contactID) && contactAccountMap.get(contactID)!=null){
                        lc.setAccountId(contactAccountMap.get(contactID));
                        lc.setContactId(contactID);
                    }
                }
            }
            if(lc.accountid == null){
                if(accountMatchMap.containsKey(lyes.Company) && accountMatchMap.get(lyes.Company)!=null){
                    lc.setAccountId(accountMatchMap.get(lyes.Company));
                }
            }
            leadConvertList.add(lc);
        }
        
        system.debug('leadConvertList = ' + leadConvertList);
        
        //update the leads
        //LeadConvert can only handle 100 records at a time
        if(!leadConvertList.isEmpty()){
            //Update Opp close date with map of data
            Map<id,id> leadOppMap = new Map<id,id>();
            List<Opportunity> updateOppList = new List<Opportunity>();
            
            //convert the leads
            for(Integer i = 0; i <= leadConvertList.size()/100 ; i++){
                list<Database.LeadConvert> tempList = new list<Database.LeadConvert>();
                Integer startIndex = i*100;
                Integer endIndex = ((startIndex+100) < leadConvertList.size()) ? startIndex+100: leadConvertList.size();
                for(Integer j=startIndex;j<endIndex;j++){
                    tempList.add(leadConvertList[j]);
                }
                system.debug('tempList conversion = ' + tempList);
                Database.LeadConvertResult[] lcrList = Database.convertLead(tempList, false);                
                for(Database.LeadConvertResult lcr : lcrList){
                   // System.assert(lcr.isSuccess());
				   if(lcr.isSuccess())){
                    leadOppMap.put(lcr.getLeadId(),lcr.getOpportunityId());
				   }
                }                    
            }
            
            //after convert, update any opportunities
            for(Lead l: givenLeads){
                if(leadOppMap.containsKey(l.Id) && leadOppMap.get(l.Id)!=null){
                    updateOppList.add(new Opportunity(id = leadOppMap.get(l.Id),
                                                      //CloseDate = l.ExpectedStartDate__c,
                                                      name = l.LastName + ' New Enrollement'
                                                      //type
                                                      //record type
                                                     ));                    
                }
            }
            
            //update opportunities
            if(updateOppList.size()>0){
                System.debug('Inserting updateOppList: ' + updateOppList);
            	Database.SaveResult[] oppDML = Database.update(updateOppList,false);
            }
            
        }
    }
}