You need to sign in to do that
Don't have an account?
Hi everyone am trying to write a testclass for my trigger but not getting any coverage please help me
Trigger:
trigger QuantityMOQValidate on OpportunityLineItem (after insert, after update) {
if(checkRecursive.runOnce()){
Set<Id> pbeIds = new Set<Id>();
for (OpportunityLineItem oli : Trigger.new){
pbeIds.add(oli.pricebookentryid);
system.debug('adding to pbeIds ------>' + oli.pricebookentry.id);
}
Map<Id, double> entries = new Map<Id, Decimal>();
for(PricebookEntry pbe : [select id, MOQ__c from PricebookEntry where id IN : pbeIds]){
entries.put(pbe.id, pbe.MOQ__c);
system.debug('putting in a map id------>' + pbe.id);
system.debug('putting in a map value------>' + pbe.MOQ__c);
}
list<OpportunityLineItem> upOLI = new list<OpportunityLineItem>();
for(OpportunityLineItem oli: Trigger.new){
if((oli.Quantity < entries.get(oli.pricebookEntryId)) && entries.get(oli.pricebookEntryId) != Null ){
system.debug('Quantity less than MOQ');
oli.addError('Quantity less than MOQ ['+ Integer.valueof(entries.get(oli.pricebookEntryId))+']');
}
system.debug('>>>>>>>>Quantity----->' + oli.Quantity+'>>MOQ val----->'+ entries.get(oli.pricebookEntryId));
}
}
}
Testclass:
@istest
public class TestQuantityMOQValidate {
static testMethod void myUnitTest() {
//Data Prep
//Create Account, Opportunity, Product, etc.
Account acct1 = new Account(name='test Account One1');
insert acct1;
//Create Opportunity on Account
Opportunity Oppty1 = new Opportunity(name='test Oppty One1');
Oppty1.StageName = 'Selected';
Oppty1.CloseDate = Date.today() + 30;
insert Oppty1;
// Create Products
Product2 testprod1 = new Product2 (name='test product one1');
testprod1.productcode = 'test pd code1one';
insert testprod1;
Product2 testprod2 = new Product2 (name='test product two2');
testprod2.productcode = 'test pd code2two';
insert testprod2;
// Get Pricebook
Pricebook2 testpb = [select id from Pricebook2 where IsStandard = true];
testpb.IsActive=true;
update testpb;
// Add to pricebook
PriceBookEntry pbe1 = new PricebookEntry();
pbe1.UnitPrice = 100;
pbe1.Product2Id = testprod1.Id;
pbe1.Pricebook2Id = testpb.id;
pbe1.IsActive = true;
pbe1.useStandardPrice = false;
insert pbe1;
PriceBookEntry pbe2 = new PricebookEntry();
pbe2.UnitPrice = 200;
pbe2.Product2Id = testprod2.Id;
pbe2.Pricebook2Id = testpb.id;
pbe2.IsActive = true;
pbe2.useStandardPrice = false;
insert pbe2;
Test.startTest();
Oppty1.StageName='Selected';
update Oppty1;
System.assertNotEquals('Prospecting', Oppty1.StageName);
Test.stopTest();
}
}
trigger QuantityMOQValidate on OpportunityLineItem (after insert, after update) {
if(checkRecursive.runOnce()){
Set<Id> pbeIds = new Set<Id>();
for (OpportunityLineItem oli : Trigger.new){
pbeIds.add(oli.pricebookentryid);
system.debug('adding to pbeIds ------>' + oli.pricebookentry.id);
}
Map<Id, double> entries = new Map<Id, Decimal>();
for(PricebookEntry pbe : [select id, MOQ__c from PricebookEntry where id IN : pbeIds]){
entries.put(pbe.id, pbe.MOQ__c);
system.debug('putting in a map id------>' + pbe.id);
system.debug('putting in a map value------>' + pbe.MOQ__c);
}
list<OpportunityLineItem> upOLI = new list<OpportunityLineItem>();
for(OpportunityLineItem oli: Trigger.new){
if((oli.Quantity < entries.get(oli.pricebookEntryId)) && entries.get(oli.pricebookEntryId) != Null ){
system.debug('Quantity less than MOQ');
oli.addError('Quantity less than MOQ ['+ Integer.valueof(entries.get(oli.pricebookEntryId))+']');
}
system.debug('>>>>>>>>Quantity----->' + oli.Quantity+'>>MOQ val----->'+ entries.get(oli.pricebookEntryId));
}
}
}
Testclass:
@istest
public class TestQuantityMOQValidate {
static testMethod void myUnitTest() {
//Data Prep
//Create Account, Opportunity, Product, etc.
Account acct1 = new Account(name='test Account One1');
insert acct1;
//Create Opportunity on Account
Opportunity Oppty1 = new Opportunity(name='test Oppty One1');
Oppty1.StageName = 'Selected';
Oppty1.CloseDate = Date.today() + 30;
insert Oppty1;
// Create Products
Product2 testprod1 = new Product2 (name='test product one1');
testprod1.productcode = 'test pd code1one';
insert testprod1;
Product2 testprod2 = new Product2 (name='test product two2');
testprod2.productcode = 'test pd code2two';
insert testprod2;
// Get Pricebook
Pricebook2 testpb = [select id from Pricebook2 where IsStandard = true];
testpb.IsActive=true;
update testpb;
// Add to pricebook
PriceBookEntry pbe1 = new PricebookEntry();
pbe1.UnitPrice = 100;
pbe1.Product2Id = testprod1.Id;
pbe1.Pricebook2Id = testpb.id;
pbe1.IsActive = true;
pbe1.useStandardPrice = false;
insert pbe1;
PriceBookEntry pbe2 = new PricebookEntry();
pbe2.UnitPrice = 200;
pbe2.Product2Id = testprod2.Id;
pbe2.Pricebook2Id = testpb.id;
pbe2.IsActive = true;
pbe2.useStandardPrice = false;
insert pbe2;
Test.startTest();
Oppty1.StageName='Selected';
update Oppty1;
System.assertNotEquals('Prospecting', Oppty1.StageName);
Test.stopTest();
}
}
Try this it will cover 90 % code coverage
Please assign
oppty1.name='Test';
oppty1.accountid =a.id;
insert required fiedls for opportunity and for the pricebook and opplineitem insertion follow the below code :
do the update logic below this .
Mark it as solved if it helps you,
Thanks ,
Karthik.
am trying both the codes but getting the erroe
System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, This price definition already exists in this price book: []
I think you are using of both our codes ,dont combine it either use any one snippet if you use my snippet no need of making seealldata=true in your test class.
Thanks ,
karthik
@isTest
public class TestQuantityMOQValidate {
static testMethod void TestMOQValidate() {
Account acc = new Account(Name = 'Test Account');
insert acc;
Pricebook2 pbk1 = new Pricebook2 (Name='Test Pricebook Entry 1',Description='Test Pricebook Entry 1', isActive=true);
insert pbk1;
Opportunity opp1 = new Opportunity (Name='Opp1',StageName='Stage 0 - Lead Handed Off',CloseDate=Date.today(), AccountId = acc.id);
insert opp1;
Id pricebookId = Test.getStandardPricebookId();
//Create your product
Product2 prod = new Product2(
Name = 'Product X',
ProductCode = 'Pro-X',
isActive = true
);
insert prod;
//Create your pricebook entry
PricebookEntry pbEntry = new PricebookEntry(
Pricebook2Id = pricebookId,
Product2Id = prod.Id,
UnitPrice = 100.00,
IsActive = true
);
insert pbEntry;
//create your opportunity line item. This assumes you already have an opportunity created, called opp
OpportunityLineItem oli = new OpportunityLineItem(
OpportunityId = opp1.Id,
Quantity = 5,
PricebookEntryId = pbEntry.Id,
TotalPrice = 234
);
insert oli;
}
}
Error:
System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, This price definition already exists in this price book: []
Remove this and try it .
1) http://salesforce.stackexchange.com/questions/8334/help-with-test-coverage
Please check below code
Let us know if this will help you