Skip to content
How To Use ChatGPT for Salesforce Generative Coding

How To Use ChatGPT for Salesforce Generative Coding

In this how-to article, I aim to first clarify what Generative Coding entails. Then I will explain OpenAI and ChatGPT, how to sign up, and how to keep your data private. Next, I will take you through my technique for using ChatGPT to accomplish repetitive Apex development. I will also share my prompts used to generate unit tests, perform Salesforce devops market research, generate test classes, and generate devops glue code. By the end of this article, you should be able to use these techniques to refactor, generate tests, and extend your current org more efficiently than ever.

What is Generative Coding?

For me, Generative Coding is the practice of using an AI to assist in any kind of software development project. The size of the organization doesn’t matter, if you’re using ChatGPT or GitHub Copilot to check your code, explain it, or create new code based on English descriptions, then you are using Generative Coding.

The significance of this term lies in the imminent proliferation of Generative Coding offerings and systems. All these systems will be based on OpenAI or other AI technology company offerings, but they will have specialized interfaces designed for coders and architects.

In this post, we’ll be using ChatGPT-4 as it currently exists, but I expect there soon to be official plugins and other systems to bring the ChatGPT-4 experience into VS Code. Plus, we are expecting GitHub Copilot X to work more like the conversational experience I’ll be showing here. And, we have yet to see any form of engineering collaboration work here. Perhaps, ChatGPT for Slack will let teams have a conversation with an AI instead of individuals.

In essence, Generative Coding represents an important product category that every engineering manager should explore in the coming months. Our profession is about to be shaken in substantial and unpredictable ways, so please don’t get blindsided by putting your head in the sand.

Who is OpenAI and What is ChatGPT?

OpenAI is the leading developer and vendor of a new technology platform known as a large language model (LLM). In recent years, the company has grown so much it appears to on its way to joining Google, Netflix, Facebook, Salesforce, and Oracle as a Silicon Valley titan.

An LLM is a type of artificial intelligence that can understand and generate human-like text based on a vast amount of data. These models are designed to learn patterns and relationships in language, enabling an LLM to create coherent and contextually relevant content. OpenAI has published papers demonstrating that GPT-3, a 175 billion parameter LLM, can perform tasks across various domains, languages, and modalities.

But there is something different about ChatGPT-4, whose parameter size has been estimated at nearly one trillion. It is so powerful that GPT-4 may have made prior efforts at building vertical-industry AI systems obsolete.

This is because high-parameter LLMs possess emergent properties that surpass those of vertical LLMs, enabling them to excel in a broader range of tasks and applications. Their increased capacity to learn and adapt allows them to understand and generate content across multiple computer languages. This adaptability makes high-parameter LLMs more versatile and powerful, as they can provide more accurate results and facilitate seamless collaboration between different programming languages and platforms.

How to Sign Up for ChatGPT and OpenAI Platform

That’s a lot of power. Fortunately, it’s easy to enlist ChatGPT-4 as your new Salesforce software engineering consultant. All you need to do is sign up, and then optionally upgrade your account to use ChatGPT-4.

First, a quick note about data privacy and ChatGPT-4. By default, ChatGPT-4 will save your conversations in a permanent thread, and that thread may be reviewed by OpenAI or ingested into the model. I find the saved thread feature particularly useful as it retains conversations across various topics. But I can see how the way OpenAI treats most user data will make compliance officers freak out.

OpenAI says they are introducing a Business account with better data safety, and no data from the API will be used for training. In the meantime, you have several options to maintain privacy. For instance, you have the option to navigate to the settings in ChatGPT and choose not to save your conversations.

Another option is to use the OpenAI API by using the OpenAI API Playground to do chats. There are some additional advantages of using the Playground, such as specifying System message and adjusting your parameters. I imagine some organizations could put together simple front ends to OpenAI API to regulate usage.

Generating Salesforce Apex Classes

Let’s delve into my experience with OpenAI and a new Salesforce org that I have developed. I have been developing a new content and knowledge management system for my subscribers and clients. In this project, I use a Salesforce org as my source-of-truth. I also keep an ephemeral MySQL database to support a Next.js website built in TypeScript and React. To implement the project there are about 30 custom objects. I’m sure I made this thing way too complicated!

When I add a new object in this architecture, I need several new Apex classes to implement CRUD functions for a custom Salesforce REST API. So, in this How-To I will show you how I consistently create new implementations of the following types of Apex classes:

  • Service Class Wrapper
  • Trigger Handler Class and Trigger
  • Static Service Functions
  • Response Wrapper Class
  • REST Handers

ChatGPT-4 Solution

When working with ChatGPT-4 I find it helpful to keep track of my prompts and GPT responses in a Markdown file stored in a shared file repository. Why Markdown? It seemed natural to me because GPT-4 generates responses in Markdown, and you can request it reprint output “in escaped markdown in a code window”. This makes saving tables and other specialized Markdown output a lot easier.

Also, GPT-4 has a limited memory. It can only keep about 2,000 “tokens” in its active memory, so sometimes you need to re-enter your prompt. Even if you don’t want to use VS Code and Markdown, use Google Keep or something to keep track of your prompts.

