Integrating ChatGPT With Salesforce: A Real-World Use Case

Introduction to ChatGPT

ChatGPT, developed by OpenAI, is a state-of-the-art language model that stands at the forefront of natural language processing technology. It excels in comprehending and generating text-based responses, making it a versatile tool for various applications. With its contextual understanding and ability to produce mostly relevant and accurate responses (unless it’s hallucinating), ChatGPT has gained recognition for its effectiveness in chatbots, virtual assistants, and more. Its impressive capabilities are rooted in extensive training on vast datasets, ensuring a typically factual and reliable foundation for your conversational AI needs.

How ChatGPT Can Be Used with Salesforce

Without integration with other software or tools, ChatGPT alone may not be able to directly perform actions within Salesforce or access its data. ChatGPT is primarily a text-based AI model that can understand and generate text-based instructions, and it would typically require integration with Salesforce or related applications to accomplish tasks that involve data retrieval or manipulation within Salesforce. However, integrating Salesforce and ChatGPT opens up numerous possibilities to enhance customer experience. Below, we delve into several key ways in which this integration can elevate your customer engagement and streamline your CRM processes:

  1. Customer Support and Service: Use ChatGPT to provide immediate assistance to customers and streamline support operations. It can answer frequently asked questions, troubleshoot common issues, and guide customers through self-service options, ultimately improving customer satisfaction and reducing support ticket volume.\
  2. Lead Generation and Qualification: Integrate ChatGPT into your lead generation process to engage website visitors and qualify potential leads. ChatGPT can engage in conversation, gather information about prospects, and assess their interest, helping your sales team prioritize and follow up with qualified leads.
  3. Data Entry and Automation: Automate data entry tasks by incorporating ChatGPT into your Salesforce environment. ChatGPT can extract information from emails, forms, or other documents and input this data directly into Salesforce records, saving time and reducing manual data entry errors.
  4. Sales Playbooks and Training: Enhance sales training and coaching by using ChatGPT to create interactive sales playbooks. It can provide real-time guidance to sales representatives, assist in objection handling, and offer suggestions during customer interactions, ultimately improving sales performance.
  5. Knowledge Base and Content Generation: Leverage ChatGPT to generate content for your Salesforce knowledge base, marketing materials, or product descriptions. It can help content creators by generating drafts and ideas, ensuring consistent messaging, and speeding up content production.

These use cases address common challenges faced by organizations using Salesforce, such as improving customer service, increasing sales efficiency, reducing data entry workload, enhancing sales training, and streamlining content creation. Integrating ChatGPT into these areas can lead to significant improvements in productivity and customer satisfaction.

Integration Options for ChatGPT and Salesforce

Options to integrate ChatGPT and Salesforce include custom development, a Salesforce AppExchange product like Declarative Webhooks, middleware or ChatGPT plugins. Let’s explore this in greater detail:

Custom Development

If you have a strong internal Salesforce team, custom development might be an option for your organization. There are a few specific implementation that a Salesforce developer could use to implement ChatGPT in Salesforce:

  1. Custom Lightning Component: A custom Lightning Component in Salesforce that communicates with the ChatGPT API. Users can interact with ChatGPT directly within Salesforce Lightning Experience.
  2. Custom Visualforce Page: Though an older standard Visualforce is still tried and true on the Salesforce platform so you can create a custom Visualforce page that incorporates ChatGPT and this too can be embedded in various Salesforce layouts.
  3. External Integration: You can create an external application that interacts with both Salesforce and ChatGPT. Use Salesforce REST APIs to exchange data with your external application, which then communicates with ChatGPT.

Declarative Webhooks (Salesforce Native)

Declarative Webhooks is a Salesforce native AppExchange solution which can allow you to seamlessly integrate ChatGPT with your Salesforce environment quickly, easily, and your way. Declarative Webhooks provides a user-friendly, no-code approach to setting up webhooks and automating data transfer between Salesforce and external systems, including ChatGPT. This allows you to easily trigger actions in Salesforce through flow, process builder, and custom buttons to name a few without the need for extensive custom development.

ChatGPT Plugin

OpenAI offers plugins that connect ChatGPT to third-party applications such as Salesforce. These plugins enable ChatGPT to interact with Salesforce APIs and allows ChatGPT to potentially perform actions in Salesforce i.e. create Salesforce tasks or update records when specific ChatGPT interactions occur. The downside of this approach is that you will take you out of Salesforce to perform your workflow, but it could offer a pretty engaging and conversational way to interact with Salesforce.

Build Your Own No-Code ChatGPT Integration with Salesforce Service Cloud

Generative AI Integration Scenario

Let’s consider a typical customer service scenario where a customer has sent an email to a Salesforce routing address which will automatically convert incoming customer emails into support cases. We want to leverate ChatGPT to analyze incoming emails to:

  1. Categorize Cases created by Email-to-Case.
  2. Set the Priority of these Cases based on the customer email.
  3. Suggest a response from the customer service representative.
  4. Assign the Case based on the customer email.

If you haven’t already done so please setup Email-to-Case before proceeding with this tutorial

