How to connect Ruby with ServiceNow

Introduction

Connecting Ruby to ServiceNow via ODBC enables seamless integration with your IT service management (ITSM) data, allowing you to automate workflows, retrieve incident reports, and interact with ServiceNow records programmatically. The ruby-odbc gem will enable you to establish a secure connection, execute queries, and enhance data management within ServiceNow. This guide will walk you through the installation, configuration, and troubleshooting process to ensure a smooth connection between Ruby and ServiceNow using ODBC.

Prerequisites

  • ODBC PowerPack: Download and install the ZappySys ODBC here.
  • ServiceNow Account

Steps

Step 1: Installing Ruby and the ruby-odbc Gem

Some versions of Ruby may not be fully compatible with the ruby-odbc gem. To ensure proper functionality, using Ruby 3.1.xis recommended, as newer versions may introduce compatibility issues.

  1. Download the installer from Ruby installer
  2. Install Ruby and DevKit.
  3. Open Command Prompt as an administrator.
  4. Verify the version you are using with this command ruby -v
  5. Install the ruby-odbc gem with the following command: gem install ruby-odbc
  6. Verify the gem installed with this command: gem list ruby-odbc

Note: If you encounter installation issues, you may need administrative permissions.

Step 2: Obtain ServiceNow Credentials

  1. Log in to ServiceNow Developers.
  2. Click on your profile icon and select your instance.
  3. Click “Manage instance password.”
  4. Note the Instance name, Username, and Password.

Step 3: Create a New ODBC Driver

  1. Search for “ODBC” on your system and open the ODBC Data Source application.

  2. Under User DSN or System DSN, click Add to add a new System DSN and select ZappySys API Driver.

  3. Choose ServiceNow as the connector and click Continue.

Step 4: ServiceNow Connector Configuration

  1. Enter your Instance name, Username, and Password and test the connection.

  2. In the Preview Tab, select a table, preview the data, and click OK to save.

Step 5: Connecting Ruby to ServiceNow via ODBC

Once the DSN is set up, you can use ruby-odbc to establish the connection and run queries. Create a Ruby file, e.g., ServiceNowRuby.rb :

require 'odbc'

# DSN name configured in ODBC
# DSN name configured in ODBC
dsn = "API Driver - ServiceNow"  # Replace with the correct name

begin
  # Connect to the database using DSN
  db = ODBC.connect(dsn)

  # Verify connection
  puts "✅ Successfully connected to ODBC"

  # Execute a test query
  query = "SELECT * FROM acl_trail"  # Adjust according to your database
  stmt = db.run(query)

  # Check if there is data
  rows = stmt.fetch_all
  if rows.empty?
    puts "⚠️ No data found in the database."
  else
    # Display results
    rows.each do |row|
      puts row.inspect
    end
  end

  # Close connection
  stmt.close
  db.disconnect

rescue ODBC::Error => e
  puts "❌ Connection error: #{e.message}"
end

When you run the code, it will call the ServiceNow API via ODBC and retrieve data about your campaign lists.

Common Errors and Solutions

:x: Error: cannot load such file -- dbi (LoadError)

Cause: You are trying to use the dbi gem, but it is not installed.
Solution:

gem install dbi
gem install dbd-odbc

:x: Error: ERROR: Failed to build gem native extension.

Cause: A compiler or necessary libraries are missing when compiling the gem. Solution: Install MSYS2 and ensure gcc and make are available.


:x: Error: Data source name not found, and no default driver specified.

Cause: The DSN is not configured, or the driver is not installed. Solution:

  • Verify the configuration in odbcad32.exe
  • Ensure the DSN name in the code exactly matches the one configured in ODBC.

:x: Error: Issues with Ruby Version

Cause: Some Ruby versions may not be compatible with the ruby-odbc gem, especially if they are too new or old. Solution:

  • Check your Ruby version with:
ruby -v
  • If your version is 3.2.x and you experience issues, try using an earlier version of Ruby (e.g., 2.7.x) or use RVM to switch versions:
rvm install 2.7.8
rvm use 2.7.8

Conclusion

To start, download and install the ZappySys ODBC PowerPack to enable ODBC connectivity.

Integrating Ruby with ServiceNow through ODBC provides a powerful way to manage ITSM data, automate tasks, and streamline operations. Following this guide, you can configure your connection, troubleshoot potential issues, and optimize your workflow. If you encounter difficulties, review the error logs and verify your DSN setup.

Related Links:

Contact Us

If you encounter any challenges or have specific use cases, please contact our support team via chat or ticket.