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
Steven Wellman 28Steven Wellman 28 

Help Bulkifying a Class

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
			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;

	            // 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'; 

	            // 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;

Bryan Leaman 6Bryan Leaman 6
Is your dupeCheck method only being called when leads are inserted? Or will it be called again when you update the leads (update leadsToUpdate) ? Recursion could be a contributing factor.

Otherwise it looks properly bulkified, so the problem may just be that there are too many other trigger functions on the lead or referral_log objects.
Sanjay Bhati 95Sanjay Bhati 95
Hi Steven,

Please check your custom object Referral_Log__c  trigger. It may be causing SQOL limit.
topdigital topdigitaltopdigital topdigital
The college’s on-line Digital Marketing Certificate Program is designed for professionals who need to beautify their website performance, create on-line advertising campaigns, social media marketing courses in bangalore and gather analytic capabilities and people who will like to improve of their advertising career.