function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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. 
if(!UserwithCars.containsKey(Case.xyz__c)){
    UserwithCars.put(Case.xyz__c, String Value what you want to add);
}

Regards,
Avishek 
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) {
                    accIdSet.add(c.AccountId);
                }
            }
            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>());
                            UserwithCars.get(c.OwnerId).add(c.xyz__c);
                            UserwithCars.get(c.OwnerId).add(a.abc__c);
                        }
                    }
                }
            }
            system.debug('------' + UserwithCars);
            for(Case c : caseList) {
                if(UserwithCars.get(c.OwnerId).Contains(c.xyz__c)) {
                    listUsersWithMatchingModel.add(c.OwnerId);
                }
            }
            system.debug('listUsersWithMatchingModel------' + listUsersWithMatchingModel);
        } catch(Exception ex) {
            system.debug('Exception---ofLine--->'+ex.getLineNumber());
            system.debug('Exception---Message--->'+ex.getMessage());
        }
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi