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
Integration 39Integration 39 

Unable to deloy inbound change set due to "Code Coverage Error"

Hello,
I am trying to write a simple trigger. It has been tested in the sandbox and all looks good.

I have put the trigger and test below. Both are in the change set.

What am i doing wrong?

Trigger
trigger CreateEnrolment on Opportunity (after insert, after update) {
List<enrolment__c> createenrolment = new List <enrolment__c> ();
    
for (Opportunity o: trigger.new) {
if (o.isWon == true) {
createenrolment.add(
new enrolment__c(
Campaign_Medium__c = o.Campaign_Medium__c,
Campaign_Name__c = o.Campaign_Name__c,
Product__c = o.Product__c,
Opportunity__c = o.id,
Account__c = o.AccountId,
Call_Centre__c = o.Call_Centre__c,
Who_Enrolled__c = UserInfo.getUserId()));
}
}
 
try {
insert createenrolment;
}
catch (Exception Ex){
system.debug(Ex);
}
}

Test
@isTest(SeeAllData=false)
private class Testcreateenrolment{
 
 
@isTest(SeeAllData=false)
private static void Testcreateenrolment() {
 
List<enrolment__c> createenrolment = new List <enrolment__c> ();
 
createenrolment.add(
new enrolment__c(
Campaign_Medium__c = 'Campaign_Medium__c',
Campaign_Name__c = 'Campaign_Name__c',
Product__c = '01t90000004q3PH',
Account__c = '00190000014B4x7',
Call_Centre__c = 'Call_Centre__c',
Who_Enrolled__c = '00590000001Symm'));
 
try {
insert createenrolment;
}
catch (Exception Ex){
system.debug(Ex);
}
}
}
 
Best Answer chosen by Integration 39
Nishant SharmaNishant Sharma
Since trigger is on Opportunity, you have to insert/update Opportunity record to make above trigger cover in test coverage.

All Answers

Nishant SharmaNishant Sharma
Since trigger is on Opportunity, you have to insert/update Opportunity record to make above trigger cover in test coverage.
This was selected as the best answer
Integration 39Integration 39
What if it is only on update?
Nishant SharmaNishant Sharma
Either insert or update should solve your problem.
Integration 39Integration 39
Sorry Nishant Sharma, it is possible to show me an example, i am not to sur ewhat you mean
Nishant SharmaNishant Sharma
Something like this:

You have to insert Opportunity record in test method
 
@isTest(SeeAllData=false)
private class Testcreateenrolment{
 
	@isTest(SeeAllData=false)
	private static void Testcreateenrolment() {
		
		// I am just including field, which is required. You can add/remove based on your need.
		insert new Opportunity(Campaign_Medium__c = 'Test Medium',Campaign_Name__c = 'Test Name');
	}	
}

 
Integration 39Integration 39
thank you, tests are now passing