+ Start a Discussion
Mohsin Khan 37Mohsin Khan 37 

Hello All,i want to bulkify my code so that it can run over multiple records.

public class CreateCampaignfromLead {
    Public static void AddCampaignmember(list<lead>ListLead){
        list<lead>ListLd=new list<lead>();
        Id OppId;
        Date FCDate;
        Id CampId;
        list<campaignmember> cmmlist = new list<campaignmember>();
        for(Lead ld:ListLead){
            if(ld.Agent_Site_Source__c=='Re-Marketed opportunity' && ld.Referenced_Opportunity__c !=null ){
                OppId=ld.Referenced_Opportunity__c;  
            }
            List<opportunity>OppList=[select id,stagename,Future_Contact_Date__c from opportunity where 
                                      (stagename='No Solution - Not Qualified' OR 
                                       stagename='No Solution - No Experience' 
                                       OR stagename='No Solution - Age Experience' OR
                                       stagename='No Solution - Outside of quote period') AND ID=:OppId];
            
            for(opportunity Opp:OppList){
                FCDate=opp.Future_Contact_Date__c;
            }  
            if(OppList.Isempty()){
                List<opportunity>OpList=[select Id,stagename,CloseDate from opportunity where (stagename='NTU - Deal Lost' OR 
                                                                                               stagename='NTU - Too Expensive' )AND ID=:OppId];
                
                for(opportunity Opp:OpList){
                    FCDate=opp.CloseDate;
                }
            }
            list<campaign>camplist = new list<campaign>();
            camplist = [select Id , Name,Campaign_Date__c from campaign  where
                        CALENDAR_MONTH(Campaign_Date__c) = :FCDate.month() AND CALENDAR_YEAR(Campaign_Date__c) 
                        =:FCDate.YEAR()];
            for(campaign cam:camplist){
                CampId=cam.Id;
            }
            campaignmember cmm = new campaignmember();
            cmm.campaignid= CampId;
            cmm.LeadId =ld.id;
            cmmlist.add(cmm);
        }
        insert cmmlist;
    }
}
Best Answer chosen by Mohsin Khan 37
Andrew GAndrew G
Hi

@anthony
That code provided is not bulkified.  Yes, you moved the SELECT statement outside the FOR loop, but that is only part of bulkification.
If we review the code, and check this component:
Id OppId;

for(Lead ld:ListLead){
            if(ld.Agent_Site_Source__c=='Re-Marketed opportunity' && ld.Referenced_Opportunity__c !=null ){
                OppId=ld.Referenced_Opportunity__c;  
				}
		}
What will happen is that if we have multiple leads provided in the Lead List, the Id value will only hold the Oppty Id for the last Lead in the loop. 
It will be updated with each Id as we loop, but then when we go to the next SELECT statement, we will only get one Oppty returned.

