Mountains reflected on water

How To Automate Salesforce Campaign Naming Conventions

Campaign naming conventions are a must have. They keep your campaigns organized, improve reporting, and provide key information about the campaign at a glance. However, campaign naming conventions can only be helpful if they are used — and used consistently. 

Enforcing naming conventions is tricky because it typically relies on the users to remember the order, abbreviations, variations by type etc. Without good governance, your campaigns will end up having disparities.

Here’s an example of possible disparities: 

Instead of continuing to rely on your users remembering your naming conventions, we can automate this process with APEX.

But First

Before you start automating your campaign names, ensure the data that goes into your naming conventions is on the campaign object. For instance, my naming convention is YYYY_MM_Campaign Type_Description/Name, so I’ll need to make sure the following fields are required:

  1. Start Date 

Year and Month will be pulled from this field

  1. Campaign Type 

Create a picklist field on the campaign object for your different campaign types such as webinar, email, trade show, etc.

  1. Short Name 

Create a text field on the campaign object for the user specified description/name. The user will only enter data into the “Short Name” field, the default field “Campaign Name” will be completed by our APEX trigger.

You’ll need to do a quick calculation to see how long your new Campaign “Short Name” field can be. The default “Campaign Name” field can only be 80 characters, so you’ll need to calculate:

calculating character count for naming convention

X=58

The Campaign “Short Name” field can be up to 58 characters.

To ensure this is clear to your users, add Help Text to both the “Short Name” and “Campaign Name” fields.

new campaign

Automating the Salesforce Campaign Name

Next, we’ve created a little Salesforce DX project that you can take a look at to see an example of how this can be done: https://github.com/sercante-llc/campaign-name-enforcer

The project includes the Custom Field, Trigger code and the APEX Test code as well.

The CampaignNameTrigger is how we can enforce the Name of the Campaign. In this code, we see that we are using the date format “YYYY_MM_” to get us started, which will write out the 4 digit year and 2 digit month. Other formats are available.

trigger CampaignNameTrigger on Campaign (before insert, before update) {
    if(Trigger.isBefore && Trigger.isInsert) {
        //we will set the Name of the Campaign based on other fields,
        //overwriting whatever was placed there before
        for(Campaign campaign : Trigger.new) {
            campaign.Name = 
                Datetime.newInstanceGmt(campaign.StartDate, 
                    Time.newInstance(0,0,0,0)).formatGmt('YYYY_MM_')
                + campaign.Type + '_' + campaign.Short_Name__c;
            if(campaign.Name.length() > 80) //make sure length is good
                //if it isn't, trim it down to size
                campaign.Name = campaign.Name.substring(0, 80); 
        }
    }
    else if(Trigger.isBefore && Trigger.isUpdate) {
        for(Campaign campaign : Trigger.new) {
            Campaign oldCampaign = Trigger.oldMap.get(campaign.Id);
            //first lets see if anyone else tried changing the name
            if(campaign.Name != oldCampaign.Name) {
                //we want to prevent that
                campaign.addError('You can\'t change the Name directly.');
                continue;
            }
            //ok, we are safe to set the correct value now
            campaign.Name = 
            Datetime.newInstanceGmt(campaign.StartDate, 
                Time.newInstance(0,0,0,0)).formatGmt('YYYY_MM_')
            + campaign.Type + '_' + campaign.Short_Name__c;
            if(campaign.Name.length() > 80) //make sure length is good
                //if it isn't, trim it down to size
                campaign.Name = campaign.Name.substring(0, 80); 
        }
    }
}

This trigger will also trim the final campaign name down to 80 characters, if needed. 

Once implemented, the above APEX Trigger will fire whenever a Campaign is created and/or edited. 

final view

This is a subtle reminder that this year’s ParDreamin virtual conference starts on October 27th! Register here.

Want to automate your campaigns even further? Check out how you can auto-generate and enforce campaign member statuses by campaign type.

Thanks to Adam Erstelle for contributing to this post.

Published by

Erin Duncan

CRM and Marketing Automation Strategist. Erin Duncan is 3x Salesforce certified and has 9+ years of experience as a Salesforce and Pardot Admin. Erin is the leader of the Atlanta B2B Marketers User Group, a Salesforce Marketing Champion, and a former Dreamforce, SE Dreamin, and NE Dreamin speaker.

Leave a Reply