You need to sign in to do that
Don't have an account?
Puja Patil 13
Test Class for before insert trigger order line item
Hi,
I have Trigger on Order Line Item. I also wrote test class for that but it can not covers "trigger.isDelete" and "Trigger.isUpdate" cases.
Please check following trigger and test class :
//********************************* Trigger *************************************
trigger TotalPiecesSum on OrderItem (before update, before insert,before delete) {
list<Order> a = new list<Order>();
set<id> OrderIDs= new set<id>();
if(trigger.isInsert || trigger.isBefore){
for(OrderItem o : Trigger.new){
OrderIDs.add(o.Orderid);
}
}
else if(trigger.isDelete){
for(OrderItem o : Trigger.old){
OrderIDs.add(o.Orderid);
}
}
else
if(Trigger.isUpdate){
for(OrderItem o : Trigger.new){
if(OrderItem.OrderId != null && o.Pieces__c!= trigger.oldMap.get(o.Id).Pieces__c){
OrderIDs.add(o.OrderId);
}
}
}
update a;
AggregateResult[] groupedResults = [SELECT SUM(Pieces__c),OrderId FROM OrderItem where OrderId IN :OrderIDs GROUP BY OrderId ];
system.debug('*******groupedResults **********'+groupedResults);
for(AggregateResult ar:groupedResults) {
Id orid = (ID)ar.get('OrderId');
system.debug('*******selected Oredr id **********'+orid);
Decimal count = (Decimal)ar.get('expr0');
Order o1 = new Order(Id=orid);
o1.Total_Pieces1__c= count;
system.debug('*******Total_Pieces1__cOredr id **********'+count);
a.add(o1);
}
update a;
}
//********************************* Test Class*************************************
@isTest(seeAllData = true)
private class TestTotalPiecesSum {
static testMethod void myUnitTest() {
//Test Account Insert
Account a = new Account();
a.Name = 'Test Account';
a.Custom2__c = '000093';
a.Business_Type__c = 'Consultant';
insert a;
Product2 p = new Product2();
p.Name = ' Test Product ';
p.Description='Test Product Entry 1';
p.productCode = 'ABC';
p.isActive = true;
insert p;
Pricebook2 standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
PricebookEntry standardPrice = new PricebookEntry();
standardPrice.Pricebook2Id = standardPb.Id;
standardPrice.Product2Id = p.Id;
standardPrice.UnitPrice = 1;
standardPrice.IsActive = true;
standardPrice.UseStandardPrice = false;
insert standardPrice ;
//Test Order Insert
Order o = new Order();
o.Name = 'Test Order ';
o.Status = 'Draft';
o.EffectiveDate = system.today();
o.EndDate = system.today() + 4;
o.AccountId = a.id;
o.Pricebook2Id = standardPb.Id ;
insert o;
system.assertEquals(o.Total_Pieces1__c, null);
OrderItem i = new OrderItem();
i.OrderId = o.id;
i.Quantity = 24;
i.UnitPrice = 240;
i.Pieces__c = i.Quantity;
i.Product2id = p.id;
i.PricebookEntryId=standardPrice.id;
//i.Total_Price__c = i.Quantity * i.UnitPrice;
insert i;
Order or1 = [SELECT Total_Pieces1__c from Order where Id = :o.Id];
system.assertEquals(or1.Total_Pieces1__c ,i.Pieces__c);
//Test OrderItem on update
OrderItem op1 = [select Pieces__c,PricebookEntryId from OrderItem where Id = :i.Id];
op1.Pieces__c = 24;
update op1;
Order or2 = [SELECT Total_Pieces1__c from Order where Id = :o.Id];
system.assertEquals(or2.Total_Pieces1__c ,op1.Pieces__c);
//Test OrderItem on second insert
OrderItem i2 = new OrderItem();
i2.OrderId = o.id;
i2.Quantity = 24;
i2.UnitPrice = 240;
i2.Pieces__c = i.Quantity;
i2.PricebookEntryId=standardPrice.id;
//i2.Total_Price__c = i.Quantity * i.UnitPrice;
insert i2;
AggregateResult ag1 = [select sum(Pieces__c) from OrderItem where OrderId = :o.Id];
Order o3 = [select Total_Pieces1__c from Order where Id = :o.Id];
system.assertEquals(o3.Total_Pieces1__c,ag1.get('expr0'));
AggregateResult ag2 = [select sum(Pieces__c) from OrderItem where OrderId = :o.Id];
Order o4 = [select Total_Pieces1__c from Order where Id = :o.Id];
system.assertEquals(o4.Total_Pieces1__c,ag1.get('expr0'));
}
}
It gives 66% Code Coverage.
I have Trigger on Order Line Item. I also wrote test class for that but it can not covers "trigger.isDelete" and "Trigger.isUpdate" cases.
Please check following trigger and test class :
//********************************* Trigger *************************************
trigger TotalPiecesSum on OrderItem (before update, before insert,before delete) {
list<Order> a = new list<Order>();
set<id> OrderIDs= new set<id>();
if(trigger.isInsert || trigger.isBefore){
for(OrderItem o : Trigger.new){
OrderIDs.add(o.Orderid);
}
}
else if(trigger.isDelete){
for(OrderItem o : Trigger.old){
OrderIDs.add(o.Orderid);
}
}
else
if(Trigger.isUpdate){
for(OrderItem o : Trigger.new){
if(OrderItem.OrderId != null && o.Pieces__c!= trigger.oldMap.get(o.Id).Pieces__c){
OrderIDs.add(o.OrderId);
}
}
}
update a;
AggregateResult[] groupedResults = [SELECT SUM(Pieces__c),OrderId FROM OrderItem where OrderId IN :OrderIDs GROUP BY OrderId ];
system.debug('*******groupedResults **********'+groupedResults);
for(AggregateResult ar:groupedResults) {
Id orid = (ID)ar.get('OrderId');
system.debug('*******selected Oredr id **********'+orid);
Decimal count = (Decimal)ar.get('expr0');
Order o1 = new Order(Id=orid);
o1.Total_Pieces1__c= count;
system.debug('*******Total_Pieces1__cOredr id **********'+count);
a.add(o1);
}
update a;
}
//********************************* Test Class*************************************
@isTest(seeAllData = true)
private class TestTotalPiecesSum {
static testMethod void myUnitTest() {
//Test Account Insert
Account a = new Account();
a.Name = 'Test Account';
a.Custom2__c = '000093';
a.Business_Type__c = 'Consultant';
insert a;
Product2 p = new Product2();
p.Name = ' Test Product ';
p.Description='Test Product Entry 1';
p.productCode = 'ABC';
p.isActive = true;
insert p;
Pricebook2 standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
PricebookEntry standardPrice = new PricebookEntry();
standardPrice.Pricebook2Id = standardPb.Id;
standardPrice.Product2Id = p.Id;
standardPrice.UnitPrice = 1;
standardPrice.IsActive = true;
standardPrice.UseStandardPrice = false;
insert standardPrice ;
//Test Order Insert
Order o = new Order();
o.Name = 'Test Order ';
o.Status = 'Draft';
o.EffectiveDate = system.today();
o.EndDate = system.today() + 4;
o.AccountId = a.id;
o.Pricebook2Id = standardPb.Id ;
insert o;
system.assertEquals(o.Total_Pieces1__c, null);
OrderItem i = new OrderItem();
i.OrderId = o.id;
i.Quantity = 24;
i.UnitPrice = 240;
i.Pieces__c = i.Quantity;
i.Product2id = p.id;
i.PricebookEntryId=standardPrice.id;
//i.Total_Price__c = i.Quantity * i.UnitPrice;
insert i;
Order or1 = [SELECT Total_Pieces1__c from Order where Id = :o.Id];
system.assertEquals(or1.Total_Pieces1__c ,i.Pieces__c);
//Test OrderItem on update
OrderItem op1 = [select Pieces__c,PricebookEntryId from OrderItem where Id = :i.Id];
op1.Pieces__c = 24;
update op1;
Order or2 = [SELECT Total_Pieces1__c from Order where Id = :o.Id];
system.assertEquals(or2.Total_Pieces1__c ,op1.Pieces__c);
//Test OrderItem on second insert
OrderItem i2 = new OrderItem();
i2.OrderId = o.id;
i2.Quantity = 24;
i2.UnitPrice = 240;
i2.Pieces__c = i.Quantity;
i2.PricebookEntryId=standardPrice.id;
//i2.Total_Price__c = i.Quantity * i.UnitPrice;
insert i2;
AggregateResult ag1 = [select sum(Pieces__c) from OrderItem where OrderId = :o.Id];
Order o3 = [select Total_Pieces1__c from Order where Id = :o.Id];
system.assertEquals(o3.Total_Pieces1__c,ag1.get('expr0'));
AggregateResult ag2 = [select sum(Pieces__c) from OrderItem where OrderId = :o.Id];
Order o4 = [select Total_Pieces1__c from Order where Id = :o.Id];
system.assertEquals(o4.Total_Pieces1__c,ag1.get('expr0'));
}
}
It gives 66% Code Coverage.
Update your code on the trigger with below code .That will cover the percentage
if(trigger.isDelete){
for(OrderItem o : Trigger.old){
OrderIDs.add(o.Orderid);
}
}
if(Trigger.isUpdate){
for(OrderItem o : Trigger.new){
if(OrderItem.OrderId != null && o.Pieces__c!= trigger.oldMap.get(o.Id).Pieces__c){
OrderIDs.add(o.OrderId);
}
}
}