With this sort of code, you will need to make use of multiple Maps.  The following should be a closer result to bulkification - not that the code is not compiled and is not complete , but should give a very good idea of how to bulkify.
public class CreateCampaignfromLead {
    Public static void AddCampaignmember(list<lead>ListLead){
        list<lead>ListLd=new list<lead>();
        Set<Id> oppIds = new List<Id>();
        Date fcDate;
        Id campId;
        list<campaignmember> cmmlist = new list<campaignmember>();
        for(Lead ld:ListLead){
            if(ld.Agent_Site_Source__c=='Re-Marketed opportunity' && ld.Referenced_Opportunity__c !=null ){
                oppIds.add(ld.Referenced_Opportunity__c);
        	}
	}
        List<opportunity>oppList=[SELECT id,stagename,Future_Contact_Date__c,CloseDate FROM opportunity WHERE
                                      (stagename='No Solution - Not Qualified' OR 
                                       stagename='No Solution - No Experience' OR
                                       stagename='No Solution - Age Experience' OR
                                       stagename='No Solution - Outside of quote period'
                                       stagename='NTU - Deal Lost' OR 
                                       stagename='NTU - Too Expensive' ) 
                                       AND Id IN :oppIds];
        
Map<Id, Opportunity> oppMap = new Map<Id,Opportunity>();
Set<Integer> monthNumbers = new Set<Integer> ();
Set<Integer> yearNumbers = new Set<Integer> ();
	if(!OppList.Isempty()){
//create a map of Oppty
              for( Opportunity opp : oppList) {
		   	oppMap.put(opp.Id, opp);
                        if(opp.stageName = 'NTU - Deal Lost'OR stagename='NTU - Too Expensive'){
                          monthNumber.add(opp.CloseDate.Month());
                          yearNumber.add(opp.CloseDate.Year());

			} else {
                          monthNumber.add(opp.Future_Contact_Date__c.Month());
                          yearNumber.add(opp.Future_Contact_Date__c.Year());
			}
              }

		list<campaign>camplist = [SELECT Id , Name, Campaign_Date__c FROM campaign  WHERE
            CALENDAR_MONTH(Campaign_Date__c) IN :monthNumber AND CALENDAR_YEAR(Campaign_Date__c) 
                  IN :yearNumber];
Map<string, campaign> campMap = new Map<String,Campaign>();
         	if(!campList.isempty()){
		    for (Campaign camp : campList ){
			campMap.put(CALENDAR_MONTH(Campaign_Date__c)+'~'+ CALENDAR_YEAR(Campaign_Date__c), camp);
}
}

for(Lead ld:ListLead){
	if(oppMap.containsKey(ld.Referenced_Opportunity__c){
            Opportunity tempOpp = oppMap.get(ld.Referenced_Opportunity__c);
Campaign tempCamp = new Campaing();
	    if(tempOpp.stageName = .......){
                   if(campMap.containsKey(opp.CloseDate.Month()+'~'+opp.CloseDate.Month()){
			tempCamp = campMap.get(opp.CloseDate.Month()+'~'+opp.CloseDate.Month());
                   }
		} else {
                   if(campMap.containsKey(opp.Future_Contact_Date__c.Month()+'~'+opp.Future_Contact_Date__c.Month()){
			tempCamp = campMap.get(opp.Future_Contact_Date__c.Month()+'~'+opp.Future_Contact_Date__c.Month());
                   }
		}

            campaignmember cmm = new campaignmember();

            cmm.campaignid= CampId;
            cmm.LeadId =ld.id;
            cmmlist.add(cmm);
	}
	}
        insert cmmlist;
}

HTH

Regards
Andrew​​​​​​​

 

All Answers

Anthony McDougaldAnthony McDougald
Hello Moshin,
Hope that your day is off to an amazing start. We've bulkified your code and hope this helps. Please feel free to contact us if you have any questions or concerns. May God bless you abundantly and have a blessed day.
public class CreateCampaignfromLead {
    Public static void AddCampaignmember(list<lead>ListLead){
        list<lead>ListLd=new list<lead>();
        Id OppId;
        Date FCDate;
        Id CampId;
        list<campaignmember> cmmlist = new list<campaignmember>();
        for(Lead ld:ListLead){
            if(ld.Agent_Site_Source__c=='Re-Marketed opportunity' && ld.Referenced_Opportunity__c !=null ){
                OppId=ld.Referenced_Opportunity__c;  
				}
		}
        
		List<opportunity>OppList=[select id,stagename,Future_Contact_Date__c from opportunity where 
                                      (stagename='No Solution - Not Qualified' OR 
                                       stagename='No Solution - No Experience' 
                                       OR stagename='No Solution - Age Experience' OR
                                       stagename='No Solution - Outside of quote period') AND ID=:OppId];
        
		if(OppList.Isempty()){
            List<opportunity>OpList=[select Id,stagename,CloseDate from opportunity where (stagename='NTU - Deal Lost' OR 
										stagename='NTU - Too Expensive' )AND ID=:OppId];
                
            for(opportunity Opp:OpList){
                FCDate=opp.CloseDate;
            }
        }else{
			for(opportunity Opp:OppList){
				FCDate=opp.Future_Contact_Date__c;
			}
		}
            
        list<campaign>camplist = [SELECT Id , Name,Campaign_Date__c FROM campaign  WHERE
            CALENDAR_MONTH(Campaign_Date__c) = :FCDate.month() AND CALENDAR_YEAR(Campaign_Date__c) 
                  =:FCDate.YEAR()];
        for(campaign cam:camplist){
                CampId=cam.Id;
        }
            campaignmember cmm = new campaignmember();
            cmm.campaignid= CampId;
            cmm.LeadId =ld.id;
            cmmlist.add(cmm);
        insert cmmlist;
    }
}

Best Regards,
Anthony McDougald
Andrew GAndrew G
Hi

@anthony
That code provided is not bulkified.  Yes, you moved the SELECT statement outside the FOR loop, but that is only part of bulkification.
If we review the code, and check this component:
Id OppId;

for(Lead ld:ListLead){
            if(ld.Agent_Site_Source__c=='Re-Marketed opportunity' && ld.Referenced_Opportunity__c !=null ){
                OppId=ld.Referenced_Opportunity__c;  
				}
		}
What will happen is that if we have multiple leads provided in the Lead List, the Id value will only hold the Oppty Id for the last Lead in the loop. 
It will be updated with each Id as we loop, but then when we go to the next SELECT statement, we will only get one Oppty returned.

With this sort of code, you will need to make use of multiple Maps.  The following should be a closer result to bulkification - not that the code is not compiled and is not complete , but should give a very good idea of how to bulkify.
public class CreateCampaignfromLead {
    Public static void AddCampaignmember(list<lead>ListLead){
        list<lead>ListLd=new list<lead>();
        Set<Id> oppIds = new List<Id>();
        Date fcDate;
        Id campId;
        list<campaignmember> cmmlist = new list<campaignmember>();
        for(Lead ld:ListLead){
            if(ld.Agent_Site_Source__c=='Re-Marketed opportunity' && ld.Referenced_Opportunity__c !=null ){
                oppIds.add(ld.Referenced_Opportunity__c);
        	}
	}
        List<opportunity>oppList=[SELECT id,stagename,Future_Contact_Date__c,CloseDate FROM opportunity WHERE
                                      (stagename='No Solution - Not Qualified' OR 
                                       stagename='No Solution - No Experience' OR
                                       stagename='No Solution - Age Experience' OR
                                       stagename='No Solution - Outside of quote period'
                                       stagename='NTU - Deal Lost' OR 
                                       stagename='NTU - Too Expensive' ) 
                                       AND Id IN :oppIds];
        
Map<Id, Opportunity> oppMap = new Map<Id,Opportunity>();
Set<Integer> monthNumbers = new Set<Integer> ();
Set<Integer> yearNumbers = new Set<Integer> ();
	if(!OppList.Isempty()){
//create a map of Oppty
              for( Opportunity opp : oppList) {
		   	oppMap.put(opp.Id, opp);
                        if(opp.stageName = 'NTU - Deal Lost'OR stagename='NTU - Too Expensive'){
                          monthNumber.add(opp.CloseDate.Month());
                          yearNumber.add(opp.CloseDate.Year());

			} else {
                          monthNumber.add(opp.Future_Contact_Date__c.Month());
                          yearNumber.add(opp.Future_Contact_Date__c.Year());
			}
              }

		list<campaign>camplist = [SELECT Id , Name, Campaign_Date__c FROM campaign  WHERE
            CALENDAR_MONTH(Campaign_Date__c) IN :monthNumber AND CALENDAR_YEAR(Campaign_Date__c) 
                  IN :yearNumber];
Map<string, campaign> campMap = new Map<String,Campaign>();
         	if(!campList.isempty()){
		    for (Campaign camp : campList ){
			campMap.put(CALENDAR_MONTH(Campaign_Date__c)+'~'+ CALENDAR_YEAR(Campaign_Date__c), camp);
}
}

for(Lead ld:ListLead){
	if(oppMap.containsKey(ld.Referenced_Opportunity__c){
            Opportunity tempOpp = oppMap.get(ld.Referenced_Opportunity__c);
Campaign tempCamp = new Campaing();
	    if(tempOpp.stageName = .......){
                   if(campMap.containsKey(opp.CloseDate.Month()+'~'+opp.CloseDate.Month()){
			tempCamp = campMap.get(opp.CloseDate.Month()+'~'+opp.CloseDate.Month());
                   }
		} else {
                   if(campMap.containsKey(opp.Future_Contact_Date__c.Month()+'~'+opp.Future_Contact_Date__c.Month()){
			tempCamp = campMap.get(opp.Future_Contact_Date__c.Month()+'~'+opp.Future_Contact_Date__c.Month());
                   }
		}

            campaignmember cmm = new campaignmember();

            cmm.campaignid= CampId;
            cmm.LeadId =ld.id;
            cmmlist.add(cmm);
	}
	}
        insert cmmlist;
}

HTH

Regards
Andrew​​​​​​​

 
This was selected as the best answer