0

I have a list of custom metadata type. I need to check if the string element is present in custom metadata type. Currently, I'm hardcoding the browser name in my if condition, but I need to compare this with the records present in custom metadata type. Can someone give a hand with this? I'm new to Apex. Thanks!

public boolean evaluate(TxnSecurity.Event e) 
{

    if (e.action == 'Login' ) 
    {
        List<Browsers__mdt> Brow = [Select Browser__c from Browsers__mdt];  

        LoginHistory loginAttempt =
        [SELECT Browser FROM LoginHistory
        WHERE Id = :e.data.get('LoginHistoryId') AND LoginType='Application'];
        String browser = loginAttempt.Browser;

        if (browser.startsWith('Chrome'))
        {
            return false;
        }
     } 
return true;
}
1

You just need to loop through the values:

for(Browsers__mdt setting: Brow) {
  if(browser.startsWith(setting.Browser__c)) {
    return false;
  }
}

This loop breaks early as soon as a matching browser is found.

  • I tried making above changes to my code but my class does not get saved. Any pointers as to what I might be doing wrong. Thanks for your help – VBALearner Nov 21 '18 at 1:54
  • @VBALearner Without an error or something, it could be anything. What error do you get? – sfdcfox Nov 21 '18 at 2:08
  • Thanks for you help. It wasn't closing the for loop properly. – VBALearner Nov 21 '18 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.