You need to sign in to do that
Don't have an account?
Need help improving an Apex Class for a trigger?
trigger AnnualInvoiceAmountUpdates on Invoice__c (before insert, before update) {
if(trigger.isupdate){
for(Invoice__c a:trigger.new){
if((a.Sign_Up_Month__c == 'January')&&(a.Billing_Period__c== 'Annual')){
a.January__c= a.Amount_Breakdown__c;}
if((a.Sign_Up_Month__c <> 'January')&&(a.Billing_Period__c== 'Annual')){
a.January__c= 0;}
if((a.Sign_Up_Month__c == 'February')&&(a.Billing_Period__c== 'Annual')){
a.February__c= a.Amount_Breakdown__c;}
if((a.Sign_Up_Month__c <> 'February')&&(a.Billing_Period__c== 'Annual')){
a.February__c= 0;}
if((a.Sign_Up_Month__c == 'March')&&(a.Billing_Period__c== 'Annual')){
a.March__c= a.Amount_Breakdown__c;}
if((a.Sign_Up_Month__c <> 'March')&&(a.Billing_Period__c== 'Annual')){
a.March__c= 0;}
if((a.Sign_Up_Month__c == 'April')&&(a.Billing_Period__c== 'Annual')){
a.April__c= a.Amount_Breakdown__c;}
if((a.Sign_Up_Month__c <> 'April')&&(a.Billing_Period__c== 'Annual')){
a.April__c= 0;}
if((a.Sign_Up_Month__c == 'May')&&(a.Billing_Period__c== 'Annual')){
a.May__c= a.Amount_Breakdown__c;}
if((a.Sign_Up_Month__c <> 'May')&&(a.Billing_Period__c== 'Annual')){
a.May__c= 0;}
if((a.Sign_Up_Month__c == 'June')&&(a.Billing_Period__c== 'Annual')){
a.June__c= a.Amount_Breakdown__c;}
if((a.Sign_Up_Month__c <> 'June')&&(a.Billing_Period__c== 'Annual')){
a.June__c= 0;}
if((a.Sign_Up_Month__c == 'July')&&(a.Billing_Period__c== 'Annual')){
a.July__c= a.Amount_Breakdown__c;}
if((a.Sign_Up_Month__c <> 'July')&&(a.Billing_Period__c== 'Annual')){
a.July__c= 0;}
if((a.Sign_Up_Month__c == 'August')&&(a.Billing_Period__c== 'Annual')){
a.August__c= a.Amount_Breakdown__c;}
if((a.Sign_Up_Month__c <> 'August')&&(a.Billing_Period__c== 'Annual')){
a.August__c= 0;}
if((a.Sign_Up_Month__c == 'September')&&(a.Billing_Period__c== 'Annual')){
a.September__c= a.Amount_Breakdown__c;}
if((a.Sign_Up_Month__c <> 'September')&&(a.Billing_Period__c== 'Annual')){
a.September__c= 0;}
if((a.Sign_Up_Month__c == 'October')&&(a.Billing_Period__c== 'Annual')){
a.October__c= a.Amount_Breakdown__c;}
if((a.Sign_Up_Month__c <> 'October')&&(a.Billing_Period__c== 'Annual')){
a.October__c= 0;}
if((a.Sign_Up_Month__c == 'November')&&(a.Billing_Period__c== 'Annual')){
a.November__c= a.Amount_Breakdown__c;}
if((a.Sign_Up_Month__c <> 'November')&&(a.Billing_Period__c== 'Annual')){
a.November__c= 0;}
if((a.Sign_Up_Month__c == 'December')&&(a.Billing_Period__c== 'Annual')){
a.December__c= a.Amount_Breakdown__c;}
if((a.Sign_Up_Month__c <> 'December')&&(a.Billing_Period__c== 'Annual')){
a.December__c= 0;}
}
}
}
Hello, I am writing an apex class for the above trigger and so far I have only got to 42% of code coverage. I would appreciate some help on improving the apex class to reach 75% code coverage. Below is my apex class.
Public Class AnnualInvoice {
public static void AnnualInvoice() {
Invoice__c a = new Invoice__c(
Billing_Period__c = 'Annual',
Bill_To__c = 'Partner',
Amount__c = 100.52,
Sign_Up_Date__c = Null,
VAT_Number__c = '234929',
VAT_Exempt__c = true
);
update a;
}
}
I would start by creating more Invoice__c records in a testMethod that will cover all the conditions in your code (ex. one per month).
The logic in your trigger seems kind of redundant. For example: do you need to test Billing_Period__c on every line? Could you just test that once per line? Your month tests all seem contradictory.
Remember: the fewer the lines of code, the less you have to test :)
Hi Tom,
Thank you for the direction! I am an amature programmer and am trying to get my feet wet and some work done at the same time :)
Will post updated code and results shortly.
Regards,
V
I have updated my trigger as per your suggestion.......
trigger AnnualInvoiceAmountUpdates2 on Invoice__c (before insert, before update) {
if(trigger.isupdate){
for(Invoice__c a:trigger.new){
If(a.Billing_Period__c == 'Annual'){
if((a.Sign_Up_Month__c == 'January')){a.January__c= a.Amount_Breakdown__c;}
if((a.Sign_Up_Month__c <> 'January')){a.January__c= 0;}
if((a.Sign_Up_Month__c == 'February')){a.February__c= a.Amount_Breakdown__c;}
if((a.Sign_Up_Month__c <> 'February')){a.February__c= 0;}
if((a.Sign_Up_Month__c == 'March')){a.March__c= a.Amount_Breakdown__c;}
if((a.Sign_Up_Month__c <> 'March')){a.March__c= 0;}
if((a.Sign_Up_Month__c == 'April')){a.April__c= a.Amount_Breakdown__c;}
if((a.Sign_Up_Month__c <> 'April')){a.April__c= 0;}
if((a.Sign_Up_Month__c == 'May')){a.May__c= a.Amount_Breakdown__c;}
if((a.Sign_Up_Month__c <> 'May')){a.May__c= 0;}
if((a.Sign_Up_Month__c == 'June')){a.June__c= a.Amount_Breakdown__c;}
if((a.Sign_Up_Month__c <> 'June')){a.June__c= 0;}
if((a.Sign_Up_Month__c == 'July')){a.July__c= a.Amount_Breakdown__c;}
if((a.Sign_Up_Month__c <> 'July')){a.July__c= 0;}
if((a.Sign_Up_Month__c == 'August')){a.August__c= a.Amount_Breakdown__c;}
if((a.Sign_Up_Month__c <> 'August')){a.August__c= 0;}
if((a.Sign_Up_Month__c == 'September')){a.September__c= a.Amount_Breakdown__c;}
if((a.Sign_Up_Month__c <> 'September')){a.September__c= 0;}
if((a.Sign_Up_Month__c == 'October')){a.October__c= a.Amount_Breakdown__c;}
if((a.Sign_Up_Month__c <> 'October')){a.October__c= 0;}
if((a.Sign_Up_Month__c == 'November')){a.November__c= a.Amount_Breakdown__c;}
if((a.Sign_Up_Month__c <> 'November')){a.November__c= 0;}
if((a.Sign_Up_Month__c == 'December')){a.December__c= a.Amount_Breakdown__c;}
if((a.Sign_Up_Month__c <> 'December')){a.December__c= 0;}
}
}
}
}
For my Apex Class, I am having trouble with the Sign_Up_Date__c field, for some reason I get error messages when I try to pass a date to this field. The months are calculated based on the Sign_Up_Date field. Without being able to pass for each month in a 12 month cycle I cannot include every month. I would very much appreciate any suggestions or directions on what to do here. Thanks!
Public Class AnnualInvoice {
public static void AnnualInvoice() {
Invoice__c a = new Invoice__c(
Billing_Period__c = 'Annual',
Bill_To__c = 'Partner',
Amount__c = 100.52,
/*Sign_Up_Date__c = Date.valueof 2011-08-08,*/
VAT_Number__c = '234929',
VAT_Exempt__c = true
);
update a;
}
}
Can anyone help me on this? I just cannot get my Apex Class right...... :(
Thanks,
V