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

How can I improve my code coverage?

I'm currently only getting a 50% code coverage. How do I increase it to closer to 100%?






trigger PPM_CreateEquipmentDetailsRecord on PPM_Project__c (after update) {
    List <PPM_Equipment_Details_History__c> RecToInsert = new List <PPM_Equipment_Details_History__c> ();
    for (PPM_Project__c PPMP : {
        PPM_Project__c beforeUpdate = System.Trigger.oldMap.get(PPMP.Id);
        if (beforeUpdate.of_Changes_to_Equipment_Details__c != PPMP.of_Changes_to_Equipment_Details__c) {
            PPM_Equipment_Details_History__c PPMBDH = new PPM_Equipment_Details_History__c ();
            PPMBDH.Equipment_Details_Old_Value__c = beforeUpdate.Equipment_Details__c;
            PPMBDH.Equipment_Details_New_Value__c = PPMP.Equipment_Details__c;
            PPMBDH.Project__c = PPMP.Id;
    try {
        insert RecToInsert; 
    } catch (system.Dmlexception e) {
        system.debug (e);


Test Class:


public class TestClass {
    Static testmethod void methodname() {
         List<PPM_Equipment_Details_History__c> recToInsert = new List<PPM_Equipment_Details_History__c>();
         List<PPM_project__c>  ppc = new List<PPM_Project__c>();
         List<PPM_project__c>  ppcnew = new List<PPM_Project__c>();
         for(Integer i=0; i<=10;i++) {
              PPM_project__c  ppc1 = new PPM_Project__c(name = 'i+s', requiredfields for inserting ppm project object);
       insert ppc;
       for(PPM_project__c pp:ppc) {
            pp.of_Changes_to_Equipmet_Details__c = "give the new value)
        update ppcnew;



1 - You can see code coverage either through Eclipse Run Test on the test Class or by using the UI and clicking Run Test on the Test Class. This will tell you the line numbers that are not tested


2 - I'm going to guess that the problem is here:


if (beforeUpdate.of_Changes_to_Equipment_Details__c != PPMP.of_Changes_to_Equipment_Details__c)

 and that you are not positively setting the value of this field in the insert to something other than in the update; System.debug statements just before this if can confirm this




are you running same test class with out changing any thing?


 PPM_project__c  ppc1 = new PPM_Project__c(name = 'i+s', requiredfields for inserting ppm project object);

in above line are you passing all fields for creating object.


send me which lines are not covered?