+ Start a Discussion
Gallery IntegrationGallery Integration 

Bulk trigger to prevent "Too many SOQL queries:101" error message on Campaign member not working

Hi All,

Previously I am using a process builder to update a field called "Reminder Date" in campaign member while the field "Reminder Date" in the campaign was changed/edited.
However, I hit too many soql queries error while uploading 100++ members at once.

So I deactivate the process builder and change it to Apex Trigger instead.
However, this bulk trigger seems not working to me, I use this same logic to update other field from contact at it works fine.
Are there anything that I am missing from this code?


trigger CampaignMemberUpdateReminderDate on CampaignMember (before insert, before update){

    Set<id> CampaignMemberIds = new Set<id>();
    for (CampaignMember a : Trigger.new)
        CampaignMemberIds.add(a.CampaignId);   

    Map<id, Campaign> campaigns = new Map<id, Campaign>([Select Reminder_Date__c from Campaign Where Id in :CampaignMemberIds]);  

    // iterate over the list of records being processed in the trigger and
    for (CampaignMember a : Trigger.new)
    { a.Reminder_Date__c = campaigns.get(a.CampaignId).Reminder_Date__c;
    }

}
 
Best Answer chosen by Gallery Integration
Narender Singh(Nads)Narender Singh(Nads)
Hi,

Try this code:
trigger CampaignMemberUpdateReminderDate on Campaign (after update){
    set<id> CampaignIds=new set<id>();
    for(Campaign c:trigger.new){
        //Checking if the field is updated
        if(c.Reminder_Date__c != trigger.oldmap.get(c.id).Reminder_Date__c){
                CampaignIds.add(c.id);
        }
    }
    CampaignMember[] UpdateList= new CampaignMember[]{};
    if(CampaignIds.size()>0){
        CampaignMember[] CampaignMemberList= new CampaignMember[]{};
        CampaignMemberList=[select id,Reminder_Date__c from CampaignMember where CampaignId in : CampaignIds];
        
        for(CampaignMember cm: CampaignMemberList ){
             cm.Reminder_Date__c = trigger.newmap.get(cm.CampaignId).Reminder_Date__c;
             UpdateList.add(cm);
        }
    if(UpdateList.size()>0)
        update UpdateList;
}

Let me know if it helps.
Thanks

All Answers

Narender Singh(Nads)Narender Singh(Nads)
Hi,

Try this code:
trigger CampaignMemberUpdateReminderDate on Campaign (after update){
    set<id> CampaignIds=new set<id>();
    for(Campaign c:trigger.new){
        //Checking if the field is updated
        if(c.Reminder_Date__c != trigger.oldmap.get(c.id).Reminder_Date__c){
                CampaignIds.add(c.id);
        }
    }
    CampaignMember[] UpdateList= new CampaignMember[]{};
    if(CampaignIds.size()>0){
        CampaignMember[] CampaignMemberList= new CampaignMember[]{};
        CampaignMemberList=[select id,Reminder_Date__c from CampaignMember where CampaignId in : CampaignIds];
        
        for(CampaignMember cm: CampaignMemberList ){
             cm.Reminder_Date__c = trigger.newmap.get(cm.CampaignId).Reminder_Date__c;
             UpdateList.add(cm);
        }
    if(UpdateList.size()>0)
        update UpdateList;
}

Let me know if it helps.
Thanks
This was selected as the best answer
Jithesh VasudevanJithesh Vasudevan
Hi Gallery,

It takes the value from Campaigns to Campaign Members only when it is created, not when it gets updated for some reasons.

trigger CampaignMemberUpdateReminderDate on CampaignMember (before insert, before update){

    Set<id> CampaignMemberIds = new Set<id>();
    for (CampaignMember a : Trigger.new)
        CampaignMemberIds.add(a.CampaignId);   

    Map<id, Campaign> campaigns = new Map<id, Campaign>([Select Id,Reminder_Date__c from Campaign Where Id in :CampaignMemberIds]);  

    // iterate over the list of records being processed in the trigger and
    for (CampaignMember a : Trigger.new)
    { a.Reminder_Date__c = campaigns.get(a.CampaignId).Reminder_Date__c;
    
    }
  
}