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
MaheemSamMaheemSam 

Bulk fix on apex code

Hi, 

 I wrote a code in line 62 code is covering the bulk issue please let me know how to fix this issue. 

 
public class CtapAssessmentTriggerUtils { 
  
   
    public static void processInsert(List<CTAP_Assessment__c> newLst) {
        Set<String> emailSet = new Set<String>();
        Set<String> partnerSet = new Set<String>();
        Lead l = new lead();
        
        Map<String, Id> mapEmailToConId = new Map<String, Id>();
        Map<String, Id> mapEmailToLeadId = new Map<String, Id>();
        List<Lead> newLeadLst = new List<Lead>();
        Map<String, CTAP_Assessment__c> newLeadforCtapMap = new Map<String, CTAP_Assessment__c> ();
         
        // collect emails in a set
        for(CTAP_Assessment__c ctap : newLst) {
            emailSet.add(ctap.Contact_Email__c);
            partnerSet.add(ctap.Partner_Account__c);
            system.debug('ctap.Contact_Email__c ' + ctap.Contact_Email__c);
        }
        // removing nulls
        emailSet.remove(null);
        
        system.debug('emailSet '  + emailSet);
        
        if(!emailSet.isEmpty()) {
            for(Contact objCon : [select id,email from contact where email IN :emailSet]){
                mapEmailToConId.put(objCon.Email, objCon.Id);
            }
            
            for(Lead objLead: [select id,email from Lead where email IN :emailSet]){
                mapEmailToLeadId.put(objLead.Email, objLead.Id);
            }
        }
        
         Account[] partnerActs = [select ownerid,owner.name from account where id = :partnerSet limit 1]; 
         
        // asssign based on map key match with email
        for(CTAP_Assessment__c ctap : newLst){
            if( mapEmailToConId.get(ctap.Contact_Email__c) != null){
              ctap.Contact__c = mapEmailToConId.get(ctap.Contact_Email__c);
              ctap.Lead__c = null;
             }else if ( mapEmailToLeadId.get(ctap.Contact_Email__c) != null) {
              ctap.Lead__c = mapEmailToLeadId.get(ctap.Contact_Email__c);
              ctap.Contact__c = null;
              }
              else {         
                  // Create a new lead         
                  l.Company = ctap.End_Customer_Name__c;
                  l.FirstName = ctap.Contact_First_Name__c; 
                  l.LastName = ctap.Contact_Last_Name__c; 
                  l.Email = ctap.Contact_Email__c;
                  l.Phone = ctap.Phone__c;
                  l.Title = ctap.Title__c;
                  l.Industry = ctap.Industry__c;
                  l.LeadSource = ctap.Lead_Source__c;
                  l.Country = ctap.End_Customer_Country__c;
                  l.State = ctap.state__c;
                  l.Postalcode = ctap.postalcode__c;
                  l.Employee_Size__c = ctap.Employee_Size__c;   
                  
                  if(ctap.Partner_Account__c !=  null && ( ctap.End_Customer_Country__c != 'USA') ){ //Here it checks only for country USA
                    l.Ownerid =  partnerActs[0].ownerid;
                  }  
                                              
                 if(ctap.Contact_Email__c <> null && 
                    ctap.End_Customer_Country__c <> null &&
                    ctap.End_Customer_Name__c <> null &&
                    ctap.Contact_First_Name__c <> null ) {                                                    
                   newLeadLst.add(l);
                  }
                  
                 newLeadforCtapMap.put(ctap.Contact_Email__c, ctap);
             
          } 
        }
        
           if ( !newLeadLst.isEmpty() ){
                insert newLeadLst;
     
                      /* Run lead assignment rule.
                         Database.DMLOptions dmo = new Database.DMLOptions();
                         dmo.assignmentRuleHeader.useDefaultRule = true;
                         Database.update(newLeadLst, dmo); 
                        */
                    
                 for(Lead lead : newLeadLst){
                    CTAP_Assessment__c ctap = newLeadforCtapMap.get(lead.Email);
                    ctap.Lead__c = lead.id; //Assign new lead to lead
                 }
            }
    }
     
