You need to sign in to do that
Don't have an account?
Mohsin 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;
}
}
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;
}
}
@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: 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.
HTH
Regards
Andrew
All Answers
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.
Best Regards,
Anthony McDougald
@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: 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.
HTH
Regards
Andrew