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
orph351orph351 

Cannot get code coverage above 40% for trigger test class

I'm trying to get my test class coverage to 100%. It doesn't seem to matter what I change, because the coverage is staying at 40%. Could you please take a look and let me know what I'm missing?
trigger setOnlineDocket on Opportunity (before insert, before update) {
    
    List<Campaign> lstCampaign=[Select Id from Campaign where Name='Online Campaign'];
    if(!lstCampaign.isEmpty()){
        for(Opportunity opp:Trigger.New){
            if(opp.StageName=='Review' && opp.CampaignId!=lstCampaign[0].Id){
                opp.CampaignId=lstCampaign[0].Id;
            }
        }
    }
}




@isTest
private class setOnlineDocketTestClass {
	static testMethod void validatesetOnlineDocket() {
		// Create Test Campaign
		List<Campaign> campNew = new List<Campaign>();
		campNew.add(new Campaign(Name = 'Test Online Campaign', IsActive = True, Type = 'Online Campaign', Meeting_Date__c = System.now()));
		insert campNew;
        
        // Verify campaign inserted successfully
		List<Campaign> newCampaign = [SELECT Name FROM Campaign WHERE Id IN :campNew];
        System.assertEquals('Test Online Campaign', newCampaign.Name);
        System.Debug('Campaign Created: ' + newCampaign.Name);
		
		// Create Test Account
		Account accNew = new Account(Name='M Test Account', Letter__c = True);
		insert accNew;
		
		// Verify that the initial state is as expected.
		accNew = [SELECT Name FROM Account WHERE Id = :accNew.Id];
		System.assertEquals('M Test Account', accNew.Name);
        System.Debug('Account Created: ' + accNew.Name);
		
		// Create Test Contact
		Contact conNew = new Contact(AccountId = accNew.Id, lastname = 'Testing', firstname = 'Apex');
		insert conNew;
		
		// Verify contact inserted successfully
		conNew = [SELECT lastname FROM Contact WHERE Id = :conNew.Id];
		System.assertEquals('Testing', conNew.lastname);
        System.Debug('Contact Created: ' + conNew.Name);
		
		// Create Test Opportunity
		Opportunity oppNew = new Opportunity(RecordTypeId = '012A0000000d8jR', AccountId = accNew.Id, Name = 'Maclellan Trigger Test Audit', StageName = 'Committee Review', CloseDate = System.today(), CampaignId = null);
		
        System.Debug('Record Type: ' + oppNew.RecordType + ', AccountId: ' + oppNew.AccountId + ', Opportunity name: ' + oppNew.Name + ', Stage: ' + oppNew.StageName + ', Close Date: ' + oppNew.CloseDate + ', Campaign Id: ' + oppNew.CampaignId);
        insert oppNew;
                
        // Retrieve New Opportunity
        Opportunity oppCreated = [SELECT Name FROM Opportunity WHERE Id = :oppNew.Id];
        System.assertEquals('M Trigger Test Audit', oppCreated.Name);
        System.Debug('Opportunity Created: ' + oppNew.Name);
        
		// Update Opportunity
		oppNew.CampaignId='701G0000000vw7M';
		update oppNew;
		
		// Retrieve Updated Opportunity
		Opportunity oppUpdated = [SELECT CampaignId FROM Opportunity WHERE Id = :oppNew.Id];
		 
		// Test that the trigger updated campaign
		System.assertEquals('701G0000000vw7M', oppUpdated.CampaignId);
		System.Debug('Opportunity Updated with CampaignId: ' + oppUpdated.CampaignId);	
    }
}
Dushyant SonwarDushyant Sonwar
Hey buddy,
you also have to add a new campaign with name "Online Campaign".
Jefferson  EscobarJefferson Escobar
Hi,
you are querying a record campaing in the trigger call "Online Campaign", line 3: [Select Id from Campaign where Name='Online Campaign'];
Make sure the name of the campaing created in the test class contains the same name as declare in the query, line 21 campNew.add(new Campaign(Name = 'Test Online Campaign', IsActive = True, Type =


I hope this work!
orph351orph351
This is the section of code that still says has no coverage:

for(Opportunity opp:Trigger.New){
            if(opp.StageName=='Review' && opp.CampaignId!=lstCampaign[0].Id){
                opp.CampaignId=lstCampaign[0].Id;
            }
        }

What am I missing to provide coverage for this section?
Jefferson  EscobarJefferson Escobar
Hi,
Set the stage of the opportunity as 'Review' and Link the record newCampaign to the opportunity line 48, 

Opportunity oppNew = new Opportunity(RecordTypeId = '012A0000000d8jR', AccountId =accNew.Id, Name = 'Maclellan Trigger Test Audit', StageName = 'Review', CloseDate =System.today(), CampaignId = newCampaign.Id);

Don't forget the name for the first campaing as 'Online Campaign'
orph351orph351
I am already linking the new capaign to the opportunity on lines 58-60:

58        // Update Opportunity
59        oppNew.CampaignId='701G0000000vw7M';
60        update oppNew;

 
Jefferson  EscobarJefferson Escobar
The opportunity Id is dinamically, you should reletaed to the record of campaing you are inserting.

oppNew.CampaignId= newCampaign.Id;