+ Start a Discussion
Priyesh Misquith 12Priyesh Misquith 12 

converting map<Id,list<string>> by comapring field to list <string>

I have a map like this which cointain the all the value like id=userid and the values from account field (Account.abc__c
)and case field(Case.xyz__c).

map<Id,list<String>> UserwithCars = new map<Id,list<String>>();

Case.xyz__c (consider it as a brand)
Account.abc__c (consider it as sub-brand)

when user creates the case record  with the account 

I want to strore the userid of the user based on the Case.xyz__c value to the below 
ie is if the UserwithCars map cointains the value which is present in Case.xyz__c then it should add userid to the below list 

List<String> listUsersWithMatchingModel = new List<String>(); 
Avishek Nanda 14Avishek Nanda 14
Hi Priyesh,

This is how you can check and put the value to Map. 
    UserwithCars.put(Case.xyz__c, String Value what you want to add);

Ajay K DubediAjay K Dubedi
Hi Priyesh,
Try the following code and if you face any problem or is not working fine then share with me:
public class CheckListOfMap {
    public static void checkString() {
        List<Case> caseList = new List<Case>();
        Set<Id> accIdSet = new Set<Id>();
        List<Account> accList = new List<Account>();
        map<Id, list<String>> UserwithCars = new map<Id, list<String>>();
        List<String> listUsersWithMatchingModel = new List<String>();
        try {
            caseList = [SELECT Id, xyz__c, AccountId, OwnerId FROM Case WHERE AccountId != null AND xyz__c != null LIMIT 10];
            for(Case c : caseList) {
                if(caseList.size() > 0) {
            if(accIdSet.size() > 0) {
                accList = [SELECT Id, abc__c FROM Account WHERE Id IN : accIdSet AND abc__c != null];
            if(caseList.size() > 0 && accIdSet.size() > 0) {
                for(Case c : caseList) {
                    for(Account a : accList) {
                        if(!UserwithCars.containsKey(c.OwnerId)) {
                            UserwithCars.put(c.OwnerId, new List<String>());
            system.debug('------' + UserwithCars);
            for(Case c : caseList) {
                if(UserwithCars.get(c.OwnerId).Contains(c.xyz__c)) {
            system.debug('listUsersWithMatchingModel------' + listUsersWithMatchingModel);
        } catch(Exception ex) {
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Ajay Dubedi