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
Rick HaagRick Haag 

Test Class for Trigger with IF using Metadata

Hello,
I am attempting to write a test class for a trigger where I am leveraging Custom Metadata to Activate/Deactivate the trigger.  I am not able to generate code coverage for both scenarios as I cannot insert test data for Custom Metadata.

In the past I used Custom Settings for trigger activation/deactivation and that it was simple enough to insert to test the "if" and the "other".  I understand Custom Metadata is the future, so here I am with incomplete code coverage.

Any recommendations on how to do this is appreciated. I have posted the test class below and added the bit for Custom Metadata, but as mentioned it isn't valid, but added to provide a sense of what I am attempting to do.

Trigger
trigger OpportunityContactRole on OpportunityContactRole (after insert, after update, after delete) {

    
    //Checks if Custom Metadata on for Trigger is Active
    List<Trigger__mdt> metadataList =  new List<Trigger__mdt>();
    metadataList = [SELECT Id FROM Trigger__mdt WHERE isActive__c != NULL AND Label = 'OpportunityContactRole'];
    
    if(metadataList.size()==0) 
    {
        return;
    }
    
    for(OpportunityContactRole ocr : Trigger.new==null ? Trigger.old : Trigger.new)
        
    {
            ------Run Stuff Here----
    }
    
}

Test Class
@isTest
private class OpportunityContactRoleTest
{

    @isTest
    static void triggerOn()
    {              
        Account acc = new Account(name='test acc');
        insert acc;
        Contact con = new Contact(lastname='test con',accountid=acc.id);
        insert con;
        Opportunity opp = new Opportunity(AccountId=acc.id,Amount=1,Name = 'test opp',StageName = 'Closed Lost',CloseDate=Date.today());
        insert opp;

        //This is will work for Custom Settings but not for Custom Metadata
        Trigger_mdt  setting = new TriggerManager__mdt();
	setting.Label = 'OpportunityContactRole';
        setting.isActive__c = TRUE;
	insert setting;
        
        //Insert OpportunityContactRole
	OpportunityContactRole ocr = new OpportunityContactRole();
	ocr.ContactId = con.Id;
	ocr.OpportunityId = opp.Id;
	ocr.IsPrimary = TRUE;
	ocr.Role = 'Decision Maker';
        test.starttest();
        insert ocr;
        
        test.stoptest();
        
    }


    @isTest
    static void triggerOff()
    {             
        Account acc = new Account(name='test acc');
        insert acc;
        Contact con = new Contact(lastname='test con',accountid=acc.id);
        insert con;
        Opportunity opp = new Opportunity(AccountId=acc.id,Amount=1,Name = 'test opp',StageName = 'Closed Lost',CloseDate=Date.today());
        insert opp;
     
        //Insert OpportunityContactRole
	OpportunityContactRole ocr = new OpportunityContactRole();
	ocr.ContactId = con.Id;
	ocr.OpportunityId = opp.Id;
	ocr.IsPrimary = TRUE;
	ocr.Role = 'Decision Maker';
        test.starttest();
        insert ocr;
        
        test.stoptest();
        
    }
    
    
}


 
Danish HodaDanish Hoda
Hi Rick,

You don't need to insert Custom metadata records in the test class, the system fetches the metadata records, unlike the custom settings.
Rick HaagRick Haag
Danish, Thanks for the response.  I should have been more clear in my initial post.  What I am uncertain of is how does one test two or more scenarios. The Test Class is dependent on the current "System" setting/values of the Custom Metadata. I don't know of a way to change the value of the Custom Metadata while a test is running so the "if/else" is covered in the trigger.

In my trigger, there is one scenario where the metadata field is "not NULL". This gets the code coverage 14 of 15

I am not able to test "if" part of the trigger.  How do I test the "NULL", (if), scenerio when the actual setting of the Custom Metadata in the in the "System" is "Not NULL".

Scenerio #1 in Test Class
"metadataList = [SELECT Id FROM Trigger__mdt WHERE isActive__c != NULL AND Label = 'OpportunityContactRole']"
This works because isActive = TRUE (code coverage 14/15)

Scenerio #2 in Test Class
metadataList = [SELECT Id FROM Trigger__mdt WHERE isActive__c = NULL AND Label = 'OpportunityContactRole']
This scenario doesn't exist in the test class because Scenerio #1 persists as it is in the Custom Metadata. Therefore no Code Coverage.

Not sure how to account for "Scenario #2" in the Test Class so I get coverage for Line 10 in the trigger and thus 15/15 code coverage.
Danish HodaDanish Hoda
Hi Rick,
If you want to use custom metadata and test all the scenarios, then try creating two fields isActive1__c and isActive2__c and modify the logic in apex class.
Rick HaagRick Haag
Danish, Thanks again for the replies and recommendations.