+ Start a Discussion
LauraStephensLauraStephens 

Campaign Trigger update

Hello everyone,

 

I created a trigger on Campaign Member and the purpose is to update the fields of the campaign member from the Lead it is associated with.

 

I am not able to figure out why the code is not updating the campaign member record even though it is hitting the code:

 

Following is my trigger:

 

trigger CampaignMemberAfter on CampaignMember(after insert, after update, after delete)
{

List<CampaignMember> camMembers = trigger.IsInsert || trigger.IsUpdate ? trigger.new : trigger.old;

if(trigger.isUpdate)

{
CampaignMemberClass.UpdateCampaignMember(camMembers);
}


}

 

 

 

Following is my class:

 

public class CampaignMemberClass
{

public static void UpdateCampaignMember(List<CampaignMember> camMembers)

{

Map<Id,CampaignMember> campaignLeads = new Map<Id,CampaignMember>{};

Map<Id,CampaignMember> campaignMems = new Map<Id,CampaignMember>{};

for(CampaignMember cm : camMembers)
{
campaignLeads.put(cm.LeadId, cm);
campaignMems.put(cm.Id, cm);
}

List<CampaignMember> CMsToUpdate = new List<CampaignMember>{};

for(CampaignMember CMem : [Select LeadId,Id,Lead.avg_attendance__c,Lead.avg_ticket_price__c,Lead.Estimated_Annual_Revenue__c,Average_Attendance__c, Average_Ticket_Price__c, Estimated_Annual_Rev__c from CampaignMember where Id in :campaignMems.keySet()])
{


CMem.Average_Attendance__c = Cmem.Lead.avg_attendance__c ;
CMem.Average_Ticket_Price__c = CMem.Lead.avg_ticket_price__c ;
CMem.Estimated_Annual_Rev__c = CMem.Lead.Estimated_Annual_Revenue__c;
CMem.Lead_Status__c = 'Open';

System.debug('AVERAGE ATTENDANCE LEAD VALUE '+ Cmem.Lead.avg_attendance__c);
System.debug('AVERAGE ATTENDANCE LEAD VALUE '+ Cmem.Lead.avg_ticket_price__c);
System.debug('AVERAGE ATTENDANCE LEAD VALUE '+ Cmem.Lead.avg_attendance__c);




CMsToUpdate.add(CMem) ;

}





}




}

 

Please help me in figuring out what I am missing.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Team WorksTeam Works

If i understand this correctly...You update eg Average_Attendence in Leads.

Then, When you update the CampaignMember record the value of Average_attendence in CampaignMembers is updated as well.

Please make the changes to your class as below. I am using Before Insert/Before update/After Delete trigger

public class CampaignMemberClass
{
public static void UpdateCampaignMember(List<CampaignMember> camMembers)

{

//Use Lead Map not CampaignMember

Map<Id,Lead> campaignLeads = new Map<Id,Lead>{};
Map<Id,CampaignMember> campaignMems = new Map<Id,CampaignMember>{};

for(CampaignMember cm : camMembers)
{
campaignLeads.put(cm.LeadId, null);
campaignMems.put(cm.Id, cm);
system.debug('CM Lead Id '+ cm.LeadId);
system.debug('CM Id '+ cm.Id);

}

campaignLeads.putAll([Select Id,Name,Average_Attendance__c from Lead where Id In :campaignLeads.keyset()]);
List<CampaignMember> CMsToUpdate = new List<CampaignMember>{};

for(CampaignMember CMem : camMembers)
{

system.debug('Camp Member to be updated '+CMem);

//Include other fields as well

CMem.Average_Attendance__c = campaignLeads.get(CMem.LeadId).Average_Attendance__c ;
//CMem.Average_Ticket_Price__c = CMem.Lead.avg_ticket_price__c ;
//CMem.Estimated_Annual_Rev__c = CMem.Lead.Estimated_Annual_Revenue__c;
//CMem.Lead_Status__c = 'Open';
//CMem.Description = CMem.Lead.Description;

System.debug('AVERAGE ATTENDANCE CAMP VALUE '+ Cmem.Average_Attendance__c);
System.debug('AVERAGE ATTENDANCE LEAD VALUE '+ Cmem.Lead.Average_Attendance__c);
//System.debug('AVERAGE ATTENDANCE LEAD VALUE '+ Cmem.Lead.avg_attendance__c);

 


CMsToUpdate.add(CMem) ;
system.debug('The List of camps to be updated '+CMsToUpdate);
}

 

 

}

 


}

 

