• Mohsin Khan 37
  • NEWBIE
  • 10 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
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;
    }
}