3. Create Service Instances for External Data Sources; Generate a SmartObject
In this step, you begin creating the data objects for your Expense Claim application. The first two data objects connect to external data sources. The first external data source is a SQL database we will use to look up Expense Categories, and the second data source is a REST service that we will use to work with currency conversion.
The first step when integrating with external systems such as SQL or REST services is to create a service instance. The service instance contains the connection configuration such as URL, server name, user name, password, and so forth. You will create a SQL Server service instance to connect to the SQL database, and a REST service, service instance to connect to the currency service.
Creating a service instance allows Nintex K2 to discover the objects (such as the properties and methods) that the data source contains. After you create a service instance, you can create a SmartObject from the service instance that allows Nintex K2 to interact with the data source through forms, workflows and rules.
Service types, service brokers, and service instances are the elements that allow Nintex K2 to interact with other systems, and which form the base layer for SmartObjects. The following is a brief overview of each concept:
- Service Type: A service type is a pointer to a broker file for a specific system or data source. Examples include: SQL Server, SharePoint, CRM, and web services. Each service type has an underlying service broker associated with it.
- Service Broker: A file that contains the logic needed to interact with a specific system. Each service type has its own requirements for interacting with the system. For example, what type of authorization will the system allow? What type of data is contained in the system?
- Service Instance: A service instance is a single connection to a data source, and is based on the service type. The service instance uses the requirements defined by the broker to connect to the target data source. For example, you might have an instance of a SQL Server service type. The instance is specific to a single SQL database. If you have multiple databases, you need multiple instances. From the instances, you can then generate SmartObjects.
SmartObjects: The middle layer that allows interaction between an object (form, view, workflow) and the target data source. For example, you have a form bound to a SQL SmartObject. When you submit the form, the SmartObject creates a new record or it updates an existing record in the SQL table. The SmartObject connects Nintex K2 to the system via the service layer.
- Create a database to connect to.
- For this example, you must create a SQL database called K2Learning. Alternatively, you can use the following SQL script to create the database. Ensure the K2 Service Account has db owner rights to the database.
- The database requires the following:
Table Name Table ExpenseCategory Column Name Data Type Column Setting Columns ExpenseCategoryId int (Autonumber) Primary key ExpenseCategoryName nvarchar(50) ExpenseCategoryCode nchar(10) - Create the following entries in the database. If you used the SQL script, these entries already exist but you still have to assign rights to your K2 Service Account.
- Return to the Management site. Use the table settings below to configure a new Service Instance of the SQL Server Service, service type. This data source provides look-up values for the expense claim categories.
Field Name Setting A Display Name K2 Tutorial Sample SQL Database B Description This is the service instance of the sample database used in the K2 Learning tutorial. Service Type SQL Server Service (default) C Authentication Mode Static D User Name Azure SQL user username (Admin user) E Password Azure SQL user password (Admin user) F On Different SQL Server true G Database K2Learning H Server Your Azure SQL server name I Command Timeout 90 J Use Native SQL Execution false K Generate SmartObjects for this Service Instance UNCHECKED - In the Management menu, expand the Integration node, then click Service Types. You will see current service types displayed in the central pane. Scroll through the service types until you locate SQL Server Service. You can also use the search option instead of scrolling through the pages. (You may see more than one result returned.)
- Select SQL Server Service, then click the New Instance button in the toolbar.
- Use the table below to configure the service instance. There is an image below the table for reference as well. If a setting is not indicated in the table, assume the default value. Click OK when you have entered the configuration settings, and then click OK for the confirmation dialog. (It may take a few seconds for the confirmation dialog box to appear).
Field Name Setting A Display Name K2 Tutorial Sample SQL Database B Description This is the service instance of the sample database used in the K2 Learning tutorial. Service Type SQL Server Service (default) C Authentication Mode Static D User Name Azure SQL user username (Admin user) E Password Azure SQL user password (Admin user) F On Different SQL Server true G Database K2Learning H Server Your Azure SQL server name I Command Timeout 90 J Use Native SQL Execution false K Generate SmartObjects for this Service Instance UNCHECKED
- In the Management menu, expand the Integration node, then click Service Types. You will see current service types displayed in the central pane. Scroll through the service types until you locate SQL Server Service. You can also use the search option instead of scrolling through the pages. (You may see more than one result returned.)
- Create a second service instance using the REST service type. Use the table settings below as a guide for the service instance configuration. This service provides two methods. The first method returns a list of currency codes (such as GBP, USD) for you to select when entering your expense claim line item. The second method performs a calculation that converts an amount entered to US Dollars. For example, if you enter a line item amount and select GBP as the currency code, the second method converts the amount and outputs it as USD.In a shared environment, you only need to create the service instance once. If the service instance already exists and you want to create your own, use a unique identifier of some kind. One example might be to add your initials to the service instance name.This REST service is for tutorial purposes only. Do not use this REST service for production implementations. Contact a member of your IT support team for help, if necessary.
Field Value Notes Service Instance A. Display Name K2 Tutorial Currency REST Service If you are working in a shared environment, include your initials in the display name to make the name unique and easily identifiable to you. B. Description This is the service instance of the sample currency REST service used in the Expense Claim tutorial. C. Service Type REST Service Authentication D. Authentication Mode Impersonate The Authentication Mode of the service instance should be set according to the authentication required by the endpoint. For example, if the endpoint supports static authentication then select the static authentication mode and provide a username and password. Service Keys
There is only one service key value to set in this step (Descriptor Location).E. Descriptor Location https://k2learning.azurewebsites.net/api/swagger.json This is the location of the descriptor file. This must be a URL. The Authentication Mode used for the service instance is also used to access the Descriptor Location JSON file when that file is a URL. This file must be available at this location when the service instance is refreshed. Paths in the Swagger file must be unencoded. For more information about getting a Swagger file, see Resources for working with the REST Service Broker. - Next you will add another service instance which points to the REST service used for currency conversion operations. Begin by opening a new browser window to verify you can connect to the following REST service:
https://k2learning.azurewebsites.net/api/swagger/ui#/
You should see a screen similar to the image below. This verifies that your firewall configuration is sufficient for connecting to this REST service. - Returning to the Management site, expand the Integration node, then click Service Types. Scroll down to (or search for) the REST service type. Select REST, then click the New Instance button in the toolbar.
- Use the table below to configure the service instance. There is an image below the table to use as a reference as well. If a setting is not indicated in the table, assume the default value. Click OK when you have entered the configuration settings. Click OK for the confirmation dialog.
Field Value Notes Service Instance A. Display Name K2 Tutorial Currency REST Service If you are working in a shared environment, include your initials in the display name to make the name unique and easily identifiable to you. B. Description This is the service instance of the sample currency REST service used in the Expense Claim tutorial. C. Service Type REST Service Authentication D. Authentication Mode Impersonate The Authentication Mode of the service instance should be set according to the authentication required by the endpoint. For example, if the endpoint supports static authentication then select the static authentication mode and provide a username and password. Service Keys
There is only one service key value to set in this step (Descriptor Location).E. Descriptor Location https://k2learning.azurewebsites.net/api/swagger.json This is the location of the descriptor file. This must be a URL. The Authentication Mode used for the service instance is also used to access the Descriptor Location JSON file when that file is a URL. This file must be available at this location when the service instance is refreshed. Paths in the Swagger file must be unencoded. For more information about getting a Swagger file, see Resources for working with the REST Service Broker. F. Generate SmartObjects for this Service Instance UNCHECKED You will manually create a SmartObject in a later step. - Still in the Integration node, click Service Instances. Confirm the two new service instances appear in central pane, K2 Tutorial Sample SQL Database and K2 Tutorial Currency REST Service. You have completed the external data source connections.
- Next you will add another service instance which points to the REST service used for currency conversion operations. Begin by opening a new browser window to verify you can connect to the following REST service:
- Next, you will generate a SmartObject for the SQL database service instance. The SmartObject acts as the "bridge" for interaction between the SQL data source and artifacts (such as views, forms, workflows).
Generate a SmartObject from the K2 Tutorial Sample SQL Database service instance. Select (only) the [Finance].[ExpenseCategory] table.- Still in the Integration section, click Service Instances once again. Navigate to (or search for) the K2 Tutorial Sample SQL Database service instance in the central pane. Select the service instance, then click the Generate SmartObjects button in the toolbar.
- On the Generate SmartObjects screen, expand the Tables node. (If the Tables and/or Views nodes are already checked, uncheck them first. You do not want to create SmartObjects from all the tables and views, you will be selecting a single table from which to create the SmartObject.) CHECK the [Finance].[ExpenseCategory] table, then click OK.
- Still in the Integration section, click Service Instances once again. Navigate to (or search for) the K2 Tutorial Sample SQL Database service instance in the central pane. Select the service instance, then click the Generate SmartObjects button in the toolbar.
-
Next, you have two minor adjustments to make to the expense categories SmartObject you just generated. Because you generated the SmartObject from the service instance, Nintex K2 saved the SmartObject in a generic category, as the table name [Finance].[ExpenseCategory]. First, you will move the SmartObject to your Expense Claim > SmartObjects category, then you will rename the SmartObject so that it's easier to work with.
Switch to the Categories menu, then edit the [Finance].[ExpenseCategory] SmartObject. Move the SmartObject to the Expense Claim > SmartObjects folder, then rename it to Expense Claim Categories.- Still in the Management site, expand the Categories node. (You can collapse the Integration node if you need to.) Navigate to the [Finance].[ExpenseCategory] SmartObject. (SQL Server Service > K2 Tutorial Sample SQL Database > Tables)
- Select the SmartObject, then click the Design link in the central pane. After the Designer launches, click the Edit link in the central pane.
- Switch to the GENERAL tab. Rename the SmartObject
Expense Claim Categories
then move it to the K2 Learning > Expense Claim > SmartObjects category. - Click FINISH to save and exit the SmartObject. Close this browser window if you already have the Designer opened in another tab.
- Still in the Management site, expand the Categories node. (You can collapse the Integration node if you need to.) Navigate to the [Finance].[ExpenseCategory] SmartObject. (SQL Server Service > K2 Tutorial Sample SQL Database > Tables)
- Before moving on to the next tutorial topic, take a minute to test the categories SmartObject. You can easily test SmartObjects from within the Management site. This will confirm your SmartObject is ready for use in other artifacts. You will test the SmartObject by executing a list method that returns all the items from the data source.
From the Management site, execute the Expense Claim Categories SmartObject's list method and confirm you see a list of categories returned. You may need to refresh the Categories node first to find the SmartObject in its new location. The SmartObject should be in the K2 Learning > Expense Claim > SmartObjects category.- You can execute SmartObjects from within the Management site. Because you just renamed and moved the categories SmartObject, you need to refresh the Categories menu to see the changes. Right-click Categories, then select Refresh Menu.
- Navigate to the Expense Claim Categories SmartObject. It should be in the K2 Learning > Expense Claim > SmartObjects folder.
- Select the Expense Claim Categories SmartObject. Notice the central pane displays the details for the SmartObject. The Properties section displays the properties (think columns in a table) and the Methods section displays the different ways you can interact with data from this SmartObject. This data source allows you to create, delete, list, read, and update its data content. Select the List method, then click Execute.
- Click the Execute button in the lower right corner of the screen. You will see results displayed in the same screen, which confirms the connection. This data source provides the drop-down list values for the expense claim categories. Click Done to close the screen.
The Execute SmartObject Method screen opens. Here, you can apply filters before executing the method. In some cases, such as read methods, you must provide input parameters before executing the method. For example, you might provide the ID of a record you want returned. For this test, you just want to confirm the connection, so you don't need any filters.
- You can execute SmartObjects from within the Management site. Because you just renamed and moved the categories SmartObject, you need to refresh the Categories menu to see the changes. Right-click Categories, then select Refresh Menu.
Review
In this step, you began creating the data sources for this application. The first two data sources are external, meaning they do not exist on your Nintex K2 server locally. To connect to external data sources, you begin by creating service instances of existing service types. (Service types do not have to be existing, you can in fact, create your own custom service types.) You configured connections to a SQL database and a REST Service. Once you have the service instance, you must generate or create a SmartObject. The SmartObject acts as the "bridge" between the data source, and the entities that consume the data (such as views, forms, and workflows). In this exercise, you auto-generated a SmartObject from a table found in the SQL database. In the next exercise, you will create a new SmartObject from scratch using the REST Service, service instance.