Let me know if this works and hit kudos

All Answers

Arun MKArun MK

Hi,

 

I don't see any Update DML statement in your code. Just see if it is because of that.

 

Regards,

Arun.

LauraStephensLauraStephens

If I do add the update statement it goes into an infinite loop and I get the error:

 

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CampaignMemberAfter: maximum trigger depth exceeded

LauraStephensLauraStephens

Thanks for the quick response Arun, please help me as I know I am missing something here

LauraStephensLauraStephens

Thanks for the quick response:

 

If I do add the update statement it goes into an infinite loop and I get the error:

 

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CampaignMemberAfter: maximum trigger depth exceeded

Arun MKArun MK

Hi, Instead of "after update", try to use the "before update". Regards, Arun

LauraStephensLauraStephens

Thanks Arun!

 

Tried that. but get the following message:

 

Apex trigger CampaignMemberAfter caused an unexpected exception, contact your administrator: CampaignMemberAfter: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00vQ00000024dlVIAQ; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 00vQ00000024dlV) is currently in trigger CampaignMemberAfter, therefore it cannot recursively update itself: []: Class.CampaignMemberClass.UpdateCampaignMember: line 42, column 1

 

 

Arun MKArun MK

Is that what you have provided the full code? When you are using after update, and it goes into an infinite loop its because each time you update, it enters the trigger again and it goes on as recursive.

 

Just to avoid that, u can use a static boolean variable created.

 

You can go through this link

 

http://www.salesforce.com/docs/developer/cookbook/Content/apex_controlling_recursive_triggers.htm

 

Regards,

Arun.

Team WorksTeam Works

If i understand this correctly...You update eg Average_Attendence in Leads.

Then, When you update the CampaignMember record the value of Average_attendence in CampaignMembers is updated as well.

Please make the changes to your class as below. I am using Before Insert/Before update/After Delete trigger

public class CampaignMemberClass
{
public static void UpdateCampaignMember(List<CampaignMember> camMembers)

{

//Use Lead Map not CampaignMember

Map<Id,Lead> campaignLeads = new Map<Id,Lead>{};
Map<Id,CampaignMember> campaignMems = new Map<Id,CampaignMember>{};

for(CampaignMember cm : camMembers)
{
campaignLeads.put(cm.LeadId, null);
campaignMems.put(cm.Id, cm);
system.debug('CM Lead Id '+ cm.LeadId);
system.debug('CM Id '+ cm.Id);

}

campaignLeads.putAll([Select Id,Name,Average_Attendance__c from Lead where Id In :campaignLeads.keyset()]);
List<CampaignMember> CMsToUpdate = new List<CampaignMember>{};

for(CampaignMember CMem : camMembers)
{

system.debug('Camp Member to be updated '+CMem);

//Include other fields as well

CMem.Average_Attendance__c = campaignLeads.get(CMem.LeadId).Average_Attendance__c ;
//CMem.Average_Ticket_Price__c = CMem.Lead.avg_ticket_price__c ;
//CMem.Estimated_Annual_Rev__c = CMem.Lead.Estimated_Annual_Revenue__c;
//CMem.Lead_Status__c = 'Open';
//CMem.Description = CMem.Lead.Description;

System.debug('AVERAGE ATTENDANCE CAMP VALUE '+ Cmem.Average_Attendance__c);
System.debug('AVERAGE ATTENDANCE LEAD VALUE '+ Cmem.Lead.Average_Attendance__c);
//System.debug('AVERAGE ATTENDANCE LEAD VALUE '+ Cmem.Lead.avg_attendance__c);

 


CMsToUpdate.add(CMem) ;
system.debug('The List of camps to be updated '+CMsToUpdate);
}

 

 

}

 


}

 

Let me know if this works and hit kudos

This was selected as the best answer
LauraStephensLauraStephens

Worked like a charm! Thank you Namo! :)