How to Create Validation Rules in Salesforce with ChatGPT

Andréa Faria
5 min readApr 18, 2023

--

In this article, I explain how to create Validation Rules in Salesforce using ChatGPT and tips on how to use ChatGPT to enhance existing Validation Rules

Young Female Data Scientist Programmer working in a conference room

Recommendations to deepen your knowledge

First of all, if you want to deepen your knowledge in CRM management and the like, here are some reading recommendations:

What are Validation Rules in Salesforce?

In Salesforce’s definition, Validation Rules:

(…) verify that the data a user enters in a record meets the standards you specify before the user can save the record.

A validation rule can contain a formula or expression that evaluates the data in one or more fields and returns a value of “True” or “False”.

Validation rules also include an error message to display to the user when the rule returns a value of “True” due to an invalid value

Validation Rules are very useful for creating automation, especially on Opportunities.

Some examples are:

  • Check if a certain field has been filled in before the user advances an opportunity to the next stage
  • Making an integer data type field accept only numbers within a range (such as 0 to 10)
  • Determining that a field can only be populated with a certain value if other fields have already been previously populated

There are several applicability for Validation Rules.

You can find examples proposed by Salesforce itself here.

How can ChatGPT help create Validation Rules?

If you have accessed the link with the Validation Rules examples or even if you have already created rules, you know that this task requires some programming knowledge.

Also, let’s be honest, the interface Salesforce offers for programming these rules isn’t the best.

It’s past time to improve this “IDE”, huh, Salesforce?

Also, writing code that creates the desired automation and doesn’t interfere with other rules in Salesforce can be tedious and isn’t always as intuitive.

Therefore, Chat GPT can be very useful for building the necessary code based on the problem you want to solve, instead of wasting hours going through the documentation.

I will explain this with an example.

Example of using GPT Chat to create a Validation Rule

In this example, I needed to create a Validation Rule where, if an opportunity didn’t reach the Trial stage and was closed as Closed Lost, the Qualified Prospect? field should receive the value No.

PS: this is true for any task involving ChatGPT , but especially when it comes to code, I recommend sending prompts in English, as the platform has been better trained in that language and has more English-speaking content as well

First, as each business has unique rules and customizations, before requesting the code itself, you need to give some context to Chat GPT, so it understands what the starting point should be.

In this case, I did this by explaining to the Chat:

  • what stages exist in our Salesforce,
  • at what stages the opportunity can be closed and
  • the existence of a field called Qualified Prospect? and what values this field can receive

After presenting the context, the second part was to request the Validation Rule itself, with details of how it should work:

I did this in a single prompt. Here, I’ve divided it into two parts to make the explanation easier.

The result was this:

In seconds, the chat created the Validation Rule for me!

Since the chat understands the context of the prompts being sent, I made a new query to ask for an additional condition in the generated code.

In this case, I asked the code to also check if the Qualified Prospect? do not leave blank:

And this was the result:

I tested the code above in my Sandbox and there was a problem.

Even when the opportunity had already advanced to Trial and the Qualified Prospect? field had the value Yes, Salesforce would not allow me to close the opportunity as Closed Lost.

So I asked Chat to redo the code and fix this problem:

And this is what the chat returned:

That’s why it’s important to test beforehand and not blindly trust the provided answer.

Example of using GPT Chat to improve Validation Rules

You can also use GPT Chat to enhance Validation Rules that already exist in Salesforce.

To do so, just cut to the chase:

And this is the result:

It even explains the updates!

General tips:

  • Be direct and concise. Prompts need to clearly explain what you want
  • Avoid sending ambiguous prompts as much as possible
  • Don’t ask open-ended questions. The more accurate the better
  • Provide context for the chat to clearly understand your business rules and customizations and how the Validation Rules should behave before them
  • Test the rules in the Salesforce Sandbox before activating the Validation Rule in the production environment. Cha GPT t is awesome, but it can be wrong too
  • Never send sensitive or confidential data! If necessary, use synthetic data and adapt your code when entering it into Salesforce

I hope these tips were helpful.

Doubts and suggestions, just comment here.

Help yourself:

  • Get organized: try Notion here 📅
  • Don’t do it all on your own. Find a professional to help you (from $5.00) here 🤑

Support the page:

Reach out:

Read Also:

--

--