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
sharrissharris 

Need help with writing test methods for 2 triggers

Could someone help me figure out how to get 75% coverage with this trigger I created on a custome object? Thank you.

 

// TRIGGER CODE=========================================

 

trigger addArtifactTemplates on NI_Change_Control__c (after insert)
{

    if (trigger.isInsert)
    { 

        for (NI_Change_Control__c nicc : Trigger.new)
        {         

            // IF RECORD TYPE IS "Hosted_RFC"
            if (nicc.RecordTypeId == '012P00000000G42') 

            {

                NICC_Artifact__c a1 = new NICC_Artifact__c(NI_Change_Control__c = nicc.Id, Type__c = 'Communication Plan');        
                insert a1;

                NICC_Artifact__c a2 = new NICC_Artifact__c(NI_Change_Control__c = nicc.Id, Type__c = 'Deployment Plan');        
                insert a2;

                NICC_Artifact__c a3 = new NICC_Artifact__c(NI_Change_Control__c = nicc.Id, Type__c = 'Monitoring Plan');        
                insert a3;

                NICC_Artifact__c a4 = new NICC_Artifact__c(NI_Change_Control__c = nicc.Id, Type__c = 'Rollback Plan');        
                insert a4;

                NICC_Artifact__c a5 = new NICC_Artifact__c(NI_Change_Control__c = nicc.Id, Type__c = 'Test Plan');        
                insert a5;

            }

        }

    }

}

 

// MY TEST CLASS ATTEMPT ==============================


@isTest
private class TestNIChangeControl
{
    static testMethod void testaddArtifactTemplates()
    {
         NI_Change_Control__c nicc = new NI_Change_Control__c (Request_Summary__c = 'Test Request Summary Text...', Date_Required__c = System.today());
         insert nicc;
        
         NICC_Artifact__c a1 = new NICC_Artifact__c(NI_Change_Control__c = nicc.Id, Type__c = 'Communication Plan');        
         insert a1;

         NICC_Artifact__c a2 = new NICC_Artifact__c(NI_Change_Control__c = nicc.Id, Type__c = 'Deployment Plan');        
         insert a2;

         NICC_Artifact__c a3 = new NICC_Artifact__c(NI_Change_Control__c = nicc.Id, Type__c = 'Monitoring Plan');        
         insert a3;

         NICC_Artifact__c a4 = new NICC_Artifact__c(NI_Change_Control__c = nicc.Id, Type__c = 'Rollback Plan');        
         insert a4;

         NICC_Artifact__c a5 = new NICC_Artifact__c(NI_Change_Control__c = nicc.Id, Type__c = 'Test Plan');        
         insert a5;
               
    }
}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
dmchengdmcheng

You need to specify the record type of the NI_Change_Control__c you create in the test so that it will fire the trigger.

 

Also - I'm not sure why you are inserting NICC_Artifact__c records in your test.  Instead, you need to retrieve the NICC_Artifact__c records that were created by the trigger and do system.AssertEquals to verify they were created correctly.

All Answers

dmchengdmcheng

You need to specify the record type of the NI_Change_Control__c you create in the test so that it will fire the trigger.

 

Also - I'm not sure why you are inserting NICC_Artifact__c records in your test.  Instead, you need to retrieve the NICC_Artifact__c records that were created by the trigger and do system.AssertEquals to verify they were created correctly.

This was selected as the best answer
sharrissharris

Thank you dmcheng. Adding the RecordTypeId to the NI_Change_Control__c insert did the trick. Thank you very much. If I wasn't such a newbie to creating Test Classes I probably would have used the system.AssertEquals as you mentioned.