Example: Salesforce Connector
TWX_Salesforce_Connector_ExtensionPackage provides generic APIs to connect to and perform CRUD operations in Salesforce systems using Salesforce REST APIs. Salesforce credentials are used for authentication in ThingWorx, and the authentication token that is created is used when interacting with the Salesforce system.
Services
The Salesforce Connector Extension provides the following services to perform CRUD operations in the Salesforce system:
  • CreateResources— Creates the specified resources in the target Salesforce system. The service returns the specified fields of the newly created resources and provides information regarding failures.
  • DeleteResources— Deletes the specified resources in the target Salesforce system. The service returns the resource IDs that could not be deleted and the reason for each failure.
  • DescribeResource— Returns field names and other information for the Salesforce object with a specified ID.
  • EscapeStringForSOSL— Utility API to escape the string used for a Salesforce SOSL find clause.
  • FindResource— Returns the requested fields for the Salesforce resource with a specified ID.
  • FindResources— Returns the requested fields for the Salesforce resources matching the specified query criteria.
  • InvokeSalesForceRESTService— Invokes Salesforce REST call and returns a JSON result which includes the status code, content body, and headers.
  • InvokeSalesForceRESTServiceAndReturnString— Invokes Salesforce REST call and returns a String representation of the JSON result which includes the status code, content body, and headers.
  • UpdateResources— Performs specified updates to the Salesforce objects. It returns the refreshed fields from the updated Salesforce resources and provides information regarding failures.
Extension Dependencies
  • TWX_IDP_ExtensionPackage— This extension is used by the TWX_Salesforce_Connector_ExtensionPackage to provide the authentication token required to perform REST API calls in the Salesforce system.
Best Practices for Developing Resource Connectors
This section describes the best practices for creating resource connectors to perform CRUD operations in external systems:
  • Authentication— The connector is responsible for satisfying authentication required by the back-end system. This example Salesforce connector uses the same back-end system as the TWX_IDP_ExtensionPackage and therefore uses the access token provided by the TWX_IDP_ExtensionPackage to authenticate in the Salesforce system.
  • Service calls— The connector uses APIs that are provided by the external system to perform CRUD operations. The TWX_Salesforce_Connector_ExtensionPackage uses the Salesforce REST APIs to perform the work. Ideally, a connector should provide a consistent implementation for all resource types of interest in the back-end system. There may be cases where some resource types require a different implementation. In those cases, one connector can be created to provide all the required implementations. Alternatively, a separate connector can be created for each resource type requiring a different implementation.
  • Data translation between ThingWorx and the external system— The connector must translate between ThingWorx IPrimitiveTypes and corresponding the back-end system types. See convertFromSfJSONToIPrimitiveType() and convertAndAddSfJson() and their usages in SalesforceConnector for an example of such translation.
  • Query translation— The connector must translate from a ThingWorx Query to a query executed in the back-end system. See buildSalesforceQuery() in SalesforceConnector for an example of such translation. In some cases, a back-end system may not be able to handle all criteria and sorts that a ThingWorx query can contain. In this case, the connector must set aside portions of the query and then apply these to the back-end system results as a post-processing step. See populateInfoTableFromSfqlResult() in SalesforceConnector for an example of such post-processing, specifically noting the use of 'postFilter'.