Categories
Tech

How to Automatically Create Notion Cards from Salesforce Cases

This script syncs Salesforce Cases into Notion, allowing for easier sprint planning and providing visibility to users who may not have access to Salesforce. It requires a Python environment, the Python File package, Salesforce access, and a Notion account. The script also requires familiarity with setting up a Python environment and creating a Salesforce Connected App. With this script, users can save time and streamline their workflow by consolidating their work into one platform.

What you will need:

  • Python Environment (local)
  • The Python File package (GitHub Repo)
  • Salesforce access
    • Admin permission, or at least the permission set to create a Connected App)
    • Service Console with Cases
  • A Notion account

Setting up a Python Environment:

If you need to set up a Python environment for the first time, you can follow my guide on setting up Python and VSCode. There are also several excellent resources that you can use to help with this process. I recommend ChatGPT for any command line errors during the setup process.

Install the needed Modules into Visual Studio Code

You must add the following modules to your environment to run this code. If everything was installed correctly within VSCode, you should be able to run these code snippets as they are shown within your terminal:

pip install simple-salesforce
pip install requests
pip install pandas

Creating the Salesforce API Connection:

For this script to work, you will need to authenticate with Salesforce. Here is what you will need from Salesforce:

YOUR_SALESFORCE_USERNAME: Your Salesforce username.

YOUR_SALESFORCE_PASSWORD: Your Salesforce password.

YOUR_SALESFORCE_SECURITY_TOKEN: Your Salesforce security token.

YOUR_SALESFORCE_CLIENT_ID: The client ID of your Salesforce Connected App.

YOUR_SALESFORCE_CLIENT_SECRET: The client secret of your Salesforce Connected App.

Your Username and Password are the same that you would use at login.salesforce.com, so I’ll move on to the Security Token.

Obtaining your Salesforce Security Token

In Salesforce, the security token is typically sent to you via email when you first set up your Salesforce account or when you reset your security token. If you need to obtain or reset your security token, you can follow these steps:

  1. Log in to Salesforce: Open your web browser and log in to your account.
  2. Access Your User Settings:
    • Click on your profile picture in the upper-right corner.
    • Select “Settings.”
  3. Navigate to the Reset My Security Token Page:
    • Click “Reset My Security Token” in the left sidebar under “My Personal Information.”
    • If you can’t find it, you can also use the quick find box at the top of the setup menu to search for “Reset My Security Token.”
  4. Reset Security Token:
    • Click the “Reset Security Token” button.
    • Salesforce will send you an email with the new security token.
  5. Check Your Email:
    • Open the email from Salesforce.
    • The email subject should be “Your new Salesforce security token.”
  6. Copy the Security Token:
    • Copy the security token from the email.

Creating a Salesforce Connected App:

Finally, to obtain the ‘client_id’ and the ‘client_secret,’ you must create a Connected App in Salesforce. You may already have one that you use for API connections, but here are the instructions just in case:

  1. Log in to Salesforce: Open your web browser and log in to your account.
  2. Access Setup:
    • Click on the gear icon in the upper-right corner.
    • Select “Setup” from the dropdown menu.
  3. Navigate to App Manager:
    • Type “App Manager” in the quick find box in the left sidebar.
    • Click on “App Manager.”
  4. Create a New Connected App:
    • Click the “New Connected App” button.
    • Fill in the required information:
      • Connected App Name: Choose a name for your app.
      • API Name: This will be automatically generated based on the name.
      • Contact Email: Your email address.
      • Enable OAuth Settings: Check this box.
      • Callback URL: Enter a valid URL (e.g., https://localhost If you will only run this on your local machine)
      • Selected OAuth Scopes: Add at least “Access and manage your data (API)” and other scopes you need.
  5. Save the Connected App:
    • Click “Save.”
    • After saving, you will be taken to the details page for your Connected App.
  6. Retrieve client_id and client_secret:
    • On the details page, look for the “Consumer Key” (this is your client_id) and “Consumer Secret” (this is your client_secret).
    • Click on the “Click to reveal” link next to the “Consumer Secret” to view and copy your client_secret.

Once you have this information, add it to the config.py file within the cloned repo.

Creating a Notion API Connection:

Next, you need to use the Notion API to create the cards. The Notion API allows you to programmatically interact with Notion databases, pages, and other elements. Here are the general steps you can follow to create a Notion card:

  1. Set Up a Notion Connection:
    • Go to the Notion Integrations page: https://www.notion.so/my-integrations
    • Click on “Create a new integration” to create a new integration.
    • After creating the integration, you’ll get an Integration Token. Save this token securely, as it will be used to authenticate your requests to the Notion API.
  2. Get Notion Database ID:
    • Open your Notion workspace and navigate to the database where you want to create cards.
    • Click on the “Share” button at the top-right of the database.
    • Click on “Copy link” to copy the link to the database. The link will contain the database ID, which will be a 32-character string of numbers and letters
  3. Add those parameters to your config.py file
  1. Install Notion SDK:
    • Install the notion-sdk-py Library a Python client for the Notion API. You can install it using:
pip install notion-sdk-py

Authorize the Integration to Run in your Notion Database

Even though you’ve created a database where you want the cards to be created, you need to allow the integration to run in Notion. You do that by:

  1. Going to your Notion page
  2. Clicking the …
  3. Click “+ Add Connections”
  4. Find the Connection you created above

Edit the SOQL Query in Salesforce

I have this code to pull only the Active Cases with their Description and Name. You may want to revise this code and change it if you need any more information (line 32)

query = "SELECT Id, CaseNumber, Subject FROM Case WHERE Status = 'New'"

If you do add anything to the query, you’ll need to map it to the Notion card as well (lines 46-51)

"properties": {
            "title": [{"text": {"content": title}}],
            "Description": [{"text": {"content": description}}],
        },
    }

