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
MOHAMMED AL IMAMMOHAMMED AL IMAM 

Hi anyone please provide the test class for below code

trigger campaigntrigger on CampaignMember (after insert) { Map<id,List<opportunity>> map1 = new Map<id,List<opportunity>>(); set<id> conSet =new set<id>(); for(CampaignMember c :trigger.new ){ conSet.add(c.contactId); } for(opportunity cm: [select id,contactId,Name,StageName from opportunity where contactid in: conSet]){ List<opportunity> oppList; if(!map1.containsKey(cm.contactId)){ oppList = new List<opportunity> (); oppList.add(cm); map1.put(cm.contactId, oppList); system.debug('success' +map1); } else{ oppList = map1.get(cm.contactId); oppList.add(cm); map1.put(cm.contactId,oppList); system.debug('fail' +map1); } } List<Opportunity> oppsToUpdate = new List<Opportunity>(); for(CampaignMember cm : trigger.new){ if(cm.ContactId != NULL){ if(map1.get(cm.ContactId) != NULL){ for(Opportunity op : map1.get(cm.ContactId)){ op.CampaignId = cm.CampaignId; oppsToUpdate.add(op); } } } } if(oppsToUpdate.size() > 0){ update oppsToUpdate; } }
CharuDuttCharuDutt
Hii Mohammed Al Imam
Try Below Test Class  (100% Coverage)
@isTest
public class campaigntriggerTest {
@isTest
    public Static void UnitTest(){
        Campaign c= new Campaign();
        c.Name = 'Test';
        Insert c;
        
        Contact Con =new Contact();
        Con.LastName = 'Test Con';
        Insert Con;
        
        opportunity opp =new Opportunity();
        Opp.Name = 'TestOpp';
        Opp.StageName = 'Closed Won';
        Opp.CloseDate = system.today();
        opp.ContactId = Con.Id;
        Insert Opp;
        
        opportunity Opp1 =new Opportunity();
        Opp1.Name = 'TestOpp1';
        Opp1.StageName = 'Closed Won';
        Opp1.CloseDate = system.today();
        opp1.ContactId = Con.Id;
        Insert Opp1;

        
        CampaignMember cm = new CampaignMember();
        cm.CampaignId = c.Id;
        cm.ContactId = Con.Id;
        insert cm;    
	}
}
Please Mark it As Best Answer If It Helps
Thank You!

 
CharuDuttCharuDutt
Hii Mohammed Al Imam
Please Close Your Query By  Marking it As Best Answer If It Helps So It Helps Others In Future
Thank You!
Suraj Tripathi 47Suraj Tripathi 47
Hi,

Please follow the below code:-
@istest
public class campaigntriggerTest {
    @istest
    public static void testTrigger(){
        
        Account acc = new account();
        acc.name = 'testing';
        insert acc;
        
        Contact con = new contact();
        con.lastname = 'testCon';
        con.AccountId = acc.id;
        insert con;
        
         Contact con1 = new contact();
        con1.lastname = 'testCon1';
        con1.AccountId = acc.id;
        insert con1;
        Campaign camp = new Campaign(
            Name = 'Test',
            IsActive = TRUE
        );            
        insert camp;
       
        Opportunity op = new opportunity();
        Op.name='testOpp';
        Op.StageName='Qualified';
        Op.CloseDate = date.today();
        Op.ContactId =con1.id;
        insert op;
        
        List<Opportunity> oppList = new List<Opportunity>();
        
        For(integer i=0;i<2; i++){
        Opportunity Opp1 = new opportunity();
        Opp1.name='tetsOpp'+i;
        Opp1.StageName='Qualified';
        Opp1.CloseDate = date.today()+1+i;
        Opp1.ContactId =con.id;
        oppList.add(Opp1);
        }
        insert oppList;
        
        
        List<CampaignMember> members = new List<CampaignMember>();
        CampaignMember member = new CampaignMember(
            ContactId = con.Id,
            Status = 'sent',
            CampaignId = camp.Id
            
        ); 
        members.add(member); 
        
        CampaignMember member2 = new CampaignMember(
            ContactId = con.Id,
            Status = 'sent',
            CampaignId = camp.Id
        ); 
        
        members.add(member2); 
        insert members;
       
    }
}

Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi
Denver TimDenver Tim
U will get everything from Website. (https://healthicspro.com/)