I’m certain that some of you might consider that I could have accomplished all of this simply by copy-pasting and then performing a Search/Replace in my existing file. But I like AI cloning significantly more because ChatGPT-4 automatically handles the different uses of plurals, and special English cases like adding an “s” to a name that ends in “y”. Plus, as I show in the script, you can paste in object metadata to customize the output with new custom fields.

Here is the Markdown script I generated for replicating the needed classes.

Engineering Analysis

Having a code workhorse at your disposal is one thing, but possessing a software engineering consultant who comprehends all the available literature on computer science and has analyzed millions of practical applications is a completely different matter.

Here’s the part the blew me away. After I had finished using all these prompts to construct all the Apex classes needed for a new REST endpoint, I wondered how good a developer I was in its eyes. So, I asked ChatGPT-4 this:

Analyze available literature for patterns used by Apex developers. See if you can classify the patterns we’ve been using with any of those patterns. Suggest ways to use a better strategy based on your literature research.

I won’t reprint the result here (check the GPT conversation above), but it gave me back six Salesforce architectural techniques I could use to make my code better. One of them was to create a Selector Layer to concentrate all the SOQL into one class. So, I asked ChatGPT-4 to “refactor available code to implement a Selector Layer for the ResearchProject object.” And, of course, it spit out a new Selector class and then rewrote the Data Access Object and Service Layer classes.

This kind of advisory service seems like it would be invaluable for engineering managers and Salesforce devops practitioners. You now have an on-demand software engineering consultant who is merging knowledge from all fields of engineering to service your problem-solving needs.

Tip: Give It Time

Before moving on to the next example, look again at that last prompt. I’ve got three separate actions in there, and they are related. OpenAI calls this technique “giving it time,” which means that you have encouraged the AI to think about the problem in pieces. I understand this translates into making the OpenAI cloud service consume more resources to respond to those prompts.

Salesforce Devops Market Research

Besides generative coding, ChatGPT comes in handy for market research. Your prompt wording and making the system take its time counts here too. In this case I asked ChatGPT-4 the following:

System: You are a market researcher in Salesforce devops.
User: I am a technical manager who is searching for a testing solution. Analyze the primary factors used to make this selection and summarize the choices in a table with the top 5 competitors by factors.

This example shows how you can use a System prompt to restrict the AI’s scope of a search, or to have certain analytical characteristics. You can start your prompt in ChatGPT-4 with a system prompt, or you can do it explicitly in the OpenAPI Platform Playground.

Beware of Unreproducible Results and Hallucinations

ChatGPT is an amazing way to get started on a research project, but I have found some significant limitations. Take the example I used here. I have been using the same prompt over time, or even multiple times on the same day, and the answers have changed. And not always for the better. Sometimes it will put a company into competition, and they don’t do Salesforce testing at all.

Two problems are coming to light here. First, the OpenAI crawl of the Internet for ChatGPT ceased back in 2021, so ChatGPT does not have the latest information from the Internet. And LLMs feel compelled to create answers, so sometimes it will fabricate a response that sounds good. AI researchers refer to this activity as AI hallucinations. Yes, that is as bad as it sounds so it’s always best to check results from LLMs for the time being.

Two More Examples

It seems like I could write a whole book on this stuff! Well, for now let’s get this post over with. I’ll quickly describe two other examples I have ready to share.

Glue Code – GitHub Actions plus Jira

Given the wide training and knowledge of scripting and open-source packages, ChatGPT is excellent at generating system integration solutions. This comes up frequently in constructing devops pipelines.

In this example I ask ChatGPT-4 to create an integration between GitHub Actions and a Jira instance. It started out with a Python solution, and then I guided it to make a Node/TypeScript version. It’s quite fascinating how proactive it is in its responses.

Unit Test Class Generation

This is an extensive recording of a session where I get ChatGPT to generate test factory classes for me. I go round about where I ask it to do some research on the best unit testing patterns, and then we start making factories.

About halfway through this example, I paste in field names to generate a new class, like I did in the first example. In the first example I used Custom Field XML files I downloaded using sfdx force:source:retrieve. In this example I make that even easier by just copying and pasting in the Fields page from Object setup.

Thank You Provar

Before finishing up this post, I wanted to thank Provar for putting together a wonderful webinar today where Richard Clark, Sam Arroyo, and I did an hour-long extravaganza on Generative Code. I understand the webinar was well attended. I expect companies like Provar to get a significant boost with OpenAI integration.

Generative Coding Takes Off

The advent of Generative Coding and AI tools like ChatGPT-4 is transforming the landscape of software development. The ability to generate code, advise on best practices, conduct in-depth research, and even interact with other software ecosystems demonstrate the immense potential of AI in numerous fields.

However, it’s essential to remember that the use of AI also brings with it certain limitations, including outdated data and the potential for inaccurate or fabricated results. Therefore, while harnessing the power of AI can lead to increased productivity and new insights, it’s crucial to cross-check the results and ensure data privacy. As we progress, the key lies in striking a balance between leveraging AI’s strengths and mitigating its limitations.

There will be lots of fun over the next year or so. I’ll be watching this Cambrian Explosion of new software carefully, so be sure to stay in touch!

Post
Filter
Apply Filters