• Chance @ Grantham
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 1
    Likes Given
  • 2
    Questions
  • 1
    Replies
I have a best practices question for you.
We have created a child object to lead/contact called Inquiries. The inquiry is a single submission of a form or an expression of interest. This is primarily used for marketing information. Marketing has an additional request: which inquiry has the primary attribution when particular milestones are met (registration, application, etc). Their idea is that if an inquiry comes in within 30 days of another, that the second one was "influenced" by the first, and should not get attribution. 
An example: inquiries come in 90 days ago, 15 days ago, and then again yesterday. If they apply (reach a milestone) today, then the second inquiry should be the one with attribution.
What is the best way to get a lead and then get the inquiry that has primary attribution? How are others solving this question?
My understanding is that native SF reporting would not be able to handle this in report builder. Simply because we cannot iterate over the inquiries, nor can we do comparative filters (most recent inquiry, inquiries less than 30 days than that inquiry). 
So I believe that we will need to do some data manipulation on either the lead or inquiry object. I have experience with apex and am comfortable building it out. As I understand, as well, I cannot create a lookup on the lead to the inquiry, as that would create a circular reference when the inquiry is both the child and the parent of the lead.
How would you build this in your org? Would you simply perform this reporting in SQL? Would you create fields on the lead that house information from the "winning" Inquiry?
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);
            }
            
        }
    }
}

 

I want to automatically convert new leads into contacts. I have a trigger on Lead after update that will bulk convert 100 leads at a time. The problem I have  is that my marketing automation tool pushes new leads into Salesforce in batches of 200 at a time. When I try to import 200 leads, the bulk convert fails due to a too many DML 151 error.

 

I read that the convertlead function can only handle 100 records at a time. How can I edit the code to handle 200 imports at a time? Is it possible?

 

Thanks in advance.

 

trigger AutoConvert on Lead (after update) {

for(Lead myLead: Trigger.new){ if(Trigger.new[0].isConverted == false) { Database.LeadConvert lc = new database.LeadConvert(); lc.setLeadId(myLead.Id); lc.convertedStatus = 'Qualified'; //Database.ConvertLead(lc,true); lc.setDoNotCreateOpportunity(true); Database.LeadConvertResult lcr = Database.convertLead(lc); System.assert(lcr.isSuccess()); }}}

 

I want to automatically convert new leads into contacts. I have a trigger on Lead after update that will bulk convert 100 leads at a time. The problem I have  is that my marketing automation tool pushes new leads into Salesforce in batches of 200 at a time. When I try to import 200 leads, the bulk convert fails due to a too many DML 151 error.

 

I read that the convertlead function can only handle 100 records at a time. How can I edit the code to handle 200 imports at a time? Is it possible?

 

Thanks in advance.

 

trigger AutoConvert on Lead (after update) {

for(Lead myLead: Trigger.new){ if(Trigger.new[0].isConverted == false) { Database.LeadConvert lc = new database.LeadConvert(); lc.setLeadId(myLead.Id); lc.convertedStatus = 'Qualified'; //Database.ConvertLead(lc,true); lc.setDoNotCreateOpportunity(true); Database.LeadConvertResult lcr = Database.convertLead(lc); System.assert(lcr.isSuccess()); }}}