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
LloydSLloydS 

Automatically Create Opportunity Upon Campaign Response

I believe this can be done and probably best by using Apex.

 

I'd like to automatically create an opportunity automatically whenever someone responds to certain campaigns. So if someone is added to a campaign or status changed on a campaign, with a status that has been identified with the campaign as to be treated as "Responded", then go ahead and create an opportunity associated with the campaign, notify the account owner, etc.

 

Can this be done and if so I'd appreciate some guidance on what I should o.

 

Thanks.

ClintLeeClintLee

You can create a trigger on the CampaignMember object that fires after insert and after update.  On an after insert, check to see if the HasResponded field is true.  On an after update, check to see if HasResponded is true (using Trigger.newMap ) and that it was previously false ( using Trigger.oldMap ).  In both cases you'll also need to be certain that the CampaignMember is related to a Contact and not a Lead.

 

Here's a sample of how to do the after update.

 

Trigger

trigger CampaignMemberTrigger on CampaignMember( after insert, after update ) {

       

              if( Trigger.isAfter ) {

                  if( Trigger.isInsert )             CampaignMemberHandler.handleCMAfterInsert( Trigger.New );

                  else if( Trigger.isUpdate ) CampaignMemberHandler.handleCMAfterUpdate( Trigger.OldMap, Trigger.NewMap );

             }

}

 

Class

public class CampaignMemberHandler {

 

            public static void handleCMAfterUpdate( Map<Id, CampaignMember> oldMap, Map<Id,CampaignMember> newMap ) {

                        makeOpportunities( oldMap, newMap );

            }

 

           

           private static void makeOpportunities( Map<Id, CampaignMember> oldMap, Map<Id, CampaignMember> newMap ) {

                       List<Opportunity> newOpps       = new List<Opportunity>(); 

                       Map<Id, Id> contactToAcctMap  = new Map<Id, Id>();

                       

                        // build a map of ContactId => AccountId

                        for( Contact cont : [ select Id, AccountId from Contact where Id IN ( select ContactId from CampaignMember where Id IN :newMap.keySet() ) ] ) {

                               contactToAcctMap.put( cont.Id, cont.AccountId );

                         }

                         
                         for( CampaignMember cm : newMap.values() ) {

                                if( cm.HasResponded && !oldMap.get( cm.Id ).HasResponded && cm.ContactId != null ) {

                                    Opportunity opp = new Opportunity( Name           = 'Sample Opp'

                                                                                                     StageName = 'Prospecting'

                                                                                                     CloseDate    = System.today()

                                                                                                     CampaignId = cm.CampaignId

                                                                                                     AccountId     =contactToAcctMap.get( cm.ContactId ) );

                                      newOpps.add( opp );

                                }

                         }

                         insert newOpps;

            }

}

 

Hope that helps!

 

~ Clint