• topdigital topdigital
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
When a lead is created, I pass the info to 2 different classes to check for dupes. I thought it was bulkified but I can't create more than 20-30 leads at a time. I'll hit a SQOL limit if I do. Is there a different/better way to bulkify this?
public class dupeLeadChecker {
	public static void dupeCheck(List<Lead> leadFromTrigger) {
		
		// Get the Id for the data quality queue
		Group dataQualityGroup = [SELECT Id FROM Group WHERE DeveloperName = 'Data_Quality' LIMIT 1];

		// Create a map to hold the Lead and phone
		Map<String, Lead> leadList = new Map<String, Lead>();
		List<Referral_Log__c> referralInsert = new List<Referral_Log__c>();
		List<Lead> leadsToUpdate = new List<Lead>();

		// Loop through all leads in the class
		for(Lead l : leadFromTrigger){
			leadList.put(l.Phone, l);
		}

		// <----------------- Lead ----------------->
		// Check to see if there's a lead match
		List<Lead> matchedLeads = [SELECT Id, Email, Status, Phone, OwnerId, qbdialer__Dials__c, Digital_Marketing_Opt_in__c, Partner_Rep_Notes__c, Button_Referral__c
									 FROM Lead
								    WHERE Phone = :leadList.keySet() 
								      AND OwnerId != :dataQualityGroup.Id
								      AND Status != 'Converted'];

		// If there's a match, pass the lead to the dupeLeadHandler
		if(!matchedLeads.isEmpty()){
			for(Lead lead : matchedLeads){
	            // Get the new lead in the map
	            Lead leadFromPhone = leadList.get(lead.Phone);

	            // Create a log in the referral history
	            Referral_Log__c leadRef = new Referral_Log__c();

	            // Set the values for the referral record
	            leadRef.Digital_Marketing_Opt_in__c = leadFromPhone.Digital_Marketing_Opt_in__c;
	            leadRef.Partner_Rep_Name__c         = leadFromPhone.Partner_Rep_Name__c;
	            leadRef.Referral_Source__c          = leadFromPhone.LeadSource;
	            leadRef.Referring_Partner__c        = leadFromPhone.Partner__c;
	            leadRef.Partner_Rep_Notes__c        = leadFromPhone.Partner_Rep_Notes__c;
	            leadRef.Button_Referral__c          = leadFromPhone.Button_Referral__c;
	            leadRef.Lead__c                     = lead.Id;
	            
	            referralInsert.add(leadRef);

	            // If new lead is button referral, update button referral on old lead
	            if(leadFromPhone.Button_Referral__c == true){
	            	lead.Button_Referral__c = true;
	            }

	            // If new lead has opted in to digital marketing move update the old lead
	            if(leadFromPhone.Digital_Marketing_Opt_in__c == true){
	            	lead.Digital_Marketing_Opt_in__c = true;
	            }

	            // If lead has opted in, or if lead is button referral and status is drip, change to new
	            if((lead.Digital_Marketing_Opt_in__c == true || lead.Button_Referral__c == true) && lead.Status == 'Drip'){
	            	lead.Status = 'New';
	            }

	            // If new lead is from PACO, update PACO field on old lead
	            if(leadFromPhone.PACO__c == true){
	            	lead.PACO__c = true;
	            }

	            // If rep has notes in new lead, append them onto the old lead
	            if(leadFromPhone.Partner_Rep_Notes__c != null){
	            	if(lead.Partner_Rep_Notes__c == null){
	            		lead.Partner_Rep_Notes__c = leadFromPhone.Partner_Rep_Notes__c;
	            	} else {
	            		lead.Partner_Rep_Notes__c = lead.Partner_Rep_Notes__c + ' | Updated notes: ' + leadFromPhone.Partner_Rep_Notes__c;
	            	}
	            }

	            // Check to see if status = Drip
	            if (lead.Status == 'Drip' && lead.qbdialer__Dials__c > 10){
	                // Cut dials in 1/2 and update status to New
	                lead.qbdialer__Dials__c = 5;
	                lead.Status = 'New'; 
	            }
	            leadsToUpdate.add(lead);

	            // Mark new lead as a dupe
	            leadFromPhone.Dupe__c = true;
	            leadFromPhone.OwnerId = dataQualityGroup.Id;
	        }

	        // Update the old leads
	        update leadsToUpdate;
	        // insert the referral log
	        insert referralInsert;
		}
	}
}