You need to sign in to do that
Don't have an account?
create class for this trigger, which is the best way
trigger Hours on Hours_Professional__c(after insert, after update, after delete)
{
Hours_Professional__c newTransaction;
if (Trigger.isInsert || Trigger.isUpdate)
newTransaction = System.Trigger.new[0];
if (Trigger.isDelete)
newTransaction = System.Trigger.old[0];
Technical_Assistance__c a ;
a = [select id, Total_Hours_Professional__c from Technical_Assistance__c where id =
:newTransaction.Technical_Assistance__c limit 1];
if (newtransaction.Total_Hours_Professional__c!= 0) {
if (Trigger.isInsert) {
if (a.Total_Hours_Professional__c== null) a.Total_Hours_Professional__c= 0;
if (Trigger.isInsert && System.Trigger.new[0].Total_Hours_Professional__c!= null) {
a.Total_Hours_Professional__c+= newtransaction.Total_Hours_Professional__c;
}
} else {
Hours_Professional__c oldTransaction = System.Trigger.old[0];
if (Trigger.isUpdate) {
if (System.Trigger.new[0].Total_Hours_Professional__c!= null)
a.Total_Hours_Professional__c+= newtransaction.Total_Hours_Professional__c;
if (System.Trigger.old[0].Total_Hours_Professional__c!= null)
a.Total_Hours_Professional__c-= oldtransaction.Total_Hours_Professional__c;
}
if (Trigger.isDelete && System.Trigger.old[0].Total_Hours_Professional__c!= null) {
a.Total_Hours_Professional__c-= oldtransaction.Total_Hours_Professional__c;
}
}
update a;
}
}
-----------------------------------------------------
31% coverage
Assertion Failed: Expected: 0, Actual: 10000
public class Hours {
static testmethod void testHours() {
Technical_Assistance__c [] a =new Technical_Assistance__c [] {
new Technical_Assistance__c (Name= 'Test12', Total_Hours_Professional__c=0, Equipment_Installed__c='02iA0000000LhK3')
};
insert a;
Hours_Professional__c[] tr1 =new Hours_Professional__c[]{
new Hours_Professional__c(Technical_Assistance__c =a[0].id, Amount__c =1 )
};
insert tr1;
Technical_Assistance__c [] acctQuery11 = [SELECT Total_Hours_Professional__c FROM Technical_Assistance__c WHERE Name ='Test12'];
System.assertEquals(acctQuery11[0].Total_Hours_Professional__c,10000) ;
tr1[0]. Amount__c = 2;
update tr1;
Technical_Assistance__c [] acctQuery12 = [SELECT Total_Hours_Professional__c FROM Technical_Assistance__c WHERE Name ='Test12'];
System.assertEquals(acctQuery12[0].Total_Hours_Professional__c,20000) ;
delete tr1;
Technical_Assistance__c [] acctQuery13 = [SELECT Total_Hours_Professional__c FROM Technical_Assistance__c WHERE Name ='Test12'];
System.assertEquals(acctQuery13[0].Total_Hours_Professional__c,0) ;
}
}
Your assertions don't look right to me - when the Hours_Professional__c object is inserted, the trigger will add the value to the total on the parent Technical_Assistance__c object.
You've created a new Technical_Assistance__c, created a new Hours_Professional__c object with 1 hour in it, inserted it and then asserted that the total on the parent is 10000 - I'd expect it to be 1 in this case. Similar with the second transaction, you've updated the hours to 2 and asserted that the total is 20000.
I'd also suggest that you consider if a master-detail relationship for these objects is viable, as you could then use a roll up summary field and avoid apex code altogether.