    public static void processUpdate(Map<id,CTAP_Assessment__c> newMap, Map<id,CTAP_Assessment__c> oldMap) {
        List<CTAP_Assessment__c> changedLst = new List<CTAP_Assessment__c>();
        for(CTAP_Assessment__c ctap : newMap.values()){
            CTAP_Assessment__c oldCtap = oldMap.get(ctap.id);
            //Compare with if condiction 
           // if(ctap.Contact_Email__c != oldCtap.Contact_Email__c) {// compare other required fields here.
              if(ctap.Contact_Email__c != null){
                changedLst.add(ctap);
            }
        
            if(!changedLst.isEmpty())
                processInsert(changedLst);
        }
    }
    
  
    
  
}

 
Wilfredo Morillo 20Wilfredo Morillo 20
Declare the new lead inside the for. 
public class CtapAssessmentTriggerUtils { 
  
   
    public static void processInsert(List<CTAP_Assessment__c> newLst) {
        Set<String> emailSet = new Set<String>();
        Set<String> partnerSet = new Set<String>();
      //  Lead l = new lead(); moved inside the for.
        
        Map<String, Id> mapEmailToConId = new Map<String, Id>();
        Map<String, Id> mapEmailToLeadId = new Map<String, Id>();
        List<Lead> newLeadLst = new List<Lead>();
        Map<String, CTAP_Assessment__c> newLeadforCtapMap = new Map<String, CTAP_Assessment__c> ();
         
        // collect emails in a set
        for(CTAP_Assessment__c ctap : newLst) {
            emailSet.add(ctap.Contact_Email__c);
            partnerSet.add(ctap.Partner_Account__c);
            system.debug('ctap.Contact_Email__c ' + ctap.Contact_Email__c);
        }
        // removing nulls
        emailSet.remove(null);
        
        system.debug('emailSet '  + emailSet);
        
        if(!emailSet.isEmpty()) {
            for(Contact objCon : [select id,email from contact where email IN :emailSet]){
                mapEmailToConId.put(objCon.Email, objCon.Id);
            }
            
            for(Lead objLead: [select id,email from Lead where email IN :emailSet]){
                mapEmailToLeadId.put(objLead.Email, objLead.Id);
            }
        }
        
         Account[] partnerActs = [select ownerid,owner.name from account where id = :partnerSet limit 1]; 
         
        // asssign based on map key match with email
        for(CTAP_Assessment__c ctap : newLst){
         //Moved from line 7
            Lead l = new lead();

            if( mapEmailToConId.get(ctap.Contact_Email__c) != null){
              ctap.Contact__c = mapEmailToConId.get(ctap.Contact_Email__c);
              ctap.Lead__c = null;
             }else if ( mapEmailToLeadId.get(ctap.Contact_Email__c) != null) {
              ctap.Lead__c = mapEmailToLeadId.get(ctap.Contact_Email__c);
              ctap.Contact__c = null;
              }
              else {         
                  // Create a new lead         
                  l.Company = ctap.End_Customer_Name__c;
                  l.FirstName = ctap.Contact_First_Name__c; 
                  l.LastName = ctap.Contact_Last_Name__c; 
                  l.Email = ctap.Contact_Email__c;
                  l.Phone = ctap.Phone__c;
                  l.Title = ctap.Title__c;
                  l.Industry = ctap.Industry__c;
                  l.LeadSource = ctap.Lead_Source__c;
                  l.Country = ctap.End_Customer_Country__c;
                  l.State = ctap.state__c;
                  l.Postalcode = ctap.postalcode__c;
                  l.Employee_Size__c = ctap.Employee_Size__c;   
                  
                  if(ctap.Partner_Account__c !=  null && ( ctap.End_Customer_Country__c != 'USA') ){ //Here it checks only for country USA
                   <b> l.Ownerid =  partnerActs[0].ownerid;</b>
                  }  
                                              
                 if(ctap.Contact_Email__c <> null && 
                    ctap.End_Customer_Country__c <> null &&
                    ctap.End_Customer_Name__c <> null &&
                    ctap.Contact_First_Name__c <> null ) {                                                    
                   newLeadLst.add(l);
                  }
                  
                 newLeadforCtapMap.put(ctap.Contact_Email__c, ctap);
             
          } 
        }
        
           if ( !newLeadLst.isEmpty() ){
                insert newLeadLst;
     
                      /* Run lead assignment rule.
                         Database.DMLOptions dmo = new Database.DMLOptions();
                         dmo.assignmentRuleHeader.useDefaultRule = true;
                         Database.update(newLeadLst, dmo); 
                        */
                    
                 for(Lead lead : newLeadLst){
                    CTAP_Assessment__c ctap = newLeadforCtapMap.get(lead.Email);
                    ctap.Lead__c = lead.id; //Assign new lead to lead
                 }
            }
    }
     
    public static void processUpdate(Map<id,CTAP_Assessment__c> newMap, Map<id,CTAP_Assessment__c> oldMap) {
        List<CTAP_Assessment__c> changedLst = new List<CTAP_Assessment__c>();
        for(CTAP_Assessment__c ctap : newMap.values()){
            CTAP_Assessment__c oldCtap = oldMap.get(ctap.id);
            //Compare with if condiction 
           // if(ctap.Contact_Email__c != oldCtap.Contact_Email__c) {// compare other required fields here.
              if(ctap.Contact_Email__c != null){
                changedLst.add(ctap);
            }
        
            if(!changedLst.isEmpty())
                processInsert(changedLst);
        }
    }
    
  
    
  
}