[SalesForce] How to check if a field value contains a value from map

I have a map that is populated with list, and also list collection from task object. How do update the task that is retrieved from the query based on certain conditions?

Map<String, List<String>> mapname = new Map<String, List<String>>();
//Create the subissue list

 List<String> subIssue1 = new List<String>{'Issue Uploading Docs (KTP)',
                                           'Issue Uploading Docs (KK)',
                                           'Issue Uploading Docs (NPWP)',
                                           'Issue Uploading Docs (All)',
                                           'Wrong Docs Request to re-submit (KTP)',
                                           'Wrong Docs Request to re-submit (NPWP)',
                                           'Wrong Docs Request to re-submit (KK)'};
List<String> subIssue2 = new List<String>{'Member Activation Request (Manager)',
                                          'Member Activation Request (VP)',
                                          'Member Activation Request (TL)'};

//put subIssue1 list into map
        mapname.put('Registration', subIssue1);                               
        System.debug(mapname.keyset());
//put subIssue2 list into map
        mapname.put('Activation', subIssue2);
        System.debug(mapname.keyset());

//get task related to subissue
List<Task> taskWithSub = [SELECT Id, SubIssue__c, CustomerHappiness__c,
Subject_Details__c FROM Task WHERE RecordTypeId = '0121p000000TgP6' AND
SubIssue__c != null AND Subject_Details__c = 'It has been changed'  ];
system.debug(taskWithSub.size());
system.debug(taskWithSub);

Here, I want to check if field SubIssue__c from each task contains one of the values from map, I would like to update Subject_Details__c field with the match key in map.

IF(taskWithSub.size()>0) 
{
//Iterate all activities with subIssue using for loop 
for(Task twh : taskWithSub){
     if(mapname.get(twh.SubIssue__c) != null){
        //code block
        //if twh.SubIssue__c contains value from mapname then 
        //Subject_Details__c = //get the map key
    }   
} 

update taskWithSub; here update
}

Best Answer

Rather than loop through for loop, you should add that lstSubIssues in the query.

You don't need to create separate Map for this lists, Since for subIssue1, Subject_Details__c = 'Registration', similarly for subIssue2, twh.Subject_Details__c = 'Activation'.

After getting the list of Task, loop though the tasks and use subIssue1.contains(twh.SubIssue__c) to find the matching values and then update accordingly.

Note: Don't try to hardcode RecordtypeId, rather use from metadata.

Here is entire code:

List<String> subIssue1 = new List<String>{'Issue Uploading Docs (KTP)',
                                           'Issue Uploading Docs (KK)',
                                           'Issue Uploading Docs (NPWP)',
                                           'Issue Uploading Docs (All)',
                                           'Wrong Docs Request to re-submit (KTP)',
                                           'Wrong Docs Request to re-submit (NPWP)',
                                           'Wrong Docs Request to re-submit (KK)'};
List<String> subIssue2 = new List<String>{'Member Activation Request (Manager)',
                                          'Member Activation Request (VP)',
                                          'Member Activation Request (TL)'};
List<String> lstSubIssues = new List<String>();
lstSubIssues.addAll(subIssue1);
lstSubIssues.addAll(subIssue2);                   

List<Task> taskWithSub = [SELECT Id, SubIssue__c, CustomerHappiness__c, Subject_Details__c 
FROM Task 
WHERE RecordTypeId = '<recordTypeId>' 
AND SubIssue__c IN:lstSubIssues  
AND Subject_Details__c = 'It has been changed'  ];

if(!taskWithSub.isEmpty()){
    for(Task twh : taskWithSub){
        if(subIssue1.contains(twh.SubIssue__c)) {
            twh.Subject_Details__c = 'Registration';
        }
        if(subIssue2.contains(twh.SubIssue__c)) {
            twh.Subject_Details__c = 'Activation';
        }  
    } 

    update taskWithSub; //here update
}