ChatGPT API Setup

  1. Create an OpenAI developer account.
  2. Go to User → API Keys and you should see something like this:
  3. Create a new secret key and name it if you wish.
  4. Make sure to copy your ChatGPT secret key and store it securely somewhere
  5. Go to Organization settings and obtain your Organization Id

You now have everything you need to start working in Salesforce to setup this integration.

Configure the ChatGPT API Callout from Salesforce

ChatGPT Callout Template Details

  1. Callout Name: EmailMessage – ChatGPT
  2. Active: true
  3. Callout URL: https://api.openai.com/v1/completions
  4. Callout Method: POST
  5. Pre-Callout: No Pre Callout
  6. Callout Authorization Type: Bearer Token
  7. Callout Authorization Token: Enter your OpenAI secret key you obtained previously
  8. Main Object: EmailMessage
  9. Number of Records per Callout: One Record
  10. Request Body Format: JSON (application/json)
  11. Retry if callout fails: You can set this to whatever you wish.

Your callout template should look like the following the click Save and Continue:

ChatGPT Callout Headers

Specify OpenAI callout headers as follows:

  1. Header Name and Header Value should already include Content-Type application/json
  2. Add and addition header by clicking the ‘Add Header’ button and entering the following
  1. Header Name: OpenAI-Organization
  2. Value: Enter the value you got at step 5 of the ChatGPT API setup

Your callout header should look like this:

AI Prompt Engineering

Prompt engineering involves the art of crafting input queries for generative AI tools with the aim of achieving the most favorable and desired outcomes. We have three distinct use cases we’re trying to solve for and will need to engineer a relevant AI prompt for each. Here’s what we came up with, but feel free to experiment:

  1. Prompt for Case Categorization: “In Salesforce we have the following Case Types: [INSERT CASE TYPE PICKLIST VALUES]. Please respond with one of these Case Types only and no other text or characters. Based on the following email, please provide the correct Categorization.
  2. Prompt for Case Priority: In Salesforce we have the following Case Priorities: [INSERT CASE PRIORY PICKLIST VALUES]. Please respond with one of these Case Priorities only and no other text or characters. Based on the following email, please provide the correct Priority.
  3. Prompt for Suggested Response: “You are the most talented customer support representative at a SaaS company. You are efficient and care deeply about customer success. You also close the most tickets of anyone on your team and have the highest customer satisfaction rating. How would you respond to this email? 

