Skip to main content
Merged in Trigger from comment.
Source Link
Daniel Ballinger
  • 103.1k
  • 40
  • 275
  • 601

Trigger:

trigger AccountTrigger on Account (after update) {
    String customMetadataField = 'Disable_Account_c_AU__c';
    Schema.SObjectType accountType = Schema.Account.sObjectType;
    accountType = Account.sObjectType;
    Boolean enableHandler = AccountTriggerCheckDisabled.IsHandlerDisabled(accountType, customMetadataField);
    if(enableHandler){
        new AccountTriggerHandler().run();
    }
} 

Trigger:

trigger AccountTrigger on Account (after update) {
    String customMetadataField = 'Disable_Account_c_AU__c';
    Schema.SObjectType accountType = Schema.Account.sObjectType;
    accountType = Account.sObjectType;
    Boolean enableHandler = AccountTriggerCheckDisabled.IsHandlerDisabled(accountType, customMetadataField);
    if(enableHandler){
        new AccountTriggerHandler().run();
    }
} 
Source Link

Testing Metadata Using Mock Call Outs to Static Resources

I have a class that is called before the handler method of a trigger to check and see if a Custom Metadata Field is checked and therefore the code in the trigger handler should not be run. I can verify this works if you manually check the box in Manage Custom Metadata Settings, however I need to get increased coverage on it. Currently the tests for the trigger provide enough coverage to deploy, but barely enough. I need to be able to feed the value of true to the Custom Metadata within a test class. So, at Daniel Balinger's suggestion, I am attempting to do that using a mock call out as well as the JSON serialize/deserialize magic that was suggested by Patlatus and the expanded example from @andrew-fawcett here: Unit Test - Creating Custom Metadata I finally got the test to run without errors, but from the debug log and the lack of code coverage, I can tell it's not working correctly.

Here is my code; any help is appreciated, as I only did a Trailhead module that involved mock call outs a year and a half ago, and have never serialized/deserialized JSON.

The Static Resource:

{
    "Disa\ble_Accou\n\t_c_AU__c": "\t\rue",
    "Disa\ble_A\t\tachme\n\t_AI__c": "\t\rue",
    "Disa\ble_A\t\tachme\n\t_AU__c": "\t\rue",
    "Disa\ble_Coop_Dolla\rs_c_AD__c": "\t\rue",
    "Disa\ble_Coop_Dolla\rs_c_AI__c": "\t\rue",
    "Disa\ble_Coop_Dolla\rs_c_AU__c": "\t\rue",
    "Disa\ble_Speci\fica\tio\n_C\redi\t_c_AI__c": "\t\rue",
    "Disa\ble_Speci\fica\tio\n_C\redi\t_c_BI__c": "\t\rue",
    "Disa\ble_Use\r_AI__c": "\t\rue"
}

The Calloout Class:

public class CalloutToStaticMetadataClass {
    public static HttpResponse getCustomMetadata(String endpoint){
        HttpRequest req = new HttpRequest();
        req.setEndpoint(endpoint);
        req.setMethod('GET');
        Http h = new Http();
        HttpResponse res = h.send(req);
        return res;
    }
}

The test:

@isTest
public class TriggersTurnedOff_Test {     
    @isTest
    public static void AreTheTriggersTurnedOff(){
        Map<String, Object> metadataSettings = new Map<String, Object>();
        // Use StaticResourceCalloutMock built-in class to
        // specify fake response and include response body 
        // in a static resource.
        StaticResourceCalloutMock mock = new StaticResourceCalloutMock();
        mock.setStaticResource('CustomMetadataStaticResource_Escaped');
        mock.setStatusCode(200);
        mock.setHeader('Content-Type', 'application/json');
        
        // Set the mock callout mode
        Test.setMock(HttpCalloutMock.class, mock);
        
        // Call the method that performs the callout
        HTTPResponse res = CalloutToStaticMetadataClass.getCustomMetadata('http://example.com/example/test'); 
        
        if(res.getStatusCode() == 200){
            // metadataSettings = (Map<String, String>) JSON.deserializeUntyped(res.getBody());
            metadataSettings = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
            System.debug('&*&**&**&*& metadataSettings &*&**&**&*&' + metadataSettings);
        }
        Disable_Triggers__mdt triggerCheckboxes = new Disable_Triggers__mdt();
        triggerCheckboxes = (Disable_Triggers__mdt) JSON.deserialize(JSON.serialize(metadataSettings), Disable_Triggers__mdt.class);
        System.debug('^&^&^&^&^ triggerCheckboxes ^&^&^&^&^' + triggerCheckboxes);
    }
    
/*    
    public static void deserializeMetadata(){
        
        Disable_Triggers__mdt triggerCheckboxes = new Disable_Triggers__mdt();
        triggerCheckboxes = (Disable_Triggers__mdt) JSON.deserialize(JSON.serialize(metadataSettings), Disable_Triggers__mdt.class);
        System.debug('^&^&^&^&^ triggerCheckboxes ^&^&^&^&^' + triggerCheckboxes);
    }
*/
       
}

Picture of the resulting debug log.

The Custom Metadata Object.