You need to sign in to do that
Don't have an account?
Bango
Simple Test Class Question
Hi there,
I wrote a a trigger to add a product to the opportunity, it works fine but I'm stuck at the test class which is giving me low code coverage, I need help with the test class. Please see the trigger/test class below, thanks in advance.
trigger AddOppLines on Opportunity (after update,after insert)
{
List<OpportunityLineItem> OppLineItems = new List<OpportunityLineItem>();
for (Opportunity newOpp: Trigger.New)
{
Integer C110013 = 1;
for(PriceBookEntry a : [SELECT Id, ProductCode FROM PriceBookEntry WHERE PriceBook2.Name='Bulk TV 1'])
{
if (a.ProductCode == '110013' && C110013 > 0)
{
OppLineItems.add(new OpportunityLineItem(OpportunityId = newOpp.Id,PricebookEntryId = a.Id,Quantity = C110013, UnitPrice = 0,Auto__c = TRUE));
}
}
}
insert OppLineItems;
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------
@isTest
private class AddOppLinesTestClass
{
static testMethod void validateAddOppLines()
{
PriceBook2 pb = new PriceBook2(Name='Bulk TV 1',IsActive=true);
insert pb;
Product2 p = new Product2(Name='TrigTest',IsActive=TRUE,ProductCode='110013',Description='TriggerTest');
insert p;
PricebookEntry pbe = new PricebookEntry(Product2Id = p.Id,PriceBook2Id = pb.Id,IsActive = true,UnitPrice = 20.00);
Opportunity b = new Opportunity(Name='Test 1',StageName='New',CloseDate=Date.today());
insert b;
}
}
I wrote a a trigger to add a product to the opportunity, it works fine but I'm stuck at the test class which is giving me low code coverage, I need help with the test class. Please see the trigger/test class below, thanks in advance.
trigger AddOppLines on Opportunity (after update,after insert)
{
List<OpportunityLineItem> OppLineItems = new List<OpportunityLineItem>();
for (Opportunity newOpp: Trigger.New)
{
Integer C110013 = 1;
for(PriceBookEntry a : [SELECT Id, ProductCode FROM PriceBookEntry WHERE PriceBook2.Name='Bulk TV 1'])
{
if (a.ProductCode == '110013' && C110013 > 0)
{
OppLineItems.add(new OpportunityLineItem(OpportunityId = newOpp.Id,PricebookEntryId = a.Id,Quantity = C110013, UnitPrice = 0,Auto__c = TRUE));
}
}
}
insert OppLineItems;
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------
@isTest
private class AddOppLinesTestClass
{
static testMethod void validateAddOppLines()
{
PriceBook2 pb = new PriceBook2(Name='Bulk TV 1',IsActive=true);
insert pb;
Product2 p = new Product2(Name='TrigTest',IsActive=TRUE,ProductCode='110013',Description='TriggerTest');
insert p;
PricebookEntry pbe = new PricebookEntry(Product2Id = p.Id,PriceBook2Id = pb.Id,IsActive = true,UnitPrice = 20.00);
Opportunity b = new Opportunity(Name='Test 1',StageName='New',CloseDate=Date.today());
insert b;
}
}
System.DmlException: Insert failed. First exception on row 0; first error: STANDARD_PRICE_NOT_DEFINED, No standard price defined for this product: []