function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bathybathy 

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.
 
PuneetsfdcPuneetsfdc
can you specify what lines are not covered, can you post snap shot showing colored code coverage.
(In  test class you have not inserted pba__Offer__c )
BDatlaBDatla
Hi Bathy,

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
bathybathy
hi Guys,

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,
BDatlaBDatla
Hi Bathy,

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
BDatlaBDatla
Hi Bathy,

Any luck after getting the recordtypeid dynamically ?

Regards,
BDatla
bathybathy
hi Varma,

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,
bathybathy
hi guys,

any suggestions to improve the code coverage?
BDatlaBDatla
Please let me know which section of the trigger is not getting covered so that i can help you.
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
PuneetsfdcPuneetsfdc
Yes that will be helpful and if possible can you also check your test class logs in case there is any exception, if test method status is fail/pass
bathybathy
hi Guys,
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?User-added image