+ Start a Discussion
ForceRookieForceRookie 

How to avoid nested loop using Map?

Can somebody help me to change my code using Map to avoid nested loops? I’m still confused with Maps.
public class UpdateParentCampaignHandler {
	public static Boolean isFirstRun = true;
	public static void updateParentCampaign(List<Campaign__c> scope) {
        if (isFirstRun) {
			isFirstRun = false;
			String RecordTypeIdMon = Schema.SObjectType.Campaign__c.getRecordTypeInfosByName().get('Monitoring').getRecordTypeId();
​
			Set<Id> parentIds = new Set<Id>();
			for (Campaign__c c : scope) {
				parentIds.add(c.Id);
			}
​
			Map<Id,Campaign__c> cMap = new Map<Id,Campaign__c>();
			if (!parentIds.isEmpty()) {
				List<Campaign__c> parentCamp = [SELECT Id, Do_Not_Notify_Subscriber__c FROM Campaign__c WHERE Id IN :parentIds];
​
				List<Campaign__c> allCamps = [SELECT Id, Parent_Campaign__c, RecordTypeId, Do_Not_Notify_Subscriber__c FROM Campaign__c WHERE Parent_Campaign__c IN :parentIds AND RecordTypeId =: RecordTypeIdMon];
				for (Campaign__c parentCampaign : parentCamp) {
					for (Campaign__c childCampaign : allCamps) {
						if (childCampaign.Do_Not_Notify_Subscriber__c = True) {
							parentCampaign.Do_Not_Notify_Subscriber__c = True;
							cMap.put(parentCampaign.Id,parentCampaign);
						}
					}
				}
				update cMap.values();
			}
		}
	}
}

 
Maharajan CMaharajan C
Hi,

Try the below cahnges :

public class UpdateParentCampaignHandler {
    public static Boolean isFirstRun = true;
    public static void updateParentCampaign(List<Campaign__c> scope) {
        if (isFirstRun) {
            isFirstRun = false;
            String RecordTypeIdMon = Schema.SObjectType.Campaign__c.getRecordTypeInfosByName().get('Monitoring').getRecordTypeId();

            Set<Id> parentIds = new Set<Id>();
            for (Campaign__c c : scope) {
                parentIds.add(c.Id);
            }

            Map<Id,Campaign__c> cMap = new Map<Id,Campaign__c>();
            Map<Id,Campaign__c> campsToUpdate = new Map<Id,Campaign__c>();
            if (!parentIds.isEmpty()) {
​                cMap = [SELECT Id, Do_Not_Notify_Subscriber__c FROM Campaign__c WHERE Id IN :parentIds];
                List<Campaign__c> allCamps = [SELECT Id, Parent_Campaign__c, RecordTypeId, Do_Not_Notify_Subscriber__c FROM Campaign__c WHERE Parent_Campaign__c IN :parentIds AND RecordTypeId =: RecordTypeIdMon];
                    for (Campaign__c childCampaign : allCamps) {
                        if (childCampaign.Do_Not_Notify_Subscriber__c = True) {
                            if(cMap.containsKey(childCampaign.Parent_Campaign__c) && cMap.get(childCampaign.Parent_Campaign__c).Do_Not_Notify_Subscriber__c != True){
                            Campaign__c cam = cMap.get(childCampaign.Parent_Campaign__c);
                            cam.Do_Not_Notify_Subscriber__c = True;
                            campsToUpdate.put(cam.Id,cam);
                            }
                        }
                    }
                if(!campsToUpdate.isEmpty())
                update campsToUpdate..values();
            }
        }
    }
}


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C