Loading

Salesforce Platform: Implement Custom Campaign Influence Models with Apex Triggers

게시 일자: Oct 13, 2022
상세 설명

Key Definitions:

  • Apex (Salesforce's programming language)
  • Apex Triggers (code that executes before/after database operations)
  • Campaign Influence (attribution model tracking which campaigns influenced opportunities)

Scenario:

Standard Campaign Influence only tracks last-touch attribution - custom models allow multi-touch revenue attribution

Business Use Case:

  • With Customizable Campaign Influence, the included Primary Campaign Source model creates a campaign influence record whenever
    • there is a match between a member of an active campaign and a contact role on an opportunity created within the time-frame specified in Setup (auto-association).
    • a user specifies a campaign in the Primary Campaign Source field on an opportunity.

  • The Primary Campaign Source model attributes 100% of opportunity revenue to the primary campaign. Your sales and marketing users can’t manually add campaign influence records to this model either.

  • Many businesses need to track campaign influence with more detail. To address your own specific campaign influence tracking scenarios, you can create custom influence models in Setup. For example, you might want to
    • let Users manually add campaign influence records.
    • perform custom revenue attributions, such as first touch gets 30%, last touch gets 30%, and all others get an equal share of the remainder.

Note: This article is about Customizable Campaign Influence and not the original version of Campaign Influence.

솔루션

When you create a custom model, you can use Apex triggers to add campaign influence records to the model. We recommend an approach that adds records to your custom model when Salesforce adds a record to the Primary Campaign Source model.

To implement this solution, specify an Apex trigger that defines when to create these records and what information to capture, depending on your influence tracking needs. In the example that follows, the trigger detects when a new record is added to the Primary Campaign Source model and then creates records relating the same opportunities and campaigns against your custom model.

Considerations for Implementing Custom Campaign Influence Models:

  • The Primary Campaign Source model’s influence records are recalculated when any of these events occur.
    • Changing any Customizable Campaign Influence setting
    • Updating an opportunity’s Close Date
    • Adding or removing members from campaigns
    • Deleting or undeleting accounts or opportunities

  • During such a recalculation, some existing campaign influence records for the Primary Campaign Source model may be deleted and re-created. Because the approach described here creates records for your custom model whenever Primary Campaign Source records are created, it’s possible that duplicate influence records can be created against your custom model.

  • To avoid this duplicate record creation, include validation checks in your own Apex code to make sure you're not inserting a record with the same modelId + opptyId + campaignId + contactId into the same model. If you find that a record already exists, perform an update operation on the record instead.

The following example shows how to insert campaign influence records for your custom model. For other operations such as update and delete, you’ll need to create additional Apex triggers.

Example:  Apex Trigger Code

This example Apex trigger creates campaign influence records against the custom model you specify. By itself, this example mimics the Primary Campaign Source model, while also allowing users to add campaign influence records themselves.


You can modify the Apex trigger provided here in order to change the percentage of opportunity revenue associated with certain campaigns, or certain campaign types, such as webinars, emails, or white paper downloads. Add code to this trigger to set the Influence and RevenueShare fields on the records, based on how you would like to track campaign influence.

  1. From Setup, enter Apex in the Quick Find box, then click Apex Triggers.
  2. Click Developer Console.
  3. Click New → Apex Trigger
  4. Create Apex Trigger for CampaignInfluence sObject
  5. Copy and paste the following code into the Apex editor.
  6. Replace the modelId on line 8 and line 11 with the ID of the Customizable Campaign Influence model you want to create records for.
  7. Click Save.
trigger CampaignInfluenceInsert on CampaignInfluence (after insert) {
   //Create a new list of CampaignInfluence Records.
   List<CampaignInfluence> campInfs = new List<CampaignInfluence>();
   for(CampaignInfluence ci: Trigger.New){
       //Create new CampaignInfluence records and add them to the List for
       //insert since the original Salesforce Model Records are not editable
       //for modelId.
       if (ci.modelId == '03VR000000005qr') return;
       CampaignInfluence newCI = new CampaignInfluence();
       //Replace the modelId below with the ID of your custom model
       newCI.modelId = '03VR000000005qr';
       newCI.campaignId = ci.campaignId;
       newCI.opportunityId = ci.opportunityId;
       newCI.Influence = ci.Influence;
       //Only insert contactId if there is one.
       if(!String.isBlank(ci.contactId)){
           newCI.contactId = ci.contactId;                        
       }
       //Add newly created or updated records to the new list.
       campInfs.add(newCI);
   }
   if(!campInfs.isEmpty()){
       upsert campInfs;//Insert or Update the records if the list if not empty.
   }
}

 

Note: You can find the ID of your custom influence model by querying the Id field of the campaignInfluenceModel.

Test Results of Your Model:

The standard campaign reports that come with Salesforce don’t include any custom model data. To see the records your custom model generates, you can

  • Make your custom model the default model. Records for the default model appear in the Campaign Influence related list on Opportunities and the Influenced Opportunities related list on Campaigns.
  • Create a custom report type based on your model.

 

Special Notes: The code provided in this article is just an example. Salesforce Support cannot assist with issues related to this sample code or implementation, as this is outside the scope of developer support.

 

Knowledge 기사 번호

000381978

 
로드 중
Salesforce Help | Article