0

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
}
1

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
}
  • Hi Santanu, thanks for the answer, I think I mislead the code, what I would like to achieve is if one of the task I query has a field "SubIssue__c" exist in the value from the map I created, then update the "Subject_Details__c" field with the corresponding key. For instance, 1 task has SubIssue__c = Issue Uploading Docs (KTP) , then the field Subject_Details__c should gets updated with key 'Registration'. How do I do this? given the value in map can be duplicated. – Adnan Zahry Jul 11 '19 at 6:43
  • Find the updated answer based on your comments – Santanu Boral Jul 11 '19 at 7:22
  • Im getting closer to undertand, one more thing Santanu, what if the subIssue1 & subIssue2 have few duplicate values? wouldn't that will confuse the method? This is why I thought to have map instead, but in the real use case, our user will fills 'SubIssue__c' which is the value that needs to be matched with the key, but in map, it has get(key) to match key with values not the other way around. Really appreciate your help Sir! – Adnan Zahry Jul 11 '19 at 8:00
  • Here key is hardcoded and based on the List of values you are putting into Map with key,value pair, if subIssue1 and subIssue2 have duplicate values then you need to decide which key to be taken, even if Map also you might face the same issue. In my code, rather than map I have used hardcoded key, basically the same thing – Santanu Boral Jul 11 '19 at 8:52
  • Understood, thank you very much Santanu!! – Adnan Zahry Jul 11 '19 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.