+ Start a Discussion

Map two unrelated objects

List<Case> caseList =  new List<Case>();
        Set<ID> recordtypeset = new Set<ID>();
        Set<ID> producttypeset =new Set<ID>();
        for (Case eachCase : lstTriggerNew){
            if(eachCase.Status == 'Ready for Assignment' && Trigger.oldMap.get(eachCase.Status)!= 'Ready for Assignment')
            {  caseList.add(eachCase);  } 
        if(caseList!=NULL && (!caseList.isEmpty())){
            List<WorkType> WT = [SELECT ID, Name FROM WorkType WHERE NU_Case_Type__c =: recordtypeset AND NU_Product_Type__c =: producttypeset];

Highlighted above are the required WorkType and Case Lists.
Requirement: Create a map between WorkType and Case.
Constraint : Case and WorkType are not related together .
Note : There is a custom field NU_Product_Type__c  which is common and required in both the objects.


First of all, include NU_Case_Type__c and NU_Product_Type__c to the queried fields. After your WorkType SELECT, I propose that you create a map from case record type to product type and collect all the related WorkTypes there with following:
Map<String, Map<String, List<WorkType>> recordTypeToProductTypeMap = new Map<String, Map<String, List<WorkType>>();
for(WorkType workType : WT) {
    if(!recordTypeToProductTypeMap.containsKey(workType.NU_Case_Type__c)) {
        recordTypeToProductTypeMap.put(workType.NU_Case_Type__c, new Map<String, List<WorkType>());
    Map<String, List<WorkType> productTypeMap = recordTypeToProductTypeMap.get(workType.NU_Case_Type__c);
    if(!productTypeMap.containsKey(workType.NU_Product_Type__c) {
        productTypeMap.put(workType.NU_Product_Type__c, new List<WorkType>());
Then to get the list for each case you can use the following:
for(Case eachCase : lstTriggerNew) {
   if(recordTypeToProductTypeMap.containsKey(eachCase.RecordTypeId) && recordTypeToProductTypeMap.get(eachCase.RecordTypeId).containsKey(eachCase.NU_Product_Type__c)) {
       List<WorkType> relatedWorkTypes = recordTypeToProductTypeMap.get(eachCase.RecordTypeId).get(eachCase.NU_Product_Type__c);
        //Do something with the worktypes