Categories
Apex Salesforce

How to compare values in Custom Metadata with given object in Apex

How to compare values in Custom Metadata with a given object field in Apex

learn more share more

Problem- Admin wants some value as configurable and set in custom metadata, now as a developer, we want to perform an action based on the matching of this mdt with values in say platform field on the account object

  public Boolean validatePlatform() {
        System.debug('Inside validatePlatform');
        List<String> lstOfPlatforms = new List<String>(); 
        Boolean isValidPlatform = false;
        String thePlatform = 'Amazon';

        //fetching the allowed sw platform from custom settings 
        List<Allowed_Platform__mdt> givenPlatforms = new List<Allowed_Platform__mdt>([
            SELECT SDFC_software_platform_value__c 
            from Allowed_Platform__mdt 
        ]);

        for(Allowed_Platform__mdt mdt :givenPlatforms ){
            lstOfPlatforms.add(mdt.platform__c);
        }
        System.debug('lstOfPlatforms = '+lstOfPlatforms );

        //incase list contains the custom settings then do the comparison with platform 
        if(!lstOfPlatforms.isEmpty() && lstOfPlatforms.contains(thePlatform)){
            System.debug('thePlatform = '+thePlatform );
            return true;
        }

        return isValidPlatform;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *