It’s my pleasure to introduce you to the brand-new IBM Watson SDK for Salesforce. The creation of this SDK shows the strong commitment of Salesforce and IBM for our developer communities, as it makes it easier to use Watson capabilities within Salesforce.
Why an SDK? It’s just an API!
Good question. If you ever tried to create a decent wrapper for a REST service, you likely remember how many lines of code you had to write. Authentication, authorization, error handling, creating, updating, reading data, and more. Using an SDK makes it easier for you. You can focus on implementing business requirements. Not that you shouldn’t check out the underlying API mechanics and how the SDK implemented them—but you don’t have to know them, if you don’t want to.
How to get started
This first release of the SDK includes these services.
In case you want to manually deploy the SDK to a scratch environment, execute the following commands with the Salesforce CLI.
git clone https://github.com/watson-developer-cloud/salesforce-sdk cd salesforce-sdk sfdx force:org:create -a watson-sdk -s -f config/project-scratch-def.json sfdx force:source:push sfdx force:org:open
This creates a new scratch environment with the alias
watson-sdk, and pushes the source code to the environment. Additional options, for example using the ant metadata deployment, are outlined in the readme file of the repo.
As mentioned above, the first version of the SDK supports access to Watson Conversation and Watson Discovery APIs—more to be added over the course of the next few weeks
Note that using the Watson services requires service credentials in IBM Cloud, meaning you’re required to create an IBM Cloud account if you do not have one already. We recommend using Named Credentials in Setup to authenticate with your account, and you can do this by following these simple steps here.
This code snippet shows, for example, how to fetch the details of a specific Watson Discovery collection.
IBMDiscoveryV1 discovery = new IBMDiscoveryV1(IBMDiscoveryV1.VERSION_DATE_2017_09_01); IBMDiscoveryV1Models.GetCollectionOptions options = new IBMDiscoveryV1Models.GetCollectionOptionsBuilder() .environmentId(environmentId) .collectionId(collectionId) .build(); IBMDiscoveryV1Models.Collection resp = discovery.getCollection(options);
Here’s what’s going on in the code above.
- Line 1: A new
IBMDiscoveryV1object for API version 2017-09-01 is created. This allows for backward compatibility to IBM’s services using this versioning scheme.
- Line 4: Using the builder pattern, the options for executing the GET request are set up. For this request, the
collectionIdare required parameters.
- Line 10: This executes the actual GET request and returns an object of type
The pattern can be applied to any of the service calls. You can explore the full range of examples in the test folder here.
The IBM Watson SDK for Salesforce is also Lightning ready! IBM exposed all necessary model class properties with the @AuraEnabled annotation. This lets developers use the SDK not only in Apex, but also directly in Lightning.
Your next steps
If you are at Dreamforce ’17, make sure you check out the Developer Session here, in Moscone West on Tuesday, November 7 at 3:15 PM PST.
You can sign up for the Watson services on IBM Cloud. After you’re signed up clone the SDK and install it into a scratch environment of your choice. Use the great examples from the SDK as your first steps. And last, but not least, try out the new Trailhead project Surface Data from IBM Watson Discovery in Salesforce, where you can learn to connect to the Discovery service without writing a single line of code. And comment on this blog post about which Watson services you’d like to see next in the SDK.
About the author
René Winkelmeyer works as Senior Developer Evangelist at Salesforce. He focuses on enterprise integrations, mobile, and security with the Salesforce Platform. You can follow him on Twitter @muenzpraeger.