Running the Code

Once these steps are completed, you can click the ▶️ icon and run your code. You should see the Cases get created in the terminal window.

Running the code in VS Code

The Cases created in Notion

Wrap up

Now that the Cases are syncing into Notion, you should look to edit the SOQL Query so that the next time you run this code, it only returns cases that are newly created. You could do that by changing the query to add some time constraints like this:

"SELECT Id, CaseNumber, Subject FROM Case WHERE Status = 'New' AND CreatedDate >= TODAY"

Let me know if you implement this code into your workflow and how you would improve it!

Categories
Tech

How to Set-Up a Python Environment on Mac

For this tutorial, I’ll walk you through the process I completed on my MacBook with VSCode. There are dozens of other Python environments (IDE’s) that you can use, and of course, you can use your Windows machine (as much as my inner Apple fan-boy doesn’t want you to).

Be sure to read the pages linked and follow the instructions carefully as not every code sample can be copied and pasted directly into your terminal to get everything installed.

VSCode installation

PATH set-up

Once you’ve installed VSCode and set-up your PATH, open a new terminal window in VSCode

Next you’ll want to Install Homebrew.

Homebrew is a package manager for macOS that simplifies the process of installing, updating, and managing software on your Mac in the terminal.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Once that finishes, don’t neglect the two steps displayed in the terminal. You’ll need to run two commands which can be copied and pasted, and then run by hitting “return”

Everything that is displayed on the first line that looks like the code below (you’ll see your home directory after the “>>” make sure that you copy that too.)

(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> <your home directory> 

Everything on the second line

eval "$(/opt/homebrew/bin/brew shellenv)"

Once you have that set-up, head over to this VS Code guide to Install Python into your IDE.

I do like to complete the “Hello World” Tutorial on a fresh build, but you by no means need to do so.

Upgrade pip

python3 -m pip install --upgrade pip

Be sure to check your version of Python when running this command. (It works as of 12.28.23)

You should be all set and ready to begin working with Python in VSCode on your Mac.

Categories
AI Tech

Add ChatGPT to DBeaver

If you use DBeaver as your SQL IDE, here’s how to add AI right now

What you need

  • DBeaver on version 22.3.5 or higher
  • An OpenAI account and a new API key

Set-Up

Download DBeaver Community Edition https://dbeaver.io/download/

Register for an OpenAI account if you don’t currently have one https://auth0.openai.com/u/signup/

Install DBeaver

Create sample database

You can also access this test database tool through Help > Create Test Database.

“Wait, why do I need the sample database?”

I assume you don’t have a huge personal database ready to go, so save yourself the trouble and have DBeaver spin one up for you.

Another thing to consider before actually using this tool to help you code is that your table header and column names will be sent to OpenAI to help you code. If you or your org have security concerns about this – you probably can’t use this tool with your job.

So instead, create the sample database so you can use the AI without security concerns.

Installing the ChatGPT Add-on

Click Help > Install New Software

In the “Works with” section, type: “DBeaver” and you should see “DBeaver AI” appear in the section below.

Follow the on-screen prompts and restart DBeaver

Open the sample database and press F3

You should see the OpenAI logo to the left of the SQL editor

Click the ChatGPT logo to launch the API and settings window

Go to your OpenAI account and generate a new key and paste it into the API token field

Click “Apply and Close”

Using the Tool

Click the ChatGPT icon again

You will get a prompt asking permission to read the table header and columns. As mentioned above, if you’re not using the sample database, be sure you’re okay with this before proceeding.

With that, you’re off and running…

ChatGPT Output:

SELECT COUNT(*) FROM Album;

Chat GPT Output:

SELECT Genre.Name, COUNT(InvoiceLine.InvoiceId) AS 'Number of Customers'
FROM Genre
INNER JOIN Track ON Genre.GenreId = Track.GenreId
INNER JOIN InvoiceLine ON Track.TrackId = InvoiceLine.TrackId
GROUP BY Genre.Name
ORDER BY COUNT(InvoiceLine.InvoiceId) DESC;

Troubleshooting

Don’t see the OpenAI icon.

  • If you don’t see the icon, confirm that the editor is allowed to run
    • Click Window > Preferences > Editors > AI (ChatGPT)
  • Ensure that “Enable smart completion” is checked.

Don’t have the DBeaver AI package available to install

  • Ensure the DBeaver is updated
    • Help > Check for Update
  • Manually Add the package
    • Click Help > Install New Software > Add