You need to sign in to do that
Don't have an account?
bathy
Improve Code Coverage for Apex trigger
Hi Guys,
I have developed a trigger to update parent record when a child record is changed.
here is the trigger for that:
trigger Updateoffercomms on pba__Closing__c (after update)
{ if(Trigger.IsUpdate)
{ Set<String> offerids= new Set<String>();
for (pba__Closing__c cl : Trigger.new)
{
if( (cl.Run_Updateoffercomms__c != Trigger.oldMap.get(cl.Id).Run_Updateoffercomms__c))
{ if(cl.Run_Updateoffercomms__c == true)
offerids.add(cl.pba__Offer__c);
}
} List<pba__Offer__c> offers= [SELECT Id,First_Commission__c, first_Commission_Date__c,Second_Commission__c,Gross_Commission__c FROM pba__Offer__c WHERE Id =: offerids];
Map<String, pba__Closing__c> cprmap = new Map<String, pba__Closing__c>();
for (pba__Closing__c cl : Trigger.new)
{
cprmap.put(cl.pba__Offer__c, cl);
}
for (pba__Offer__c o : offers)
{
if (cprmap.containsKey(o.Id))
{
o.First_Commission__c = cprmap.get(o.Id).X1st_Commission_Amount_payable__c; o.first_Commission_Date__c = cprmap.get(o.Id).Date_when_Contract_went_unconditional__c; o.Second_Commission__c = cprmap.get(o.Id).X2nd_Commission_amount_payable__c; o.Gross_Commission__c = cprmap.get(o.Id).Gross_Commission_Available__c;
}
}
update offers;
}
}
and the test class for this is :
@isTest
private class Mycprtest2
{
static testMethod void cprtriggertest2()
{
contact c = new Contact(FirstName = 'Yams1', LastName = 'Bat');
insert c;
pba__Listing__c L = new pba__Listing__c(RecordTypeId = '01290000000rIhl',Name = 'tess3' , pba__Address_pb__c = '66 cotterell rd' ,pba__City_pb__c = 'Petrie');
insert L;
pba__Offer__c o = new pba__Offer__c(pba__Listing__c = L.id ,pba__Contact__c = c.id);
date dtend = Date.newInstance(2015, 31 , 03);
pba__Closing__c cl = new pba__Closing__c(pba__Offer__c = o.id);
insert cl;
cl.Run_Updateoffercomms__c = true;
cl.X1st_Commission_Amount_payable__c = 90;
cl.X2nd_Commission_amount_payable__c = 68;
cl.Gross_Commission_Available__c = 78909;
cl.Date_when_Contract_went_unconditional__c = dtend;
update cl;
}
}
The problem here is my trigger is only getting 68% coverage. Any ways to improve it?
Any help is greatly appreciated.
Thanks in advance.
I have developed a trigger to update parent record when a child record is changed.
here is the trigger for that:
trigger Updateoffercomms on pba__Closing__c (after update)
{ if(Trigger.IsUpdate)
{ Set<String> offerids= new Set<String>();
for (pba__Closing__c cl : Trigger.new)
{
if( (cl.Run_Updateoffercomms__c != Trigger.oldMap.get(cl.Id).Run_Updateoffercomms__c))
{ if(cl.Run_Updateoffercomms__c == true)
offerids.add(cl.pba__Offer__c);
}
} List<pba__Offer__c> offers= [SELECT Id,First_Commission__c, first_Commission_Date__c,Second_Commission__c,Gross_Commission__c FROM pba__Offer__c WHERE Id =: offerids];
Map<String, pba__Closing__c> cprmap = new Map<String, pba__Closing__c>();
for (pba__Closing__c cl : Trigger.new)
{
cprmap.put(cl.pba__Offer__c, cl);
}
for (pba__Offer__c o : offers)
{
if (cprmap.containsKey(o.Id))
{
o.First_Commission__c = cprmap.get(o.Id).X1st_Commission_Amount_payable__c; o.first_Commission_Date__c = cprmap.get(o.Id).Date_when_Contract_went_unconditional__c; o.Second_Commission__c = cprmap.get(o.Id).X2nd_Commission_amount_payable__c; o.Gross_Commission__c = cprmap.get(o.Id).Gross_Commission_Available__c;
}
}
update offers;
}
}
and the test class for this is :
@isTest
private class Mycprtest2
{
static testMethod void cprtriggertest2()
{
contact c = new Contact(FirstName = 'Yams1', LastName = 'Bat');
insert c;
pba__Listing__c L = new pba__Listing__c(RecordTypeId = '01290000000rIhl',Name = 'tess3' , pba__Address_pb__c = '66 cotterell rd' ,pba__City_pb__c = 'Petrie');
insert L;
pba__Offer__c o = new pba__Offer__c(pba__Listing__c = L.id ,pba__Contact__c = c.id);
date dtend = Date.newInstance(2015, 31 , 03);
pba__Closing__c cl = new pba__Closing__c(pba__Offer__c = o.id);
insert cl;
cl.Run_Updateoffercomms__c = true;
cl.X1st_Commission_Amount_payable__c = 90;
cl.X2nd_Commission_amount_payable__c = 68;
cl.Gross_Commission_Available__c = 78909;
cl.Date_when_Contract_went_unconditional__c = dtend;
update cl;
}
}
The problem here is my trigger is only getting 68% coverage. Any ways to improve it?
Any help is greatly appreciated.
Thanks in advance.
(In test class you have not inserted pba__Offer__c )
Please perform insert dml on pba__Offer__c and also assign it to cl.pba__Offer__c so that you can get some data in the offerids collection.
Let me know for any issues/calrificationns.
Regards,
BDatla
Thanks for your suggestions. I inserted offer and the code coverage in sandbox is 100%now. but in the production while i deploy it, it says average code coverage is 71% must be atleast 75%. I am not sure what went wrong. in the sandbox it says 100% code coevrage. Any suggestions to increase the average code coerage.
Thanks,
Get the recordtype ID dynamically by querying recordtype object.
pba__Listing__c L = new pba__Listing__c(RecordTypeId = '01290000000rIhl',Name = 'tess3' , pba__Address_pb__c = '66 cotterell rd' ,pba__City_pb__c = 'Petrie');
example : - RecordType rt = [Select ID, Name From RecordType Where sObjectType = 'Opportunity' and Name ='somename']
;
Regards,
Varma Datla
Any luck after getting the recordtypeid dynamically ?
Regards,
BDatla
I tried your idea but it is still saying the same that the average code coverage is only 71% and must be atlease 75% . Any idea of what can be done to improve this?
Thanks,
any suggestions to improve the code coverage?
Please run the test method using developer console so that you can easily identify which section of the trigger is not having code coverage.
Regards,
BDatla
sorry for the delay in my reply.
I tried to execute the test class in my sandbox's devloper console and it is giving me the below error. Should I test this in my production's develpor Console?