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
snovvblindsnovvblind 

Apex Test Classes Help

Hi All, I'm new to apex and I'm having trouble writing a test classes. :( How can I go about getting at least a 80% code coverage?

 

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 : Trigger.new) {
        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;
            RecToInsert.add(PPMBDH);
        }
    }
    try {
        insert RecToInsert; 
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
sivaextsivaext

Hi 

@isTest

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);

              ppc.add(ppc1);

       }

       insert ppc;

       for(PPM_project__c pp:ppc) {

            pp.of_Changes_to_Equipmet_Details__c = "give the new value)

            ppcnew.add(pp);

       }

        update ppcnew;

        

              

}

}

All Answers

Vinit_KumarVinit_Kumar

Try below,

 

public class mytestClass{
  
     static testMethod void myMethod(){

 

PPM_Project__c pm = new PPM_Project__c(of_Changes_to_Equipment_Details__c = 'ABC');// put all the required fields values to insert records

 

insert pm;

 

pm.of_Changes_to_Equipment_Details__c='XYZ';

update pm;

 

}

}

sivaextsivaext

Hi 

@isTest

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);

              ppc.add(ppc1);

       }

       insert ppc;

       for(PPM_project__c pp:ppc) {

            pp.of_Changes_to_Equipmet_Details__c = "give the new value)

            ppcnew.add(pp);

       }

        update ppcnew;

        

              

}

}

This was selected as the best answer
snovvblindsnovvblind

Hi Siva,

 

I'm currently only getting a 50% code coverage. How can I improve this?

 

Apologies, I'm still learning my way around the platform.