
Using the HTTP Callout action in Salesforce Flow, it is possible to make calls to external web services without writing a single line of code. This powerful capability opens up a world of opportunities for integrating Salesforce with external systems and services. For instance, it is possible to integrate ChatGPT with Salesforce to streamline processes and enhance customer interactions.
In this post, we will explore how to harness the potential of the HTTP Callout action to seamlessly integrate ChatGPT with Salesforce. This integration can revolutionize the way you engage with your customers and manage data within your Salesforce environment.
What is ChatGPT?
ChatGPT is an AI chatbot using natural language processing to create human-like conversations and various content like articles and emails. It operates as generative AI, users input prompts to get AI-generated text. Similar to customer service chatbots, users can ask questions and get response from ChatGPT. It undergoes training with reinforcement learning, which uses human feedback to improve future responses.
Integrating ChatGPT into Salesforce can be crucial for enhancing customer interactions and streamlining processes. Using ChatGPT in Salesforce, it is possible to provide instant responses to customer inquiries and generate personalized content. This can ultimately boost efficiency and customer satisfaction within the Salesforce ecosystem.
Let's see how to use the HTTP Callout action in order to integrate ChatGPT with Salesforce.
Steps Before Building the Action
There are a few steps before building the HTTP Callout action in flow.
1- The first step is to add an external credential. Search for named credentials in setup and then create a new external credential.

2- In order to integrate with Salesforce, you have to create an API key in OpenAI. After logging in to OpenAI, click to view API keys and then create a new secret key.

3- Copy the API key that you created and create a custom header related to the external credential. Name should be "Authorization" and paste the API key after writing "Bearer " into the value.

4- Create a new principal related to the external credential. This step is important for giving permissions through profile or permission sets.

5- Create a new named credential and choose the external service that you created in the previous step.

6- In order to give permission to specific users, create a new permission set. Then, give access to the external credential principal that you created.

Now, you can create the HTTP Callout action from flow.
Creating the HTTP Callout Action
It is possible to create an HTTP Callout action from any flow type that supports the action element. Let's create the HTTP Callout action from a screen flow.
1- Add an action element and click the "Create HTTP Callout" button. Give a name to the external service and choose the named credential that you created before.

2- Give a name to the invocable action and choose POST as the method.

3- Next, you have to provide a sample request and a sample response.
Use this as the sample request.
{
"model": "text-davinci-003",
"prompt": "What is the capital city of France?",
"temperature": 0.7,
"max_tokens": 250
}
Use this as the sample response.
{
"warning": "Warning message.",
"id": "cmpl-7z1DK3tYzc1kLDJ8eAEuGmMTe1amU",
"object": "text_completion",
"created": 1694777030,
"model": "text-davinci-003",
"choices": [
{
"text": "\n\nParis.",
"index": 0,
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 8,
"completion_tokens": 4,
"total_tokens": 12
}
}

After this step, you have an invocable action that you can use to integrate ChatGPT with Salesforce.
Build a Flow to Integrate ChatGPT with Salesforce
Let's build a simple screen flow to integrate ChatGPT with Salesforce.
1- Add a screen element with a text input field, so that the user can type in their question.

2- In order to use the invocable action that you created, you have to assign values to the Apex-defined variable. This will be the body of your request.
Create a new Apex-defined variable and then use an assignment element to assign values.


3- Add an action element and choose the invocable action that you created. Then choose the Apex-defined variable that you assigned as the request body.

4- This action's response has a collection called "Choices". Add an assignment element and assign the "Choices" collection to an Apex-defined collection variable.


5- Since the response is a collection, you have to use loop and assignment elements in order to extract the real response that you need.

Create a new Apex-defined variable (single) for the Choice value.

Then, use an assignment element to assign the current item from the loop to the Choice variable.

6- At the end of the loop, use a screen element in order to display the response from ChatGPT. Answer for your question is stored in the "text" parameter.

At the end, your flow should look like this.

Demo
Here is how it works. As you can see, flow displays ChatGPT's answer for the user's question.

Improvements
In this post, we saw how to use flow to integrate ChatGPT with Salesforce. Besides giving users a screen to ask questions, it is possible to use this integration in many areas. For instance, it is possible to build a flow that helps users solve cases.

On the other hand, you can build a record-triggered flow to automate the process as well.
To sum up, integrating ChatGPT with Salesforce empowers businesses to elevate customer engagement. It automates tasks and enhances overall operational efficiency. Moreover, it fosters growth and improved customer satisfaction.
Leave a Reply