-2

Currently we have hard coded the states

**private static Set<String> allowstates = new Set<String>{'California','New Hampshire','New York'};**
if (!allowstates.contains(bank.State__c)) {
        AuraHandledException e = new AuraHandledException('Exception');
        e.setMessage('Error.');            
}

I want to pull these states from Metadata table

state__mdt[] allowStates = [select Label from state__mdt];

obviously this is giving me the list of states. (State__mdt:{Label=california, Id=XXXXX}, State__mdt:{Label=NewYork, Id=XXXXX})

How can I use this metadata results in the if condition and check if it is not in the bank.state__c

Thanks!

  • where exactly are you stuck? have you tried implementing the code? please include what you have ried so far so other community members can provide pointers in regards to specific code problems you are facing. – glls Aug 8 '18 at 23:04
  • The above code works but we have hard coded the states in the class, instead I want to use Metadata table to fetch the states and check if that state exists in bank.state__c. – Sfdc learner Aug 8 '18 at 23:12
2

You just generate exactly the same Set<String> by iterating your records.

Set<String> whitelist = new Set<String>();
for (MyMetadata__mdt record : [SELECT Label FROM MyMetadata__mdt])
{
    whitelist.add(record.Label);
}

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.