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
ellsajames11.3892668100636045E12ellsajames11.3892668100636045E12 

Trigger to create new campaign member based based on "Responded" campaign member field?

Hello experts,

I was wondering if it is possible to have a trigger fire off the "Responded" checkbox (Or a custom field) on the campaign member object? What I would like to achieve is this----

If a campaign member resonds to campaign A (Ie: "Responded" checkbox is true). I would the like a trigger to assign them to campaign B. (Or create a new campaign member record for them on campaign B)

If they do not respond to campaign A, then the trigger should assign them to campaign C.

Basically I would like to route campaign members through a series of campaigns based on if they respond or not.

If anyone could post some code to achieve this or at least point me in the right direction I would appreciate it.

Thanks.
Graham
hitesh90hitesh90
Hello Graham,

You have to write trigger on CampaignMember object on after insert and after update event.
and in this trigger you have to update the same CampaignMember record with particular campaignId as per the conditions(campaign A or B).

Because of this trigger is on after update it will call recursively so you have to handle recursive calling of trigger using static variable.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/
ellsajames11.3892668100636045E12ellsajames11.3892668100636045E12
Hi Hitesh,

Thanks for the reply. I have posted some code that I have written so far for this. Can you please take a look and ammend it where you see fit or advise what I need to add and where? Keep in mind that I am a newbie to APEX.

Code
=====

trigger newMember on CampaignMember (After Insert,After Update)
{
    List<CampaignMember> c = new List <CampaignMember>();     
       
    For(CampaignMember Member : trigger.new)
    {
       If(Member.HasResponded != False)
       {
                                   
       c.Update(CampaignMember(                     
         CampaignId = '701D0000000P9jg' )       
    );
       }   
    } 
   
    update c; 
}

====================

Thank you
GlynAGlynA
Graham,

I'm not sure Hitesh is steering you in the right direction.  Because you are modifying fields on the CampaignMember object, you should be doing this in a "before" trigger.  This also eliminates the recursion problem.

A couple of questions for you:  Do you really want to assign them to Campaign C if they haven't responded?  How do you know that they won't still respond?  If you trigger on "insert", then they won't have responded yet, and they'll get assigned immediately to Campaign C.  No one will ever wind up in Campaign B.

The code in the following post does a couple of things.  The first part of the trigger queries Campaign so you don't have to hard-code the Campaign IDs.  The last part moves the CampaignMember to Campaign B if they are already in Campaign A and have responded.  It doesn't do anything about Campaign C.  Let me know what the desired behavior is with respect to Campaign C.

Apologies if there are any typos - I could not compile the code.

Glyn Anderson
Sr Developer | System Analyst | ClosedWon | closedwon.com
Certified Developer | Certified Advanced Administrator
Blog: GlynATheApexGuy.blogspot.com
Twitter: @GlynAtClosedWon
GlynAGlynA
<pre>
trigger newMember on CampaignMember ( before insert, before update )
{
    Set<String> set_CampaignNames = new Set<String>
    (   'Campaign A',
        'Campaign B',
        'Campaign C'
    );

    Map<String,Id> map_CampaignName_ID = Map<String,Id>();

    for ( Campaign theCampaign :
        [   SELECT  Id, Name
            FROM    Campaign
            WHERE   Name IN :set_CampaignNames
        ]
        )
    {
        map_CampaignName_ID.put( theCampaign.Name, theCampaign.Id );
    }

    for ( CampaignMember member : trigger.new )
    {
        if  (   member.CampaignId == map_CampaignName_ID.get( 'Campaign A' )
            &&  member.HasResponded
            )
        {
            member.CampaignId = map_CampaignName_ID.get( 'Campaign B' )
        }
    }
}
</pre>
GlynAGlynA
Whoops!  Found a typo already.  The parentheses on lines 04 and 07 should be curly braces "{" and "}".  Sorry about that.

-Glyn
GlynAGlynA
When your ready to describe the entire "series" of Campaigns - I have an idea how to handle that.  But let's get the basic code working for you first.

-Glyn
ellsajames11.3892668100636045E12ellsajames11.3892668100636045E12
Hi Glyn,

Thank you for your help. It is greatly appreciated. Let me start by saying that I do not have a specific use case for this at the moment but am looking at it as an option to introduce new functionality for a client. i understand what you mean about the responded field and you are correct. It does not make sense to trigger of this field. I would still like to be able to route campaign members to different campaigns based on their response.

 - Would it be possible to route the campaign member to a specific campaign based on the option they select from a custom picklist ? (For example:      Not Interested, interested, etc....).

- Could I have the campaign member routed to a campaign if they do not reply in a certain timeframe? (For example: after 2 weeks).

Thanks again.

GlynAGlynA
Routing to a Campaign based on a picklist response would be pretty easy to do.  You could even add fields to Campaign that would determine which Campaigns to assign the members to (rather than hard-coding them in the trigger).

Routing after a period of non-response is more complicated.  There might be a way to use time-based workflow, but as a programmer, my thought is to use a scheduled job that periodically (nightly?) checks for CampaignMembers that have not responded within the appropriate time period (or perhaps if it's after the Campaign's end date).  It would reassign them.

There are other less-automated ways to do it.  For example, you could trigger when a Campaign status is changed to some value.

-Glyn