You need to sign in to do that
Don't have an account?
Chelsea Lukowski
Code Coverage for Trigger
I have the trigger below set up in Sandbox, but I have no Code Coverage. I need help writing a test class to check code coverage.
trigger UpdateOrderOppPrice on Order_Product__c (before insert) {
set<Id> psi = new set<Id>();
set<Id> osi = new set<Id>();
set<Id> asi = new set<Id>();
set<string> pci = new set<string>();
set<decimal> acn = new set<decimal>();
map<Id,string> pdci = new map<Id,string>();
map<String,Id> msi = new map<String,Id>();
map<Id,case> mis = new map<Id,case>();
map<string,Titan_Contract__c> mst = new map<string,Titan_Contract__c>();
map<string,Titan_Contract__c> mdct = new map<string,Titan_Contract__c>();
map<string,Titan_Contract__c> mpdct = new map<string,Titan_Contract__c>();
map<Id,decimal> pbe = new map<Id,decimal>();
for(Order_Product__c order:trigger.new){
psi.add(order.Product__c);
osi.add(order.case_record__c);
}
for(PricebookEntry pe :[Select Id, UnitPrice,Product2ID,Product2.Discount_Code__c from PricebookEntry where Product2ID IN:psi]){
pbe.put(pe.Product2ID,pe.UnitPrice);
pdci.put(pe.Product2ID,pe.Product2.Discount_Code__c);
}
for(Case c:[Select ID,Bill_To_Account__c,customer_number__c,company__c,Account.Discount_Code__c,Account.Company__c from Case where Id IN:osi]){
asi.add((id)c.Bill_To_Account__c);
mis.put(c.Id,c);
if(c.Account.Discount_Code__c!=null)pci.add(c.Account.Discount_Code__c);
if(c.Account.Company__c!=null)acn.add(decimal.valueOf(c.Account.Company__c));
}
for(Order_Product__c order:trigger.new){
if(msi.containsKey(string.valueOf(order.Product__c)+mis.get(order.case_record__c).Company__c))
order.Product__C=msi.get(string.valueOf(order.Product__c)+mis.get(order.case_record__c).Company__c);
}
for(Titan_Contract__c t:[SELECT Account__c, Beginning_Date__c, Customer_Number_or_Discount_Code__c, Expiration_Date__c, Price_or_Multiplier_Contract__c,
Price_or_Percent_of_List_Price_1__c, Price_or_Percent_of_List_Price_2__c, Price_or_Percent_of_List_Price_3__c,
Price_or_Percent_of_List_Price_4__c, Price_or_Percent_of_List_Price_5__c, Price_or_Percent_of_List_Price_6__c,
Price_or_Percent_of_List_Price_7__c, Price_or_Percent_of_List_Price_8__c, Product__c, Quantity_Level_1__c, Quantity_Level_2__c,
Quantity_Level_3__c, Quantity_Level_4__c, Quantity_Level_5__c, Quantity_Level_6__c, Quantity_Level_7__c, Quantity_Level_8__c,
Quote_Number__c, Quote_Revision__c, Id, SystemModstamp, Name, Universal_Customer__c
FROM Titan_Contract__c
where Account__c IN:asi and Product__c IN:psi and Beginning_Date__c <:system.today() and Expiration_Date__c >:system.today()]){
mst.put(string.ValueOf(t.Account__c).left(15)+string.ValueOf(t.Product__c),t);
}
for(Titan_Contract__c t:[SELECT Account__c, Beginning_Date__c, Customer_Number_or_Discount_Code__c, Expiration_Date__c, Price_or_Multiplier_Contract__c,
Price_or_Percent_of_List_Price_1__c, Price_or_Percent_of_List_Price_2__c, Price_or_Percent_of_List_Price_3__c,
Price_or_Percent_of_List_Price_4__c, Price_or_Percent_of_List_Price_5__c, Price_or_Percent_of_List_Price_6__c,
Price_or_Percent_of_List_Price_7__c, Price_or_Percent_of_List_Price_8__c, Product__c, Quantity_Level_1__c, Quantity_Level_2__c,
Quantity_Level_3__c, Quantity_Level_4__c, Quantity_Level_5__c, Quantity_Level_6__c, Quantity_Level_7__c, Quantity_Level_8__c,
Quote_Number__c, Quote_Revision__c, Id, SystemModstamp, Name, Universal_Customer__c,Product_Discount_Code__c,Company_Number__c
FROM Titan_Contract__c
where Customer_Number_or_Discount_Code__c!=null and Customer_Number_or_Discount_Code__c IN:pci and Beginning_Date__c <:system.today() and Expiration_Date__c >:system.today()
and Company_Number__c IN:acn and (Product__c IN:psi or Product__c=null)]){
mdct.put(t.Customer_Number_or_Discount_Code__c+(t.Company_Number__c!=null?string.ValueOf(t.Company_Number__c):'')+(t.Product__c!=null?string.ValueOf(t.Product__c):''),t);
}
for(Titan_Contract__c t:[SELECT Account__c, Beginning_Date__c, Customer_Number_or_Discount_Code__c, Expiration_Date__c, Price_or_Multiplier_Contract__c,
Price_or_Percent_of_List_Price_1__c, Price_or_Percent_of_List_Price_2__c, Price_or_Percent_of_List_Price_3__c,
Price_or_Percent_of_List_Price_4__c, Price_or_Percent_of_List_Price_5__c, Price_or_Percent_of_List_Price_6__c,
Price_or_Percent_of_List_Price_7__c, Price_or_Percent_of_List_Price_8__c, Product__c, Quantity_Level_1__c, Quantity_Level_2__c,
Quantity_Level_3__c, Quantity_Level_4__c, Quantity_Level_5__c, Quantity_Level_6__c, Quantity_Level_7__c, Quantity_Level_8__c,
Quote_Number__c, Quote_Revision__c, Id, SystemModstamp, Name, Universal_Customer__c,Product_Discount_Code__c,Company_Number__c
FROM Titan_Contract__c
where Product_Discount_Code__c!=null and Product_Discount_Code__c IN:pdci.values() and Product__c IN:psi and Company_Number__c IN:acn
and Beginning_Date__c <:system.today() and Expiration_Date__c >:system.today()]){
mpdct.put(t.Product_Discount_Code__c+(t.Company_Number__c!=null?string.ValueOf(t.Company_Number__c):'')+string.ValueOf(t.Product__c),t);
}
for(Order_Product__c order:trigger.new){
order.Price__c=null;
string key = mis.get(order.case_record__c).Bill_To_Account__c+string.ValueOf(order.Product__c);
system.debug(mst);
system.debug(key);
if(mst.containskey(key)){
Titan_Contract__c t = mst.get(key);
for(integer i=1;i<10;i++){
if(t.get('Quantity_Level_'+(i+1)+'__c')==null || order.Quantity__c < (decimal)t.get('Quantity_Level_'+(i+1)+'__c')){
order.Price__c = (t.Price_or_Multiplier_Contract__c == 'P')?(decimal)t.get('Price_or_Percent_of_List_Price_'+i+'__c'):pbe.get(t.Product__c)*(decimal)t.get('Price_or_Percent_of_List_Price_'+i+'__c');
break;
}
}
}
if(order.Price__c==null){
if(mis.get(order.case_record__c).Account.Discount_Code__c!=null){
key=mis.get(order.case_record__c).Account.Discount_Code__c+(mis.get(order.case_record__c).Account.Company__c!=null?mis.get(order.case_record__c).Account.Company__c:'');
if(mdct.containskey(key+string.ValueOf(order.Product__c)))key+=string.ValueOf(order.Product__c);
if(mdct.containskey(key)){
Titan_Contract__c t = mdct.get(key);
for(integer i=1;i<10;i++){
if(t.get('Quantity_Level_'+(i+1)+'__c')==null || order.Quantity__c < (decimal)t.get('Quantity_Level_'+(i+1)+'__c')){
order.Price__c = (t.Price_or_Multiplier_Contract__c == 'P')?(decimal)t.get('Price_or_Percent_of_List_Price_'+i+'__c'):pbe.get((t.Product__c!=null?t.Product__c:order.Product__c))*(decimal)t.get('Price_or_Percent_of_List_Price_'+i+'__c');
break;
}
}
}
}
}
if(order.Price__c==null){
if(pdci.containskey(order.Product__c) && pdci.get(order.Product__c)!=null){
key=pdci.get(order.Product__c)+(mis.get(order.case_record__c).Account.Company__c!=null?mis.get(order.case_record__c).Account.Company__c:'')+string.ValueOf(order.Product__c);
if(mpdct.containskey(key)){
Titan_Contract__c t = mpdct.get(key);
for(integer i=1;i<10;i++){
if(t.get('Quantity_Level_'+(i+1)+'__c')==null || order.Quantity__c < (decimal)t.get('Quantity_Level_'+(i+1)+'__c')){
order.Price__c = (t.Price_or_Multiplier_Contract__c == 'P')?(decimal)t.get('Price_or_Percent_of_List_Price_'+i+'__c'):pbe.get((t.Product__c!=null?t.Product__c:order.Product__c))*(decimal)t.get('Price_or_Percent_of_List_Price_'+i+'__c');
break;
}
}
}
}
}
if(order.Price__c==null)order.Price__c = pbe.get(order.Product__c);
}
}
trigger UpdateOrderOppPrice on Order_Product__c (before insert) {
set<Id> psi = new set<Id>();
set<Id> osi = new set<Id>();
set<Id> asi = new set<Id>();
set<string> pci = new set<string>();
set<decimal> acn = new set<decimal>();
map<Id,string> pdci = new map<Id,string>();
map<String,Id> msi = new map<String,Id>();
map<Id,case> mis = new map<Id,case>();
map<string,Titan_Contract__c> mst = new map<string,Titan_Contract__c>();
map<string,Titan_Contract__c> mdct = new map<string,Titan_Contract__c>();
map<string,Titan_Contract__c> mpdct = new map<string,Titan_Contract__c>();
map<Id,decimal> pbe = new map<Id,decimal>();
for(Order_Product__c order:trigger.new){
psi.add(order.Product__c);
osi.add(order.case_record__c);
}
for(PricebookEntry pe :[Select Id, UnitPrice,Product2ID,Product2.Discount_Code__c from PricebookEntry where Product2ID IN:psi]){
pbe.put(pe.Product2ID,pe.UnitPrice);
pdci.put(pe.Product2ID,pe.Product2.Discount_Code__c);
}
for(Case c:[Select ID,Bill_To_Account__c,customer_number__c,company__c,Account.Discount_Code__c,Account.Company__c from Case where Id IN:osi]){
asi.add((id)c.Bill_To_Account__c);
mis.put(c.Id,c);
if(c.Account.Discount_Code__c!=null)pci.add(c.Account.Discount_Code__c);
if(c.Account.Company__c!=null)acn.add(decimal.valueOf(c.Account.Company__c));
}
for(Order_Product__c order:trigger.new){
if(msi.containsKey(string.valueOf(order.Product__c)+mis.get(order.case_record__c).Company__c))
order.Product__C=msi.get(string.valueOf(order.Product__c)+mis.get(order.case_record__c).Company__c);
}
for(Titan_Contract__c t:[SELECT Account__c, Beginning_Date__c, Customer_Number_or_Discount_Code__c, Expiration_Date__c, Price_or_Multiplier_Contract__c,
Price_or_Percent_of_List_Price_1__c, Price_or_Percent_of_List_Price_2__c, Price_or_Percent_of_List_Price_3__c,
Price_or_Percent_of_List_Price_4__c, Price_or_Percent_of_List_Price_5__c, Price_or_Percent_of_List_Price_6__c,
Price_or_Percent_of_List_Price_7__c, Price_or_Percent_of_List_Price_8__c, Product__c, Quantity_Level_1__c, Quantity_Level_2__c,
Quantity_Level_3__c, Quantity_Level_4__c, Quantity_Level_5__c, Quantity_Level_6__c, Quantity_Level_7__c, Quantity_Level_8__c,
Quote_Number__c, Quote_Revision__c, Id, SystemModstamp, Name, Universal_Customer__c
FROM Titan_Contract__c
where Account__c IN:asi and Product__c IN:psi and Beginning_Date__c <:system.today() and Expiration_Date__c >:system.today()]){
mst.put(string.ValueOf(t.Account__c).left(15)+string.ValueOf(t.Product__c),t);
}
for(Titan_Contract__c t:[SELECT Account__c, Beginning_Date__c, Customer_Number_or_Discount_Code__c, Expiration_Date__c, Price_or_Multiplier_Contract__c,
Price_or_Percent_of_List_Price_1__c, Price_or_Percent_of_List_Price_2__c, Price_or_Percent_of_List_Price_3__c,
Price_or_Percent_of_List_Price_4__c, Price_or_Percent_of_List_Price_5__c, Price_or_Percent_of_List_Price_6__c,
Price_or_Percent_of_List_Price_7__c, Price_or_Percent_of_List_Price_8__c, Product__c, Quantity_Level_1__c, Quantity_Level_2__c,
Quantity_Level_3__c, Quantity_Level_4__c, Quantity_Level_5__c, Quantity_Level_6__c, Quantity_Level_7__c, Quantity_Level_8__c,
Quote_Number__c, Quote_Revision__c, Id, SystemModstamp, Name, Universal_Customer__c,Product_Discount_Code__c,Company_Number__c
FROM Titan_Contract__c
where Customer_Number_or_Discount_Code__c!=null and Customer_Number_or_Discount_Code__c IN:pci and Beginning_Date__c <:system.today() and Expiration_Date__c >:system.today()
and Company_Number__c IN:acn and (Product__c IN:psi or Product__c=null)]){
mdct.put(t.Customer_Number_or_Discount_Code__c+(t.Company_Number__c!=null?string.ValueOf(t.Company_Number__c):'')+(t.Product__c!=null?string.ValueOf(t.Product__c):''),t);
}
for(Titan_Contract__c t:[SELECT Account__c, Beginning_Date__c, Customer_Number_or_Discount_Code__c, Expiration_Date__c, Price_or_Multiplier_Contract__c,
Price_or_Percent_of_List_Price_1__c, Price_or_Percent_of_List_Price_2__c, Price_or_Percent_of_List_Price_3__c,
Price_or_Percent_of_List_Price_4__c, Price_or_Percent_of_List_Price_5__c, Price_or_Percent_of_List_Price_6__c,
Price_or_Percent_of_List_Price_7__c, Price_or_Percent_of_List_Price_8__c, Product__c, Quantity_Level_1__c, Quantity_Level_2__c,
Quantity_Level_3__c, Quantity_Level_4__c, Quantity_Level_5__c, Quantity_Level_6__c, Quantity_Level_7__c, Quantity_Level_8__c,
Quote_Number__c, Quote_Revision__c, Id, SystemModstamp, Name, Universal_Customer__c,Product_Discount_Code__c,Company_Number__c
FROM Titan_Contract__c
where Product_Discount_Code__c!=null and Product_Discount_Code__c IN:pdci.values() and Product__c IN:psi and Company_Number__c IN:acn
and Beginning_Date__c <:system.today() and Expiration_Date__c >:system.today()]){
mpdct.put(t.Product_Discount_Code__c+(t.Company_Number__c!=null?string.ValueOf(t.Company_Number__c):'')+string.ValueOf(t.Product__c),t);
}
for(Order_Product__c order:trigger.new){
order.Price__c=null;
string key = mis.get(order.case_record__c).Bill_To_Account__c+string.ValueOf(order.Product__c);
system.debug(mst);
system.debug(key);
if(mst.containskey(key)){
Titan_Contract__c t = mst.get(key);
for(integer i=1;i<10;i++){
if(t.get('Quantity_Level_'+(i+1)+'__c')==null || order.Quantity__c < (decimal)t.get('Quantity_Level_'+(i+1)+'__c')){
order.Price__c = (t.Price_or_Multiplier_Contract__c == 'P')?(decimal)t.get('Price_or_Percent_of_List_Price_'+i+'__c'):pbe.get(t.Product__c)*(decimal)t.get('Price_or_Percent_of_List_Price_'+i+'__c');
break;
}
}
}
if(order.Price__c==null){
if(mis.get(order.case_record__c).Account.Discount_Code__c!=null){
key=mis.get(order.case_record__c).Account.Discount_Code__c+(mis.get(order.case_record__c).Account.Company__c!=null?mis.get(order.case_record__c).Account.Company__c:'');
if(mdct.containskey(key+string.ValueOf(order.Product__c)))key+=string.ValueOf(order.Product__c);
if(mdct.containskey(key)){
Titan_Contract__c t = mdct.get(key);
for(integer i=1;i<10;i++){
if(t.get('Quantity_Level_'+(i+1)+'__c')==null || order.Quantity__c < (decimal)t.get('Quantity_Level_'+(i+1)+'__c')){
order.Price__c = (t.Price_or_Multiplier_Contract__c == 'P')?(decimal)t.get('Price_or_Percent_of_List_Price_'+i+'__c'):pbe.get((t.Product__c!=null?t.Product__c:order.Product__c))*(decimal)t.get('Price_or_Percent_of_List_Price_'+i+'__c');
break;
}
}
}
}
}
if(order.Price__c==null){
if(pdci.containskey(order.Product__c) && pdci.get(order.Product__c)!=null){
key=pdci.get(order.Product__c)+(mis.get(order.case_record__c).Account.Company__c!=null?mis.get(order.case_record__c).Account.Company__c:'')+string.ValueOf(order.Product__c);
if(mpdct.containskey(key)){
Titan_Contract__c t = mpdct.get(key);
for(integer i=1;i<10;i++){
if(t.get('Quantity_Level_'+(i+1)+'__c')==null || order.Quantity__c < (decimal)t.get('Quantity_Level_'+(i+1)+'__c')){
order.Price__c = (t.Price_or_Multiplier_Contract__c == 'P')?(decimal)t.get('Price_or_Percent_of_List_Price_'+i+'__c'):pbe.get((t.Product__c!=null?t.Product__c:order.Product__c))*(decimal)t.get('Price_or_Percent_of_List_Price_'+i+'__c');
break;
}
}
}
}
}
if(order.Price__c==null)order.Price__c = pbe.get(order.Product__c);
}
}
After that, your class can create a list of Order_Product__c records and insert them. This will fire the trigger. You should create different kinds of Order_Product__c records so that each of the code paths is executed.
Lastly you *should* check to see that the order.Price__c that you arrive at is the value that you expect, based on the test data you created.
Sorry if that is too vague. Is there a particular question you are stuck on with regards to writing the test class?
Separately, my advice would be to do three things:
1. This is more logic than I like to have in a trigger. I would make a class, move this logic to the class, and then just have the trigger call the class.
2. Your code should be separated out into methods.
3. You should consider trying to only query the DB for the information in Titan_Contract__c once, just to avoid hitting limits later on.