Instant Content Generation in Salesforce Marketing Cloud with ChatGPT

As the world of artificial intelligence is constantly evolving, you are probably a bit tired (as I am) of seeing the immense amount of posts around ChatGPT.  

Now that the waters have calmed a bit, I decided to take a deeper look at the language’s capabilities and think about how we can combine this super-powerful tool with Salesforce Marketing Cloud. 

Before we begin, what’s ChatGPT? (Explained by ChatGPT) 

ChatGPT is a language model developed by OpenAI that has gained immense popularity in recent years due to its remarkable ability to generate human-like text. Using deep learning techniques, ChatGPT can understand and respond to natural language queries in a way that is often indistinguishable from human responses. Its versatility has made it a highly sought-after tool in various industries, including marketing, customer service, and content creation.  

Content Generation and Experimentation in Salesforce Marketing Cloud 

Salesforce Marketing Cloud includes a variety of AI-powered tools to define content based on specific rules as well as to test the performance of our messages and decide what message performs better, for example: 

  • Einstein Content Selection is a no-code platform that populates content when the email is opened and optimizes it based on engagement rates 
  • Dynamic Content lets you set different content based on rules you define against data in Salesforce Marketing Cloud 
  • Journey Builder allows you to use things like decision splits, random splits or path optimizers to send contacts through different paths with different messages based on different rules

All the examples above are great tools to experiment with content, but what about the actual generation of that content? What if I wanted to get 10 variations of a headline to see which one performs better? 

Salesforce’s announcement of Einstein GPT at TDX indicates that there’s a bright future ahead for us marketers, but you don’t really need to wait until Einstein GPT gets to Salesforce Marketing Cloud to tap into the power of AI. I’ll show you how. 

The Use Case  

For our example, we are going to use a fictitious online soccer apparel store called FutbolShop.    

The FutbolShop marketing team is building their welcome email and they would like to test different variations of their headline, as they are not very convinced with the one they have right now: 

The FutbolShop team wants to test five different variations of the headline to see which one has the best click-through rate, determined by checking engagement with the “Find your style” button. 

The easy method: using ChatGPT   

ChatGPT is quite simple to use, you can get an account at and start chatting.  

To get the most out of this tool you’d want to use some of these guidelines: 

  1. Be clear and specific with your questions: ChatGPT works best when you ask clear and specific questions. This helps the model to understand what you’re looking for and provide a more accurate response. 
  2. Provide context: When asking questions, it’s helpful to provide context to help ChatGPT understand the situation better. This can include relevant information about your background, what you’re trying to accomplish, and any other details that might be helpful.
  3. Experiment with different types of questions: ChatGPT can answer a wide range of questions, so it’s worth experimenting with different types to see what works best. For example, you might ask for advice, clarification, or recommendations. 
  4. Use proper grammar and spelling: ChatGPT works best when you use proper grammar and spelling. This helps the model to understand your questions more accurately and provide better responses. 

Once you get your account, we can go ahead and ask ChatGPT to provide us with alternatives headlines: 

Once we’re happy with the results provided, we will set up an email for each of the headlines and utilize Journey Builder’s path optimizer to split the email into six branches: one with our original headline and five with ChatGPT’s generated ones.  

Journey Builder will then pick a winner based on the level of engagement with the email and take it from there, easy… right? 

The More Complex but Perhaps More Fun/Scalable Method: Using the ChatGPT API 

So, I decided to experiment a little bit with the ChatGPT API to see if I could come up with a more automated and reusable way to generate content on the fly at the time of sending! 

Step 1. Get a ChatGPT API Key 

This is what ChatGPT had to say about this step: 

Here are the steps to create a ChatGPT API key:

1. Go to the OpenAI website at and click on the “Get started for free” button.

2. Enter your email address and select a password to create an account.

3. Follow the prompts to verify your email address and complete your account registration.

4.Once you are logged in to your OpenAI account, navigate to the “API Keys” tab.

5. Click on the “New API Key” button to generate a new API key.

6. Specify the name and permissions of your new API key, and click on the “Create API Key” button.

7. Copy your new API key and use it to connect to the ChatGPT API in your code.

It is worth noting that the OpenAI API is currently in a limited access beta program, and you may need to submit an application to gain access to the API. Once you have been granted access, you can follow the above steps to create your ChatGPT API key and start using the language model to generate engaging content.

Step 2. Create a Data Extension to Hold Your Content 

We are going to create a Data Extension that will hold the content we generate at the time of send. That way, we don’t have to retrieve the content for every subscriber.   

Your DE should have the following fields: 

  • EmailName, Text (250) 
  • Variation, Number 
  • GeneratedText, Text (500) 

Step 3. Code! 

We are going to create a code snippet so we can reuse it in other emails.   

Simply create a code snippet with this code; I will explain what the code does below:   

What does this code do? 

This code snippet receives three variables declared in AMPscript: one to determine how many variations ChatGPT should return, another one with the prompt we want to get alternatives for, and a last one with how many maximum tokens we want per reply. Tokens are the way that ChatGPT has to process words. There’s not a 1-1 relationship between a token and a word, but it’s close. You can read more about tokens here.  

The snippet will try to retrieve previously generated content for this email in our previously created Data Extension by utilizing the Email Name as the key. If no records are found, it will connect to the ChatGPT API to get alternatives for the content provided, based on how many variations we’ve asked for. 

Once the results are returned by the API, we generate a random number from 1 up to the number of variations we’ve requested, and grab one of the alternative texts from there. 

The next time you request content for the same email, the snippet will directly look for content inside the Data Extension. This way, our code will run optimally – saving us a trip to the ChatGPT API for every subscriber. 

Note: You can also copy and paste this code in ChatGPT and you’ll get an explanation of what this code does. 

Step 4. Add the Code to Your Email and Analyze the Results 

The last step is quite simple. We will declare two variables – number of variations and prompt – then call our code snippet and use the result to print our resulting content:  

Once you’ve started the send, you can verify that ChatGPT generated content for the variations indicated:   

And when your send is completed, you can review your click activity to see which variation got the most clicks: 

You can use this data to define the headline or content that will earn you the most engagement. In our case, variation #4 got the most engagement, so we can refer back to our Data Extension to see what copy was used and go with that! 

Final considerations: 

  1. You can use this code snippet to generate subject line alternatives!
  2. The code looks up data utilizing the Email Name, meaning there can only be one piece of ChatGPT-generated content per email. But this can be easily adjusted if you add an additional column in the Data Extension (maybe call it ContentName) and provide that value in your lookup. This way, you can have more than one AI-generated content piece per email. 
  3. You can use the @generatedContentAlias as an Impression Region utilizing the Begin and End Impression Regions functions. That way, you can analyze the performance of an entire content block versus a single link. 
  4. You can place this Script in a Script activity in Automation Studio if you wanted to generate some content and look at it before using it in your emails (less fun, but … I guess safer). 
  5. Consider this code as experimental; test it well if you are thinking of using this solution in one of your emails. 

Remember: ChatGPT is a very powerful tool to work with, but not a replacement of work. I hope you’ve enjoyed reading this article the same way I’ve enjoyed working on this use case!   

If you want to access the full solution, including a package to deploy the Data Extension and Code Snippet using Package Manager, visit my GitHub repository:

Pato Sapir

Pato Sapir is a certified Salesforce Professional with more than 10 years of experience designing, implementing and extending solutions for the Salesforce platform.