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
ForceRookieForceRookie 

Update parent record based on child’s field

On update of Parent Campaign, it will check it’s Child Campaign that has Monitoring RecordType. If Do_Not_Notify_Subscriber__c = True, then update Parent Campaign Do_Not_Notify_Subscriber__c = True.

But I’m getting Duplicate id in list.. what’s wrong with my code?
public class UpdateParentCampaignHandler implements TriggerHandlerInterface {
	public static void updateParentCampaign(List<Campaign__c> scope) {
        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);
        }
                
        List<Campaign__c> cList = new List<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__cDo_Not_Notify_Subscriber__c = True) {
                        parentCampaign.Do_Not_Notify_Subscriber__c = True;
                        cList.add(parentCampaign);
                    }
                }
            }
            update cList;
        }
    }
}

 
Best Answer chosen by ForceRookie
Raj VakatiRaj Vakati
  1.  
  2. can u remove 2 soql and make one 
  3. looks like your trigger is firing recursively 
refer this link for how to avoid recursive 

https://help.salesforce.com/articleView?amp;language=en_US&id=000199485&type=1

All Answers

Raj VakatiRaj Vakati
Please use this code .. 

But i guess the code whihc you shared is not complete
 
public class UpdateParentCampaignHandler implements TriggerHandlerInterface {
	public static void updateParentCampaign(List<Campaign__c> scope) {
        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> cList = 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__cDo_Not_Notify_Subscriber__c = True) {
                        parentCampaign.Do_Not_Notify_Subscriber__c = True;
                       map.put(parentCampaign.Id ,parentCampaign) ;
                    }
                }
            }
            update cList.values();
        }
    }
}

 
ForceRookieForceRookie
Hi Raj, I’m getting the Too many SOQL queries: 101 error
Raj VakatiRaj Vakati
  1.  
  2. can u remove 2 soql and make one 
  3. looks like your trigger is firing recursively 
refer this link for how to avoid recursive 

https://help.salesforce.com/articleView?amp;language=en_US&id=000199485&type=1
This was selected as the best answer
ForceRookieForceRookie
Will you help me how to apply the avoid recursion in my code?
Raj VakatiRaj Vakati
GIve me your trigger pls
ForceRookieForceRookie
Nevermind, it’s working now. Thanks for the help. Map and avoid recursive help me.
Raj VakatiRaj Vakati
Great! close this thread