/salesforce-jdbc

Primary LanguageJavaMIT LicenseMIT

Salesforce JDBC Driver

Salesforce JDBC driver allows Java programs to connect to a Salesforce data services using standard, database independent Java code. Is an open source JDBC driver written in Pure Java, and communicates over SOAP/HTTP(S) protocol. The main purpose of the driver is to retrieve (only) data from Salesforce services for data analysis. Primary target platform for the driver usage is Eclipse BIRT engine.

The original Git repository for this driver is here However that version is not compatible with IntelliJ because of a lot of unsupported features:

  • table names and columns names filtration is not implemented
  • table name and column names are case sensitive
  • no metadata provided for queries so IntelliJ just ignores the results returned by the driver

These issues were fixed in the current version in this fork.

Watch the demo video

Watch the demo video

Supported Salesforce and Java versions

The current version of the driver should be compatible with Salesforce Partner API version 58.0 and higher and Java 17.

Get the driver

Download the driver JAR file:

  1. Read-Only version 1.3.1
  2. Write/Delete support version 1.4.5

Supported features

  1. Queries support native SOQL;
 select Id, Account.Name, Owner.id, Owner.Name from Account;
 
 -- * fields reference will expand into up to 100 first fields of the entity for root entity
 select * from Account;
  1. Nested queries are supported;

  2. Write is supported as INSERT/UPDATE statements for version >= 1.4.0

    The following functions are supported as part of calculation of new values:

    • NOW()
    • GETDATE()

    For Example:

 INSERT INTO Account(Name, Phone) VALUES 
  ('Account01', '555-123-1111'),
  ('Account02', '555-123-2222');
  
  INSERT INTO Contact(FirstName, LastName, AccountId) 
    SELECT Name, Phone, Id 
      FROM Account
      WHERE Name like 'Account0%';

  UPDATE Contact SET LastName = 'Updated_Now_'+NOW()
      WHERE AccountId IN (
          SELECT ID from Account where Phone = '555-123-1111' AND CreatedDate > {ts '2020-01-01 00:10:12Z'}
      );
  1. DELETE is supported for version >= 1.4.1
DELETE from Opportunity where name like 'Pushed Out Insightor Opp%';

Example of response

  1. Request caching support on local drive. Caching supports 2 modes: global and session. Global mode means that the cached result will be accessible for all system users for certain JVM session. Session cache mode works for each Salesforce connection session separately. Both modes cache stores request result while JVM still running but no longer than for 1 hour. The cache mode can be enabled with a prefix of SOQL query.

How to use:

  • Global cache mode:
CACHE GLOBAL SELECT Id, Name FROM Account
  • Session cache mode
CACHE SESSION SELECT Id, Name FROM Account
  1. Reconnect to other organization at the same host
-- Postgres Notation
CONNECT USER admin@OtherOrg.com IDENTIFIED BY "123456"

-- Oracle Notation
CONNECT admin@OtherOrg.com/123456

-- Postgres Notation to a different host using secure connection (by default)
CONNECT 
    TO ap1.stmpa.stm.salesforce.com
    USER admin@OtherOrg.com IDENTIFIED BY "123456"

-- Postgres Notation to a different host - local host using insecure connection
CONNECT 
    TO http://localhost:6109
    USER admin@OtherOrg.com IDENTIFIED BY "123456"

P.S. You need to use the machine host name in the connection url - not MyDomain org host name.

Limitations

  1. Version < 1.4.0 The driver is only for read-only purposes now. Insert/update/delete functionality is not implemented yet.
  2. Version >= 1.4.0 Limited support of INSERT/UPDATE operations
  3. Version >= 1.4.1 Limited support of DELETE operations

With Maven

Add dependency

<dependency>
    <groupId>com.ascendix.salesforce</groupId>
    <artifactId>salesforce-jdbc</artifactId>
    <version>1.5.4-release</version>
 </dependency>

Warning

The 1.5.4-release uses a custom version of JSQLParser that adds the INCLUDES and EXCLUDES operators needed for filters on picklists. The change has been merged into the library, until the release of version 4.10 with the change, the custom version must be used.

How to connect

Driver class name

com.ascendix.jdbc.salesforce.ForceDriver

JDBC URL format

jdbc:ascendix:salesforce://[;propertyName1=propertyValue1[;propertyName2=propertyValue2]...]

There are two ways to connect to Salesforce:

  1. by using user and password;
  2. by using sessionId.

User and password parameters are ignored if sessionId parameter is set.

An example for a connection URL with user and password parameters:

jdbc:ascendix:salesforce://;user=myname@companyorg.com.xre.ci;password=passwordandsecretkey

An example for a connection URL with sessionId parameter:

jdbc:ascendix:salesforce://;sessionId=uniqueIdAssociatedWithTheSession

Configuration Properties

Property Description
user Login username.
password Login password is associated with the specified username.
Warning! A password provided should contain your password and secret key joined in one string.
sessionId Unique ID associated with this session.
loginDomain Top-level domain for a login request.
Default value is login.salesforce.com.
Set test.salesforce.com value to use sandbox.
https Switch to use HTTP protocol instead of HTTPS
Default value is true
api Api version to use.
Default value is 50.0.
Set test.salesforce.com value to use sandbox.
client Client Id to use.
Default value is empty.
insecurehttps Allow invalid certificates for SSL.

Configure BIRT Studio to use Salesforce JDBC driver

  1. How to add a JDBC driver

  2. How to set configuration properties for Salesforce JDBC driver.

    Birt provides various ways to set parameters for JDBC driver. For example, it can be done with the property binding feature in the data source editor and a report parameter.

    image

    See how it's done in [Salesforce JDBC report sample](docs/birt/Salesforce JDBC sample.rptdesign)

Configure IntelliJ to use Salesforce JDBC driver

  1. How to add a JDBC driver

  2. How to set configuration properties for Salesforce JDBC driver.

    IntelliJ provides various ways to set parameters for JDBC driver. For example, it can be done with the property binding feature in the data source editor and a report parameter. Example JDBC Url:

    jdbc:ascendix:salesforce://dev@Local.org:123456@localorg.localhost.internal.salesforce.com:6109?https=false&api=48.0

    Please check what kind of access do you have to your org - HTTP or HTTPS and the API version to use. Here is screenshot about results output and autocomplete support for SOQL queries in IntelliJ:

    image

In case of issues with the WSDL

Steps to update the partners.wsdl

  1. Get and build https://github.com/forcedotcom/wsc

  2. Run command:

    java -jar target/force-wsc-50.0.0-uber.jar blt/app/main/core/shared/submodules/wsdl/src/main/wsdl/partner.wsdl sforce-partner.jar

  3. Copy the com.sforce.soap to the driver

Version History

See CHANGELOG.md.

Contributing

Interested in making contributions to this project? Please review the guides below.

Also, consider sponsoring this project! ✌️