You need to sign in to do that
Don't have an account?
travis.truett
Test Class Not Working
I wrote a trigger that automatically adds a revenue schedule to an opportunity product when that opp product is created.I need to release it into my company's salesforce instance, but cannot for the life of me figure out how testing works in order to get my code coverage up to the standard. Here's my test class as of now:
@isTest
private class testEditSchedule{
static testMethod void testUpdateSchedule() {
List<Account> aList = new List<Account>();
Account a = new Account(
Name = 'Test Account'
);
aList.add(a);
insert aList;
List<Opportunity> oList = new List<Opportunity>();
Opportunity o = new Opportunity(
Name = 'Test Opportunity',
StageName = 'Discovery',
CloseDate = Date.today(),
AccountId = a.Id
);
oList.add(o);
insert oList;
List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
OpportunityLineItem oli = new OpportunityLineItem(
OpportunityId = o.Id,
PricebookEntryId = [select id from PricebookEntry where product2.name='Onboarding' LIMIT 1].id,
TotalPrice = 1
);
oliList.add(oli);
insert oliList;
System.assertEquals(True, oli.HasRevenueSchedule);
}
}//end of test class
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Create_followup: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject Trigger.Create_followup: line 3, column 1: []
When I run my test, I get the above exception, although I imagine this is not the only issue with my test. I would really appreciate some help figuring out how to correctly test a trigger. I can also provice the code for my trigger if need be.
@isTest
private class testEditSchedule{
static testMethod void testUpdateSchedule() {
List<Account> aList = new List<Account>();
Account a = new Account(
Name = 'Test Account'
);
aList.add(a);
insert aList;
List<Opportunity> oList = new List<Opportunity>();
Opportunity o = new Opportunity(
Name = 'Test Opportunity',
StageName = 'Discovery',
CloseDate = Date.today(),
AccountId = a.Id
);
oList.add(o);
insert oList;
List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
OpportunityLineItem oli = new OpportunityLineItem(
OpportunityId = o.Id,
PricebookEntryId = [select id from PricebookEntry where product2.name='Onboarding' LIMIT 1].id,
TotalPrice = 1
);
oliList.add(oli);
insert oliList;
System.assertEquals(True, oli.HasRevenueSchedule);
}
}//end of test class
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Create_followup: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject Trigger.Create_followup: line 3, column 1: []
When I run my test, I get the above exception, although I imagine this is not the only issue with my test. I would really appreciate some help figuring out how to correctly test a trigger. I can also provice the code for my trigger if need be.
trigger Create_followup on Opportunity (before update, after insert) {
Pricebook2 standardBook = [SELECT Id FROM Pricebook2 WHERE Name = :'Ambition'];//Create an instance of the standard pricebook
if(Trigger.isUpdate){
List<Opportunity> listOppor = new List<Opportunity>();
for (Opportunity o: Trigger.new){
if (o.StageName == 'Closed Won' && o.Stage_Change__c == false && o.Is_Clone__c == false){
Opportunity oppNew = o.clone();
oppNew.Name = oppNew.Name + ' - Annual ' + o.CloseDate.year();
if(o.Renewal_Date__c != null){
oppNew.Renewal_Date__c = o.Renewal_Date__c.addYears(1);
oppNew.CloseDate = o.Renewal_Date__c.addYears(1);}
oppNew.StageName = 'Discovery';
oppNew.Probability = 25;
oppNew.Pricebook2Id = standardBook.Id;//associate the standard pricebook with this opportunity
oppNew.Is_Clone__c = true;
listOppor.add(oppNew);
o.Stage_Change__c = true;
}
}//end of for loop
if(listOppor.size() > 0){
insert listOppor;
}
}
if(trigger.isInsert){
try{
OpportunityLineItem[] lines = new OpportunityLineItem[0];
PricebookEntry entry = [SELECT Id, UnitPrice FROM PricebookEntry WHERE Pricebook2Id = :standardBook.Id AND Product2.ProductCode = 'ENTERPRISE_ANNUAL_UPFRONT'];
List<Event> eventList = new List<Event>();
List<Note> noteList = new List<Note>();
for(Opportunity o: Trigger.new){
if(o.Is_Clone__c == true){
noteList.add(new Note(ParentId=o.id,Title='Matt is the Apex_God',Body='Matt is the Apex_God',isPrivate=false));
lines.add(new OpportunityLineItem(PricebookEntryId=entry.Id, OpportunityId=o.Id, UnitPrice=entry.UnitPrice, Quantity=1));
if(o.Renewal_Date__c != null){
DateTime myDateTime = o.Renewal_Date__c.addMonths(-10);
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime,subject='Account Management Follow-Up', EndDateTime=myDateTime, IsAllDayEvent=true));
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(2),subject='Account Management Follow-Up', EndDateTime=myDateTime.addMonths(2), IsAllDayEvent=true));
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(4),subject='Account Management Follow-Up', EndDateTime=myDateTime.addMonths(4), IsAllDayEvent=true));
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(6),subject='Account Management Follow-Up', EndDateTime=myDateTime.addMonths(6), IsAllDayEvent=true));
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(8),subject='Account Management Follow-Up', EndDateTime=myDateTime.addMonths(8), IsAllDayEvent=true));
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(10),subject='Account Management Follow-Up', EndDateTime=myDateTime.addMonths(10), IsAllDayEvent=true));
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(10),subject='Renewal',EndDateTime=myDateTime.addMonths(10), IsAllDayEvent=true));
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(9),subject='Sales Contract Follow-Up',EndDateTime=myDateTime.addMonths(9), IsAllDayEvent=true));
}//end of if
}
}
insert lines;
insert eventList;
insert noteList;
}
catch(Exception e){