Create the ChatGPT API Request Body

  1. Based on the ChatGPT API documentation we know the API request is going to look something like this:
{
    "model": "text-davinci-003",
    "prompt": "Some prompt",
    "max_tokents": 200
}
  1. We can copy and paste this sample request from the documentation and click ‘Create from Sample JSON request.
  2. Then we just need to map the Request Body values
    1. For model, we’re going to select:
      1. Node Name: model
      2. Node Type: Value
      3. Node Data Type: Text (String)
      4. Send null as empty string: false
      5. Value Source: Static
      6. Enter Value: text-davinci-003 (you could consider other ChatGPT models)
    2. For the prompt, we’re going to create a formula in Declarative Webhooks as follows:
      1. Node Name: prompt
      2. Node Type: Value
      3. Node Data Type: Text (String)
      4. Send null as empty string: false
      5. Value Source: Formula
      6. Formula: text-davinci-003 (you could consider other ChatGPT models)”You are the most talented customer support representative at a SaaS company. You are efficient and care deeply about customer success. You also close the most tickets of anyone on your team and have the highest customer satisfaction rating. How would you respond to this email? ” &  TextBody (note TextBody is a field on the EmailMessage object which includes the email as text.
    3. For the max-tokens, this is optional, but we’re using it to control the number of tokens used in the response to control our usage. The setup is the same as the model in 8a., except for Enter-Value I am using 200.

Your Request Body will look like the following then click Save and Continue:

Create the ChatGPT API Response

The ChatGPT API Response will look something like this. We can copy and paste this sample response and click ‘Create from Sample JSON request.

{
  "id": "cmpl-6nZGeHTkSywdEhYe8EVcYKOMp6b7r",
  "object": "text_completion",
  "created": 1677271660,
  "model": "text-davinci-003",
  "choices": [
    {
      "text": "\n\nHi there, \n\nI'm so sorry to hear that you're having trouble importing a template with Declarative Webhooks. I would be happy to help you out. Can you please provide me specific details about the issue you are experiencing and also, if possible, screenshots of what you are seeing so I can better assist?\n\nThank you for reaching out and I look forward to helping you resolve this issue. \n\nBest regards,\n[Your name]",
      "index": 0,
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 71,
    "completion_tokens": 99,
    "total_tokens": 170
  }
}

 Your Response Body will look like the following:

Now click Save and Continue.

Map the ChatGPT API Response to Salesforce

You do not need to Update the main record (which is the EmailMessage). Instead, you want to update the related Case which we can access through the ParentId.

  1. Click Add New Action then Add New “Updated Related Record” Action
  2. Select Related Record to be Parent (ParentId)
  3. Click the Add Field button. I have created a field on the Case called Current_Suggested_Email_Response__c. You’ll need to add this field to the Case object or a similar field to map the suggested email response from ChatGPT. Select this under Field.
  4. Type should be JSON Mapping.
  5. Click the Select button and under Value map to choices.text JSON node.

The final result will look like this:

Click Save Changes and then Save and Close.

Believe it or not you’ve, you’ve finished creating your callout to the ChatGPT API.

Extending Your Callout to Additional ChatGPT Prompts

In order to show more use cases, I’m going to Export my template and Import the same template twice so I can effectively clone two callout templates and add my additional prompts for Case Categorization and Case Priority. Let’s see how that’s done:

  1. From the Callout Template record page click the ‘Export Template’ quick action button.
  2. Go to the Administration tab and click the Callout Template tab in the Administration section of the Declarative Webhooks app. Scroll to the bottom of the page and click the ‘Import Template’ button:
  3. Click Choose File and then the Import button on the top right of the screen.


Import the same template twice so we have two more templates we can configure with our additional prompts for categorizing our Case Type and Case Priority.

  1. Rename your Callout Templates so you can easily differentiate them. Click Save and Continue until you get to the Response Body page. On the Response Body page click the pencil icon to edit the prompt node. All you’re going to do is change the Formulas in each Callout Template to the following:
    1. Case Type Categorization: “In Salesforce we have the following Case Types: Problem, Feature Request, Question. Please respond with one of these Case Types only and no other text or characters. Based on the following email, please provide the correct Categorization. ” &  TextBody
    2. Case Priority:
  2. Click Save and Continue twice then the Update related record: ParentId action. You will also need to update the Response Action to update the correct fields. Change the Case Field to Case Type in one Callout Template and Priority in the other. Your action should look like this:
    1. Case Type:
  1. Priority:

Implement your ChatGPT API Callouts in a Salesforce Flow Record-Triggered Flow

  1. Create a new Record-Trigger Flow as follows:
    1. Object: Email Message
    2. Trigger the Flow when: A record is created
    3. Set Entry Conditions:
      1. Condition Requirement: All Condition Are Met (AND)
      2. Field: ParentId
      3. Operator: Is Null 
      4. Value: False
    4. Optimize the Flow for: Action and Related Records
  2. Add an element to your flow selecting Action then Invoke Callout (Using Developer Name). 
  3. Enter the Callout Template Developer Name which you can get from your Callout Template Record and for Record Id enter {!$Record.Id}. It should look like the following:
  4. Repeat this two more times with your additional Callout Templates and varying the Callout Template Developer Name. When it’s all done it will look like this:



Complete the End-To-End ChatGPT Workflow in Salesforce

We’re going to add a few more components to round out this workflow spanning Salesforce and ChatGPT. First, I want to create an email template that will pull in the Suggested Case Email Response field so that customer service can quickly respond to Cases in Salesforce. Second, I want to create a Case Assignment Rule so that based on the Case Type and Case Priority being set by ChatGPT, we assign the Case to the appropriate party. I created this email template:

And here’s the Case Assignment Rule which will assign the Case to our Lead Developer Lucian Ciobanu if our Case is Type ‘Problem’ and Priority ‘High.’

When I was testing this workflow, I realized the Case Assignment Rule was not running because the Case is created with an Owner and updated with the Type and Priority after the fact. I found this handy package that allows you to run Case Assignment Rules in a flow. In order to leverage this package, I had to:

  1. Install the package at the link above.
  2. Create a Record-Triggered Flow:
    1. Object: Case
    2. Condition Requirements: All Conditions Are Met
      1. Field: Origin Equals Email
  3. Add a Schedule Path:
    1. Path Label: Run Assignment Rules in One Minute
    2. Time Source: When Case is Created
    3. Offset Number: 1
    4. Offset Options: Minutes After
    5. Add element Action called Run Assignment
      1. Set the CaseId Input Variable to {!$Record.Id}
    6. Save, name, and activate your flow. I called the flow Run Case Assignment Rules. It should look something like this:

We’re all done and ready to test our entire ChatGPT workflow in Salesforce! Let’s see it in action:

Conclusion

In conclusion, the seamless integration of ChatGPT with Salesforce, accomplished entirely on the Salesforce platform with Declarative Webhooks, empowers organizations to enhance customer engagement and streamline CRM processes without writing a single line of code. ChatGPT’s versatile capabilities, spanning customer support, lead generation, data automation, and content generation, are harnessed effortlessly through the user-friendly Declarative Webhooks interface. 

We outlined one potential workflow providing a step-by-step guide for setting up ChatGPT API callouts within Salesforce Service Cloud, demonstrating its potential to categorize cases, set priorities, suggest responses, and streamline case assignments. Hopefully we got you thinking about other scenarios we could solve similarly. Integrating ChatGPT with Salesforce holds significant promise for organizations seeking to boost productivity and elevate customer satisfaction by harnessing the power of AI-driven text generation within their Salesforce workflows.

If you want to learn more about Declarative Webhooks, please follow the links below: