[SalesForce] How to find a Map key (with possible contains text)

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);
        }
    }
}

Best Answer

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;

}
Related Topic