+ Start a Discussion

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;
        } //end if
    }//end for

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

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



static testMethod void testManagedCareProducts(){



Insert the record which meets all the conditions;


Query for it and

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