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
jpbenjpben 

Help with Test Class

Hi All,

 

I am new to Apex coding.  I need help with creating a test class for my trigger below.  Thanks in advance.

 

 

 

trigger ManagedCareProducts on Contract (after insert) {

//When Managed Care Contract is created, a list of Service Line items will be 
//inserted into the Contract record.

    List <Contract_Products__c> CPToInsert = new List <Contract_Products__c> (); 
    
    for (Contract cont : Trigger.new) {
        
        if(cont.RecordTypeId == '012Z00000004TIC')  {
     
        
        //New Service Line: Drug//
        Contract_Products__c CP1 = new Contract_Products__c (); 
            CP1.Name = 'Drug';
            CP1.Product__c = '01tZ0000000N38u';
            CP1.Contract__c = cont.Id;
           
      //New Service Line: E&M//
        Contract_Products__c CP2 = new Contract_Products__c (); 
            CP2.Name = 'E&M';
            CP2.Product__c = '01tZ0000000N38z';
            CP2.Contract__c = cont.Id;
            
      //New Service Line: Chemo Admin//
        Contract_Products__c CP3 = new Contract_Products__c (); 
            CP3.Name = 'Chemo Admin';
            CP3.Product__c = '01tZ0000000Npbs';
            CP3.Contract__c = cont.Id;
            
        //New Service Line: Radiation//
        Contract_Products__c CP4 = new Contract_Products__c (); 
            CP4.Name = 'Radiation';
            CP4.Product__c = '01tZ0000000NpcG';
            CP4.Contract__c = cont.Id;
            
        //New Service Line: Diagnostic//
        Contract_Products__c CP5 = new Contract_Products__c (); 
            CP5.Name = 'Diagnostic';
            CP5.Product__c = '01tZ0000000Npc1';
            CP5.Contract__c = cont.Id;
            
       //New Service Line: Gyn//
        Contract_Products__c CP6 = new Contract_Products__c (); 
            CP6.Name = 'Gyn';
            CP6.Product__c = '01tZ0000000Npc6';
            CP6.Contract__c = cont.Id;
            
        //New Service Line: PET//
        Contract_Products__c CP7 = new Contract_Products__c (); 
            CP7.Name = 'PET';
            CP7.Product__c = '01tG0000002mSlb';
            CP7.Contract__c = cont.Id;
            
       //New Service Line: Lab//
        Contract_Products__c CP8 = new Contract_Products__c (); 
            CP8.Name = 'Lab';
            CP8.Product__c = '01tZ0000000NpcB';
            CP8.Contract__c = cont.Id;
            
        //New Service Line: Surgical//
        Contract_Products__c CP9 = new Contract_Products__c (); 
            CP9.Name = 'Surgical';
            CP9.Product__c = '01tZ0000000NpcL';
            CP9.Contract__c = cont.Id;
            
        //New Service Line: Other//
        Contract_Products__c CP10 = new Contract_Products__c (); 
            CP10.Name = 'Other';
            CP10.Product__c = '01tZ0000000NpcQ';
            CP10.Contract__c = cont.Id;
             
        
                   
        CPToInsert.add(CP1);
        CPToInsert.add(CP2);
        CPToInsert.add(CP3);
        CPToInsert.add(CP4);
        CPToInsert.add(CP5);
        CPToInsert.add(CP6);
        CPToInsert.add(CP7);
        CPToInsert.add(CP8);
        CPToInsert.add(CP9);
        CPToInsert.add(CP10); 
        } //end if
        
    }//end for

     try {
        insert CPToInsert; 
    } catch (system.Dmlexception e) {
        system.debug (e);
    }

Best Answer chosen by Admin (Salesforce Developers) 
Suresh RaghuramSuresh Raghuram

@isTest

 

static testMethod void testManagedCareProducts(){

 

test.StartTest();

Insert the record which meets all the conditions;

 

Query for it and

chek it in system.assert(x=y,'Error Message');

 

test.StopTest();

 

}