Sweet Potato

Categories
Technical

Auto Attach The Lead To The Campaign In Salesforce

Hello, my name is Jothi, I’m a Salesforce Consultant here at SPT

As a consultant, I have worked on many projects and provided solutions to our clients and I’m here to share my learnings from one of my recent works.

Custom metadata type:

Custom metadata type in salesforce is customizable, deployable, packageable, and upgradeable application metadata. First, you create a custom metadata type, which defines the form of the application metadata. Then you build reusable functionality that determines the behavior based on metadata of that type. 

In simple words, we can use CMDTs to store master data records that can be used throughout an organization. Some common use cases I have seen are storing organization-wide VAT or tax calculations. In the case of tax calculations, relationships between country and state are created to store individual tax rates. Apex code or formula fields can access the metadata records to perform the calculations.

Custom metadata type is used to store information that can be accessed from Formula field, Apex code, Process builder, etc. Though custom metadata type is similar to custom objects, it performs better than custom objects because it is available in the cache. Another difference you can notice between the custom metadata type and custom object is the suffix. Custom metadata type has __mdt suffix at the end while the custom object has __c at the end.

Business Use case:

Let us start with a business use case, one of our clients provided us a requirement to automatically attach the leads created in Salesforce to the appropriate campaign using a picklist value as criteria, this automation should work for any lead that is created on their org whether it may be created manually or it may be received from the web, etc.

The client provided us with the list of picklist values and their respective campaign IDs to which the lead should attach automatically.

Solution:

In this case, the first thing that came into my mind was process builder as our client mentioned that they don’t want us to create code.

We can create a process builder with the picklist values as criteria, for each value we have a campaign Id to which the lead wants to get attached.

But hang on…Is it a best practice to add a set of hardcoded id values in the process builder?

The answer is – No, it is not a best practice to hard code ids in the formula, validation rules, Apex code, processes, flows, and so on.

Alternate to hardcode:

Rather than using the Ids in the process builder, we need something else to store the ids. we can use the same configuration tools that developers use in code — such as custom metadata types (CMDTs), custom labels, and custom settings — to store Salesforce IDs

From the above suggestion, my choice is “Custom Metadata Type“.

First, we can create a custom metadata type that stores the picklist values and their associated campaign ids, and then we can create a process that can fetch the data from the custom metadata type. Similar to a custom object or custom setting, a CMDT has a list of custom fields that represent aspects of the metadata.

I have performed the following steps to solve this requirement:

1. To create a new custom metadata type, navigate to Setup | Custom Metadata Types and click on the New Custom Metadata Type button. Now fill the form as mentioned in the following screenshot:

New Custom Metadata Type

2. Click on the Save button.

3. Create custom fields for the CMDT. In this example, we’re creating one picklist field and one text field for Id.

 Once done, click Manage to create the data records for the CMDT.

Auto Attach lead to Campaign

We can use it for all the things you want to use CMDT first over a custom label or custom setting. CMDT data records are metadata type, which means it does not get deleted with sandbox refreshes or new sandboxes. You can deploy the data using CMDT in change set.

Now add this in process builder, To create a process builder navigate to Setup | Process Builder Click the New button

1. Enter the process name and A Process starts with “A record changes”

2. Select the object “LEAD” and criteria as “Only when a record is created”

3. Click Criteria then enter the name for criteria and select “conditions are met” for executing actions.

4. Now in conditions, select the picklist field and the first value and click save.

5. In Immediate action, click add action and Select the action to be performed as Create a Record.

6. Enter the name for action and select the record type as “Campaign Member” because a lead or a contact attached to the campaign is a campaign member.

 7. In Set Field Values, For the campaign Id create a formula like below

8. Click Save.

9. Repeat these steps for all the picklist values.

Deploying the CMDT

We can deploy using a change set, you’ll find the CMDT name listed alphabetically in the component list. When you select the CMDT name, you’ll find the data records. From there, click

View/Add Dependencies. This will identify the CMDT itself. You can select to add all of those components to your change set.

Leave a Reply

Your email address will not be published. Required fields are marked *