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 Answer 1

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.

3
  • 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
    Commented Nov 21, 2018 at 1:54
  • @VBALearner Without an error or something, it could be anything. What error do you get?
    – sfdcfox
    Commented Nov 21, 2018 at 2:08
  • Thanks for you help. It wasn't closing the for loop properly.
    – VBALearner
    Commented Nov 21, 2018 at 15:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .