You need to sign in to do that
Don't have an account?
Frank Carter
apex test class insert help
Hello,
I need help with a test class.I have created an object related to opportunity, “billing detail”.
The result I want to reach is the creation of multiple child object, Billing details, according the number of months that I Have from the difference to “End of billing” and “Created date” divided for a number. The number is 2 if the billing is two-monthly, 1 if is monthly, 3 if is quarterly, 12 for annual. I have an after insert trigger that call an apex class(Helper).
Apex class:
I thought to put into "System.assert.Equals" the length of the listBD(class Helper - CreateBD method) but it doesn't recognize listBD in TestHelper.
Can anyone help me?
Thanks,
Frank
I need help with a test class.I have created an object related to opportunity, “billing detail”.
The result I want to reach is the creation of multiple child object, Billing details, according the number of months that I Have from the difference to “End of billing” and “Created date” divided for a number. The number is 2 if the billing is two-monthly, 1 if is monthly, 3 if is quarterly, 12 for annual. I have an after insert trigger that call an apex class(Helper).
Apex class:
public class Helper { public static void createBD(Billing_Detail__c bd){ List <Billing_Detail__c> listBD = new List <Billing_Detail__c> (); Integer i=0; Integer K=0; if (bd.Billing_Period__c=='two-monthly') { k=2; Date myDate =bd.Monthly_Forecast__c; if(((bd.Billing_Type__c=='FEE')||(bd.Billing_Type__c=='PPU'))&& bd.Billing_Period__c=='two-monthly'){ i= myDate.monthsBetween(bd.End_of_Billing__c)/2; } for(Integer n =0; n<i-1; n++){//-1 mi serve altrimenti non considerando quella da interfaccia mi crea un'istanza in più if(i!=1){ Billing_Detail__c newBd = new Billing_Detail__c (); newBd.Amount__c=bd.Amount__c; newBd.Billing_Date__c=bd.Billing_Date__c; newBd.Billing_Type__c=bd.Billing_Type__c; newBd.Billing_Status__c=bd.Billing_Status__c; newBd.Billing_Period__c= bd.Billing_Period__c; newBd.Product_Line__c=bd.Product_Line__c; newBd.Product__c=bd.Product__c; newBd.End_of_Billing__c=bd.End_of_Billing__c; newBd.Billing_Detail__c=bd.Billing_Detail__c; newBd.CurrencyIsoCode=bd.CurrencyIsoCode; newBd.Monthly_Forecast__c= bd.Monthly_Forecast__c.addMonths(k); k+=2; listBD.add(newBd); } } insert listBD; } // ripeto le stesso operazioni di prima solo che qui si parla di trimestre quindi divido per 3 else if (bd.Billing_Period__c=='quarterly') { k=3; Date myDate =bd.Monthly_Forecast__c; if (((bd.Billing_Type__c=='FEE')||(bd.Billing_Type__c=='PPU'))&& bd.Billing_Period__c=='quarterly'){ i= myDate.monthsBetween(bd.End_of_Billing__c)/3; } for(Integer n =0; n<i-1; n++){ if(i!=1){ Billing_Detail__c newBd = new Billing_Detail__c (); newBd.Amount__c=bd.Amount__c; newBd.Billing_Date__c=bd.Billing_Date__c; newBd.Billing_Type__c=bd.Billing_Type__c; newBd.Billing_Status__c=bd.Billing_Status__c; newBd.Billing_Period__c= bd.Billing_Period__c; newBd.Product_Line__c=bd.Product_Line__c; newBd.Product__c=bd.Product__c; newBd.End_of_Billing__c=bd.End_of_Billing__c; newBd.Billing_Detail__c=bd.Billing_Detail__c; newBd.CurrencyIsoCode=bd.CurrencyIsoCode; newBd.Monthly_Forecast__c= bd.Monthly_Forecast__c.addMonths(k);//parametro k per data scalata k+=3; listBD.add(newBd); } } insert listBD; } else if (bd.Billing_Period__c=='annual') { k=12; Date myDate =bd.Monthly_Forecast__c; if (((bd.Billing_Type__c=='FEE')||(bd.Billing_Type__c=='PPU'))&& bd.Billing_Period__c=='annual'){ i= myDate.monthsBetween(bd.End_of_Billing__c)/12; } for(Integer n =0; n<i-1; n++){ if(i!=1){ Billing_Detail__c newBd = new Billing_Detail__c (); newBd.Amount__c=bd.Amount__c; newBd.Billing_Date__c=bd.Billing_Date__c; newBd.Billing_Type__c=bd.Billing_Type__c; newBd.Billing_Status__c=bd.Billing_Status__c; newBd.Billing_Period__c= bd.Billing_Period__c; newBd.Product_Line__c=bd.Product_Line__c; newBd.Product__c=bd.Product__c; newBd.End_of_Billing__c=bd.End_of_Billing__c; newBd.Billing_Detail__c=bd.Billing_Detail__c; newBd.CurrencyIsoCode=bd.CurrencyIsoCode; newBd.Monthly_Forecast__c= bd.Monthly_Forecast__c.addMonths(k); k+=12; listBD.add(newBd); } } insert listBD; } else if (bd.Billing_Period__c=='monthly'){ Date myDate =bd.Monthly_Forecast__c; if(((bd.Billing_Type__c=='FEE')||(bd.Billing_Type__c=='PPU'))&& bd.Billing_Period__c=='monthly'){ i= myDate.monthsBetween(bd.End_of_Billing__c); System.debug('i='+i); } for(Integer n =0; n<i-1; n++){ if(i!=1){ Billing_Detail__c newBd = new Billing_Detail__c (); System.debug('n='+n); newBd.Amount__c=bd.Amount__c; System.debug('amount'+newBd.Amount__c); newBd.Billing_Date__c=bd.Billing_Date__c; newBd.Billing_Type__c=bd.Billing_Type__c; newBd.Billing_Status__c=bd.Billing_Status__c; newBd.Billing_Period__c= bd.Billing_Period__c; newBd.Product_Line__c=bd.Product_Line__c; newBd.Product__c=bd.Product__c; newBd.End_of_Billing__c=bd.End_of_Billing__c; newBd.Billing_Detail__c=bd.Billing_Detail__c; newBd.CurrencyIsoCode=bd.CurrencyIsoCode; newBd.Monthly_Forecast__c= bd.Monthly_Forecast__c.addMonths(n+1); System.debug('monthly forecast'+newBd.Monthly_Forecast__c); listBD.add(newBd); } } insert listBD; } } }Now I'm trying to create a test class.
@isTest public class testHelper { @isTest static void testcreateBD(){ // create the object in input of the class Helper Billing_Detail__c c= new Billing_Detail__c(); c.Amount__c= 100; c.Billing_Type__c='UNA TANTUM'; c.Billing_Status__c='Authorized for Billing'; c.Product_Line__c='Interactive Experience (iX)'; c.Product__c='Chat Delivery'; c.Monthly_Forecast__ c=Date.newInstance(2018, 7, 9); c.End_of_Billing__c=Date.newInstance(2018, 7, 9); c.Billing_Detail__c='test 1'; c.CurrencyIsoCode='€'; //call Helper class with createBD with c in input Helper.createBD(c); System.assert.Equals( ??? ); } }but I don't understand how to continue.
I thought to put into "System.assert.Equals" the length of the listBD(class Helper - CreateBD method) but it doesn't recognize listBD in TestHelper.
Can anyone help me?
Thanks,
Frank
All Answers
I change the code to have a listBD.size() > 1); but code coverage =0;
You need to run the test and share your debug log here for us to scrutinize.
ok, I'll do it. but I've doubts.
1. the test class have sense?
2. it is necessary to create first(in the test class) the account and opportunity related to the billing detail?
Yes, it can definitely be improved but at the very least the Helper.createBD(c) method takes a Billing_Detail__c and creates 1 or more new Billing_Detail__c records, therefore it makes sense for the test method to check for the creation of new BD records.
2. it is necessary to create first(in the test class) the account and opportunity related to the billing detail?
Only if the Billing_Detail__c cannot exist without account and opportunity, but that does not seem to be the case.
Lastly are you using this Helper class in a trigger? If so, share the trigger as well when you do post the execution log.
error:
System.QueryException: List has no rows for assignment to SObject
Class.testHelper.testFeeMonthly: line 4, column 1
Class.testHelper.testPPUmonthly: line 30, column 1