You need to sign in to do that
Don't have an account?
Test class for Trigger
Hi
I am bit confused inw riting a test class for the following trigger. Please help me out in this
trigger PerfMetricUpdate on Performance_Metric__c (after update)
{
Performance_Metric__c[] pm = Trigger.new;
if(pm[0].Monthly_Recurring_Other_Quota__c != Trigger.oldMap.get(pm[0].Id).Monthly_Recurring_Other_Quota__c ||
pm[0].Monthly_Recurring_Data_Quota__c != Trigger.oldMap.get(pm[0].Id).Monthly_Recurring_Data_Quota__c ||
pm[0].Monthly_CPE_Revenue_Quota__c != Trigger.oldMap.get(pm[0].Id).Monthly_CPE_Revenue_Quota__c ||
pm[0].Monthly_Recurring_Other_Amt_Attained__c != Trigger.oldMap.get(pm[0].Id).Monthly_Recurring_Other_Amt_Attained__c ||
pm[0].Monthly_Recurring_Data_Amt_Attained__c != Trigger.oldMap.get(pm[0].Id).Monthly_Recurring_Data_Amt_Attained__c ||
pm[0].Monthly_CPE_Revenue_Amt_Attained__c != Trigger.oldMap.get(pm[0].Id).Monthly_CPE_Revenue_Amt_Attained__c ||
pm[0].Monthly_CPE_Margin_Quota__c != Trigger.oldMap.get(pm[0].Id).Monthly_CPE_Margin_Quota__c ||
pm[0].Monthly_CPE_Margin_Amt_Attained__c != Trigger.oldMap.get(pm[0].Id).Monthly_CPE_Margin_Amt_Attained__c)
{
//delete the existing Quota Reporting records for this performance metric
Quota_Reporting__c[] deleteQR = [select id from Quota_Reporting__c where Performance_Metric__c = :pm[0].Id];
delete deleteQR;
//build the array
Quota_Reporting__c[] insertQR = new Quota_Reporting__c[0];
//start building the inserts of each type
Quota_Reporting__c qr = new Quota_Reporting__c(Performance_Metric__c = pm[0].Id, Type__c='Rec Data', Amount__c = pm[0].Monthly_Recurring_Data_Amt_Attained__c);
insertQR.add(qr);
Quota_Reporting__c qr1 = new Quota_Reporting__c(Performance_Metric__c = pm[0].Id, Type__c='Rec Data - Quota', Amount__c = pm[0].Monthly_Recurring_Data_Quota__c);
insertQR.add(qr1);
Quota_Reporting__c qr2 = new Quota_Reporting__c(Performance_Metric__c = pm[0].Id, Type__c='Rec Other', Amount__c = pm[0].Monthly_Recurring_Other_Amt_Attained__c);
insertQR.add(qr2);
Quota_Reporting__c qr3 = new Quota_Reporting__c(Performance_Metric__c = pm[0].Id, Type__c='Rec Other - Quota', Amount__c = pm[0].Monthly_Recurring_Other_Quota__c);
insertQR.add(qr3);
Quota_Reporting__c qr4 = new Quota_Reporting__c(Performance_Metric__c = pm[0].Id, Type__c='Non Rec', Amount__c = pm[0].Monthly_CPE_Revenue_Amt_Attained__c);
insertQR.add(qr4);
Quota_Reporting__c qr5 = new Quota_Reporting__c(Performance_Metric__c = pm[0].Id, Type__c='Non Rec - Quota', Amount__c = pm[0].Monthly_CPE_Revenue_Quota__c);
insertQR.add(qr5);
Quota_Reporting__c qr6 = new Quota_Reporting__c(Performance_Metric__c = pm[0].Id, Type__c='Non Rec Margin', Amount__c = pm[0].Monthly_CPE_Margin_Amt_Attained__c);
insertQR.add(qr6);
Quota_Reporting__c qr7 = new Quota_Reporting__c(Performance_Metric__c = pm[0].Id, Type__c='Non Rec Margin - Quota', Amount__c = pm[0].Monthly_CPE_Margin_Quota__c);
insertQR.add(qr7);
insert insertQR;
}
}
A very good article An Introduction to Writing Apex Test Method is posted on developer wiki.
Please check it out. If you still have any issue please feel free to post your queries.
with best,
Prafull G.