+ Start a Discussion
Bharat.SharmaBharat.Sharma 

Trigger is Properly Working but stuck in Code Coverage please help me

I am looking for help to Create a test class for my trigger.
when leads enter in database and associate with a camapign,i want to add that campaign as a campaigninfluence (opportunity related list) record in opportunity when lead is converted. Number of campaign may be more then one.
like..
A lead associate with  campaign 'A' to the time of creation and get added with one or more campaign after the creation so we have multiple campaign in "Campaign history" . when lead convert into a opportunity , i want to add  that campaigns as campaign influence in Opportunity  related list (Campaign influence).
first campaign
first campaign of lead should have 100% influence and rest of them must have 0% influence.
Trigger--
trigger Add_as_Campaigninfluence on Lead ( After update)  {
    map <Opportunity,Id> OppsAndContacts = new map <Opportunity,Id>(); 
    Campaigninfluence[] C = new list<Campaigninfluence>();
    //List <CampaignMember> cmm =[SELECT CampaignId FROM CampaignMember WHERE ContactId = :l.convertedcontactid ORDER BY CreatedDate ASC NULLS FIRST];
    Integer i=0;
    for (Lead l : Trigger.new) {
        If(l.IsConverted){ 
            Opportunity Opp = [SELECT Id from Opportunity where Id = :l.convertedopportunityid];
            OppsAndContacts.put(Opp, l.ConvertedContactID);
        }
        List <CampaignMember> cmm =[SELECT CampaignId FROM CampaignMember WHERE ContactId = :l.convertedcontactid ORDER BY CreatedDate ASC NULLS FIRST];
        for(CampaignMember ii:cmm)
        {
            if(i==0)
            {
                Campaigninfluence ci =new Campaigninfluence();
                ci.CampaignId =ii.CampaignId;
                ci.ContactId=l.convertedcontactid;
                ci.OpportunityId=l.convertedopportunityid;
                ci.ModelId='03V6F000000LIvWUAW';
                ci.Influence=100;
                i++;
                c.add(ci);
                
            }
            
            else{
                Campaigninfluence ci1 =new Campaigninfluence();
                ci1.CampaignId =ii.CampaignId;
                ci1.ContactId=l.convertedcontactid;
                ci1.OpportunityId=l.convertedopportunityid;
                ci1.ModelId='03V6F000000LIvWUAW';
                ci1.Influence=0;
                c.add(ci1);
                
            }
        }
    }
    insert c;
}
 Test Class - -
@isTest(SeeAllData=true)
public classAdd_as_Campaigninfluence_test { 
    
    public static testmethod void unitTest(){
        Lead leadObj1 = new lead(lastname = 'Rahul',company='goggle');
       Add_as_Campaigninfluence_test objC = new Add_as_Campaigninfluence_test();
        insert leadObj1;
        
        Lead get=[select company from Lead where lastname='Rahul'];
        get.LastName='testing';
        update get;
        
        database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(leadObj1.Id);
        LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
        lc.setConvertedStatus(convertStatus.MasterLabel);
        
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.assert(lcr.isSuccess());
        if(leadObj1.IsConverted){
            Campaigninfluence li =new Campaigninfluence();
            li.CampaignId = '7016F000001519d';
            li.ContactId = leadObj1.ConvertedContactId;
            li.Influence = 100;
            li.OpportunityId = leadObj1.ConvertedOpportunityId;
            li.ModelId = '03V6F000000LIvWUAW';
            insert li;
            
        }
        if(leadObj1.IsConverted){
            Campaigninfluence li =new Campaigninfluence();
            li.CampaignId = '7016F000001519d';
            li.ContactId = leadObj1.ConvertedContactId;
            li.Influence = 0;
            li.OpportunityId = leadObj1.ConvertedOpportunityId;
            li.ModelId = '03V6F000000LIvWUAW';
            insert li;
        }
        
    }
}
Any help will/would be appreciated :)