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
Jeff TalbotJeff Talbot 

Trigger creation of a Task (related to Lead/Contact) when Campaign Member record is updated

I have a need that can't be accomplished with Workflow. I'm posting here hoping someone has some insight for me.

 

When a Campaign Member record is updated (to be specific - when a value in a particular custom field is updated), I need to create a completed Task on the Contact (or Lead) record that is associated with that Campaign Member record. I'm thinking this could be accomplished with an Apex Trigger. Unfortunately, I have almost no experience writing Apex Triggers.

 

So before I go down the path of trying to learn Apex and learn how to accomplish this particular goal, can anyone confirm if I can even achieve this goal using an Apex trigger? If so, and if you have any tips or helpful links, I'd really appreciate it. Thank you! Love the SF community!

Jeff TalbotJeff Talbot

Okay, so it's several hours later and I've written an Apex Trigger (in my sandbox)! Detail: The Trigger creates a completed Task related to the Contact record when a Campaign Member record is updated. I also added an IF filter so that it only fires when a custom checkbox is checked on the Campaign Member record.

 

So the Trigger works, BUT, it's creating TWO Tasks every time it fires. Detail: One Task is created with all of the Campaign Member data that I have referenced in my code. A second Task is created that does not have any of the Campaign Member data in it, just the data that is hardcoded (i.e. Subject line appears as "Call - " instead of "Call - value from Call Result picklist"

 

I welcome any insight on why my code is creating two Tasks instead of one. Here's the code:

 

trigger InsertCompletedCallTask on CampaignMember (before update) {
list<Task> AddTask=new List<Task>();
for(CampaignMember CM : Trigger.new) {
if(cm.create_completed_call_task__c == true){
AddTask.add(new Task(
WhoId = CM.ContactId,
Description = CM.Call_Notes__c,
ActivityDate = CM.Call_Date__c,
Status = 'Completed',
Subject = 'Call - ' + CM.Call_Result__c, 
Related_Campaign_ID__c = CM.CampaignId));
}
}
insert AddTask;
}

 

 

So I went from clueless to putting together a mostly working trigger with a few hours of internet research and some hacking. Not bad for a novice? Well, to that end, if you have any tips for me on writing a test class for this trigger and/or exactly what I need to do to get my trigger from sandbox to production, I'd really welcome that information too. I'm clueless in those areas too for the moment.

 

Thanks!

Ritesh AswaneyRitesh Aswaney

Good work, I'd recommend an After Update trigger for your purpose, here goes :

 

 

trigger CampaignMemberAfterUpdate on CampaignMember (after Update) {

List<Task> tasks = new List<Task>{};

for (CampaignMember cMemb : Trigger.new)
{
    if(cMemb.HasResponded)
    {
        Task t = new Task(WhoId = cMemb.Contact != null ? cMemb.Contact.Id : cMemb.Lead.Id, Status = 'Completed');
        tasks.add(t);
    }
}

if(tasks !=null && !tasks.isEmpty())
    Database.insert(tasks);

}
Jeff TalbotJeff Talbot

Thanks Ritesh for more sample code to learn from! I see some tips I can take from there, including how to populate WhoID with Lead or Contact depending on what the Campaign Member is. That's great.

Jeff TalbotJeff Talbot

As always, the "aha!" moment occurs after posting. I solved the problem, but I still want to understand why.

 

I had a workflow updating the Campaign Member record. My trigger was firing before AND after the workflow made field updates. On one hand that's logical. On the other hand, the salesforce "order of operations" didn't lead me to believe that a trigger would fire again after a workflow update. Oh well, now I know.

 

Anyhow, I will tackle the test class and deployment to production in the morning. Please tell me this will be simple.

Ritesh AswaneyRitesh Aswaney

I would use an After Update trigger, well done !

 

Yes, there are ways of avoiding a trigger firing twice by using static (global) variables, but don't think you'd require that.

 

The test class should be relatively simple and deployment to Production, well that depends on the quality of your existing code :)

t-hoft-hof

Hi, did you ever get this working right?  I'm trying to accomplish pretty much the same thing.

Jess ZieglarJess Zieglar

I am having the hardest time getting this trigger to deploy. Does anyone have some great suggestions for the Apex Test Class? Any guidance you are willing to provide would be AMAZING!!!