You need to sign in to do that
Don't have an account?
Joshua Graham 13
Having Problems with Test Class
“I am having problems with trigger test code coverage. The following code is the original trigger:
trigger CreateImplementationCaseOnWin on Opportunity (before update) {
List<Case> CaseList = new List<Case> ();
for(Opportunity o: Trigger.new) {
String opptyId = o.AccountId;
Opportunity oldopp = trigger.OldMap.get (o.id);
if (o.isWon != oldopp.isWon)
if(o.isWon == true && o.Type == 'New Business' && o.HasOpportunityLineItem == true && o.StageName == '990 - Closed/Won' ){
Case c = new Case();
c.AccountId = o.AccountId;
c.OwnerId = o.OwnerId;
c.Subject = o.Name;
c.RecordTypeId = o.Case_Creation_RT__c;
c.OB_Signed_Contract_Closed_Won__c = o.CloseDate;
CaseList.add(c);
}
if(CaseList.size() > 0)
insert(CaseList);
} }
What I need to do is add another trigger with some tweaks to the if statement including removing o.iswon == true, changing type from 'New Business' to 'Existing Business' and changing the Stage from '90 - Closed/Won' to '08 - Contract Executed'. So...
if(o.Type == 'Existing Business' && o.HasOpportunityLineItem == true && o.StageName == '08 - Contract Executed' )
However, when I do this and use the same test code from the original trigger above....the test code coverage drops dramatically. I try to make tweaks to that test code but it drops the coverage even further. I am not sure what I am doing wrong. Below is the original test code that covers 100% of the above code. However, when I make the change to the if statement to the one in bold above, the coverage drops from 100% to 46%. I tried adding the following code after insert oll in the original test code but it didn't seem to work. I did have a question about if a custom field is set to required on the page layout when in StageName == '08 - Contract Executed, do i need to make sure those 'required' fields are in my test code? I am very new to coding as you can probably tell!
oo.StageName= '08 - Contract Executed';
oo.Type = 'Existing Business';
update oo;
Case c = new Case();
c.AccountId = aa.Id;
c.OwnerId = oo.OwnerId;
c.Subject = oo.Name;
c.RecordTypeId = oo.Case_Creation_RT__c;
c.OB_Signed_Contract_Closed_Won__c = oo.CloseDate;
insert c;
//original test code that covers 100% of the trigger at the top of this post
@isTest (SeeAllData=true)
private class Test_Class_CreateImplementationCaseOnWin {
static testMethod void CreateImplementationCaseOnWin() {
Account aa = new Account();
aa.Name = 'Test Account';
aa.Type = 'Customer';
aa.BDM__c = 'Test User';
aa.Time_Zone__c = 'Pacific';
insert aa;
Opportunity oo = new Opportunity();
oo.AccountId = aa.Id;
oo.Name = 'test';
oo.Amount = 1000;
oo.StageName = 'Prospecting';
oo.CloseDate = date.today();
insert oo;
//For production test class use 01u50000001F4l9
OpportunityLineItem oll = new OpportunityLineItem();
oll.OpportunityId = oo.Id;
oll.Quantity = 1;
oll.UnitPrice = 2.00;
oll.PricebookEntryId = [SELECT Id FROM PricebookEntry WHERE isActive = true LIMIT 1].id;
insert oll;
oo.StageName= '90 - Closed/Won';
update oo;
//Verify that a case record was created
for(Case cr : [SELECT Subject, Id FROM Case WHERE AccountId = :aa.Id LIMIT 1]){
//System.assertEquals(cr.Subject = o.Name);
}
delete oll;
delete oo;
}
}”
trigger CreateImplementationCaseOnWin on Opportunity (before update) {
List<Case> CaseList = new List<Case> ();
for(Opportunity o: Trigger.new) {
String opptyId = o.AccountId;
Opportunity oldopp = trigger.OldMap.get (o.id);
if (o.isWon != oldopp.isWon)
if(o.isWon == true && o.Type == 'New Business' && o.HasOpportunityLineItem == true && o.StageName == '990 - Closed/Won' ){
Case c = new Case();
c.AccountId = o.AccountId;
c.OwnerId = o.OwnerId;
c.Subject = o.Name;
c.RecordTypeId = o.Case_Creation_RT__c;
c.OB_Signed_Contract_Closed_Won__c = o.CloseDate;
CaseList.add(c);
}
if(CaseList.size() > 0)
insert(CaseList);
} }
What I need to do is add another trigger with some tweaks to the if statement including removing o.iswon == true, changing type from 'New Business' to 'Existing Business' and changing the Stage from '90 - Closed/Won' to '08 - Contract Executed'. So...
if(o.Type == 'Existing Business' && o.HasOpportunityLineItem == true && o.StageName == '08 - Contract Executed' )
However, when I do this and use the same test code from the original trigger above....the test code coverage drops dramatically. I try to make tweaks to that test code but it drops the coverage even further. I am not sure what I am doing wrong. Below is the original test code that covers 100% of the above code. However, when I make the change to the if statement to the one in bold above, the coverage drops from 100% to 46%. I tried adding the following code after insert oll in the original test code but it didn't seem to work. I did have a question about if a custom field is set to required on the page layout when in StageName == '08 - Contract Executed, do i need to make sure those 'required' fields are in my test code? I am very new to coding as you can probably tell!
oo.StageName= '08 - Contract Executed';
oo.Type = 'Existing Business';
update oo;
Case c = new Case();
c.AccountId = aa.Id;
c.OwnerId = oo.OwnerId;
c.Subject = oo.Name;
c.RecordTypeId = oo.Case_Creation_RT__c;
c.OB_Signed_Contract_Closed_Won__c = oo.CloseDate;
insert c;
//original test code that covers 100% of the trigger at the top of this post
@isTest (SeeAllData=true)
private class Test_Class_CreateImplementationCaseOnWin {
static testMethod void CreateImplementationCaseOnWin() {
Account aa = new Account();
aa.Name = 'Test Account';
aa.Type = 'Customer';
aa.BDM__c = 'Test User';
aa.Time_Zone__c = 'Pacific';
insert aa;
Opportunity oo = new Opportunity();
oo.AccountId = aa.Id;
oo.Name = 'test';
oo.Amount = 1000;
oo.StageName = 'Prospecting';
oo.CloseDate = date.today();
insert oo;
//For production test class use 01u50000001F4l9
OpportunityLineItem oll = new OpportunityLineItem();
oll.OpportunityId = oo.Id;
oll.Quantity = 1;
oll.UnitPrice = 2.00;
oll.PricebookEntryId = [SELECT Id FROM PricebookEntry WHERE isActive = true LIMIT 1].id;
insert oll;
oo.StageName= '90 - Closed/Won';
update oo;
//Verify that a case record was created
for(Case cr : [SELECT Subject, Id FROM Case WHERE AccountId = :aa.Id LIMIT 1]){
//System.assertEquals(cr.Subject = o.Name);
}
delete oll;
delete oo;
}
}”
Go for this post
http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html
Regards,
Abhi