1

I have custom metadata records in the format:

enter image description here

If I store these Key and Values in a Map, how to have a conditional 'contains' check for the auxiliary word: Apartment_1

Use-case: I will need to compare the auxiliary value that is being passed by the user to my search. So, whenever my temporary search value contains the word: Apartment_1, I should be able to check if my map contains a key that matches part of the that temporary search value. And if yes, I should also compare the value of this key to another string coming as an another input.

Example:

Inputted temporary search key variable: Apartment_1

Inputted temporary search value variable: WindowLess

Scenario #1: Map contains Apartment_1 as a key == TRUE, but value == Window (which is not equal to WindowLess).

// SKIP

Scenario #2: Map contains Apartment_1 as a key == TRUE, value == Windowless

// DO SOME ACTION.

My implementation without using a Map for the values:

//Retrieving from CMD
List<String> auxiliaryList = new List<String>();
List<CustomMetadata__mdt> myCMDRecords = 
        [Select Id, Auixliary__c, Value__c From CustomMetadata__mdt];

if(!myCMDRecords .isEmpty( )) {

valueDevNames = new Set<String>(); 
 for(CustomMetadata__mdt rec : myCMDRecords ){
 auxiliaryList.add(rec.Auixliary__c);
 valueDevNames.add(rec.Value__c)
}

//Creating a map of Queues
if(!auxiliaryList.isEmpty()){
                queueMap = new Map<Id, Group>([Select Id, Name From Group 
                                               Where Type = 'Queue' And Name = :auxiliaryList]);
    }
}


//Implementation logic
Set<Id> caseIdSet = new Set<Id>();
if(!queueMap.isEmpty() && !valueDevNames.isEmpty()){
 for(Case caseRec: [Select Id, OwnerId, RecordType.DeveloperName From Case Where Id IN : passedCaseIds]){
          if(queueMap.containsKey(caseRec.OwnerId) && valueDevNames.contains(caseRec.RecordType.DeveloperName)){
             caseIdSet.add(caseRec.Id);
        }
    }
}
6
  • do you have any code you've attempted? You basically wrote out the steps. Query all custom metadata records, utilize maps and for loops, and do some if else checks . Dec 16, 2019 at 20:14
  • Yes, currently I am using a Set variable to store the above values. And I am checking if the inputted Value exists in the provided Set. I will post my code soon. Dec 16, 2019 at 20:18
  • What is the question here? Dec 16, 2019 at 21:12
  • @ZachHutchins If I store the Key and Values in a Map (from above screenshot), how to have a conditional 'contains' check for an inputted string within the Keys? Dec 16, 2019 at 21:15
  • @AustinEvans From where are you getting the Key? is it stored on the Mdt record?
    – samdev
    Dec 16, 2019 at 21:15

1 Answer 1

0

Is this what you are talking about?

private List<Map<string,object>> MapKeysContain(Map<string,object> map, string comparer){
    Set<string> keys = map.keySet();
    Map<string,object> m_subsetMap = new Map<string,object>();
    for(string k : keys)
    {
        if(k.contains(comparer))
        {
            m_subSetMap.put(key, map.get(k));
        }
    }
    // Just here for example, can do a lot with this like map.size() > 0
    return m_subSetMap;

}

You must log in to answer this question.

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