4. Create the Expense Claim Currency Code SmartObject
In this step, you will create the Expense Claim Currency Code SmartObject from scratch. In the last topic, you auto-generated the Expense Claim Categories SmartObject from its service instance, which automatically created a SmartObject with all the available methods and properties for the targeted service object. However, by creating a SmartObject from scratch, you can create your own methods and properties and tailor them specifically to your application. In this step, you will create two methods based on service objects from the K2 Tutorial Currency REST Service, service instance. They are as follows:
- List Currencies: This method returns a list of currency codes which you will use later on in a currency code drop-down list control on the Expense Claim Form. Users can select a code as they enter their line items. This allows users to enter expense claims that occurred in other countries.
- Calculate USD Amount: This method performs a mathematical calculation that converts all line item amounts to US dollars. You will use this later on to auto-calculate the equivalent US dollar amount for an expense entered in another currency.
-
First, return to the the Designer. Add a new SmartObject to the SmartObjects category. Use the table below for the SmartObject settings. If a setting is not specified in the table, assume the default value.
Field Value Notes Name Expense Claim Currency Codes Description This SmartObject provides currency code look-up methods for expense claims. Category Leave this as-is
(K2 Learning\Expense Claim\SmartObjects)Type Advanced SmartObject You will point this SmartObject to the REST service, service instance that you created earlier. - Return to the the Designer site. (It should already be a tab in your browser.) Navigate to your Expense Claim categories. Right-click the SmartObjects category, then select New SmartObject.
- In the GENERAL tab, configure the properties for the new SmartObject using the table below as a guide. Click CREATE.
Field Value Notes Name Expense Claim Currency Codes Description This SmartObject provides currency look-up methods for expense claims. Category Leave this as-is
(K2 Learning\Expense Claim\SmartObjects)Type Advanced SmartObject You will point this SmartObject to the REST service, service instance that you created earlier.
- Return to the the Designer site. (It should already be a tab in your browser.) Navigate to your Expense Claim categories. Right-click the SmartObjects category, then select New SmartObject.
-
First, you will add and configure the method that returns a list of currency codes. You will apply this method to a drop-down list on the Expense Claim form in a later step.
Add a new method called List Currencies that points to the following service object method: K2 Tutorial Currency REST Service > Service Operations > ListCurrencies > ListCurrencies. Bind the returned string property to a new SmartObject property called Currency Codes.- In the PROPERTIES & METHODS tab, click the Methods tab, then click Add. You will select a method from the K2 Tutorial Currency REST Service, service instance.
Recall that you registered a new service instance of the REST Service. Nintex K2 discovered the objects found in the REST service and returned the properties and methods. Because the REST service already has properties, you do not need to define them (which is why you are skipping over the Properties tab). For the Expense Claim application, you will map one of the methods that was discovered for the service.
- On the Add Method screen, select the following service object method:
K2 Tutorial Currency REST Service > Service Operations > ListCurrencies > ListCurrencies, then click NEXT. - Change the name of the method from 'ListCurrencies' to
List Currencies
then select the Default list method check box. Click NEXT to continue.ServiceObjects can have more than one list method. For example, an Active Directory ServiceObject might have a GetUsers list method that returns all user accounts. The ServiceObject might also have a method that returns all users in a specific group. By selecting the default list method option, Nintex K2 knows which list method to use throughout your application. - You are now on the INPUTS & OUTPUTS screen. Double-click the String RETURN PROPERTY NAME. You will create a new property in the SmartObject that is bound to this string return property. The new property is what you will use in your drop-down list for returning currency codes. (The "string" return property is the property in the data source that contains the currency codes.)
- In the dialog box that opens, click the CREATE button to create a new property called
Currency Codes
then click OK and OK again to return to the mapping screen. - You should see the service method return property is now bound to the new SmartObject property, Currency Codes. Click NEXT, then FINISH to complete the wizard.
- In the PROPERTIES & METHODS tab, click the Methods tab, then click Add. You will select a method from the K2 Tutorial Currency REST Service, service instance.
-
Now, you will add the second method. This method performs a mathematical calculation behind-the-scenes. It automatically converts all expense claim line items to US dollars. For example, if the user enters a line item in GBP (UK pounds), we will call this method to convert the amount to USD. This way, all line items are of the same currency and can be accurately totaled. It also provides a convenience to your users who don't have to do the calculation themselves.
Add another method called Calculate USD Amount to the SmartObject that points to the service object method K2 Tutorial Currency REST Service > Objects Types> HistoricalConversion > HistoricalConversion. Configure the input parameters and return property as follows:- Because your users have the option to enter expense claim line items in another country's currency, you need a way to convert that amount to US dollars. You want all the line items to be US dollars that can be summed for a final total amount. In this step, you will add a method which calculates the USD exchange rate for a given date, amount and currency code.
Once again, click the Add button to add another method to the SmartObject. - On the Select a ServiceObject method screen, select the following ServiceObject method:
K2 Tutorial Currency REST Service > Object Types > HistoricalConversion > HistoricalConversion, then click NEXT. - Rename the method to
Calculate USD Amount - Date - the date that the expense was incurred (because currency rates change from day to day).
- FromCurrencyCode - the starting currency type (GBP for British pound for example).
- FromAmount - the amount to be converted.
- ToCurrencyCode - the ending currency type (USD, for example).
- On the bottom half of this screen, click the Add button to add a new parameter of type Text called
FromCurrencyCode
then click OK. - Add another parameter of type Decimal called
FromAmount
then click OK. - Add another parameter of type Date/Time called
Date
then click OK. - You should see your three input parameters listed at the bottom of the screen. You don't need to add a parameter for "ToCurrencyCode" because that value is hard coded. (The user won't be inputting this value.) Click NEXT.
- Select the Date parameter, then click the Assign button.
- In the dialog box, select SmartObject method parameter from the Map to drop-down list, then select Date from the Parameter drop-down list. Click OK.
- Select the FromAmount method parameter and use the same procedure to map it to the FromAmount SmartObject method parameter that you created earlier. Click OK.
- Repeat the same procedure to map the FromCurrencyCode method parameter to the FromCurrencyCode SmartObject method parameter.
- Last, select ToCurrencyCode, then Assign it to a Specific value. For the value, enter:
USD
(For this tutorial, you will always convert currencies back to US dollars, which is the static value you are entering in this step.) Click OK. - Select the Decimal return property on the bottom half of the screen, then click Assign. Create a new SmartObject property called
USD Amount
then click OK and OK once again. - Your INPUTS & OUTPUTS screen should look like the image below. Click NEXT, then FINISH to complete the method wizard.
- You should now have two methods for the advanced SmartObject. Click FINISH to complete the SmartObject configuration.
This method requires input parameters to perform the conversion calculation, then return a result. There are four parameters necessary for the conversion process:On the next screen, notice the four ServiceObject method input parameters. These are the parameters discovered in the ServiceObject. You will map the three parameters that require values at runtime for the conversion to take place. The fourth parameter is the ToCurrencyCode parameter and you are going to hard-code that value so that it is always USD.
Once the currency conversion is complete, the HistoricalConversion method will return a Decimal value. Like the List Currencies method, you will create a new SmartObject property for the returned value in your Expense Claim Currency Codes SmartObject. In a later step, you will map the new property to a form field, which will in turn, display the converted value on your form.
- Because your users have the option to enter expense claim line items in another country's currency, you need a way to convert that amount to US dollars. You want all the line items to be US dollars that can be summed for a final total amount. In this step, you will add a method which calculates the USD exchange rate for a given date, amount and currency code.
-
Before moving on to the next tutorial topic, take a minute to test the SmartObject from the Management site. (Categories > K2 Learning > Expense Claim > SmartObjects > Expense Claim Currency Codes) This validates your method configurations are correct, plus gives you a glimpse into how the calculate method takes the parameter values, then calculates the return amount (USD Amount).
- To test the two new methods, return to the Management site. (It should be a tab in your browser. If not, launch it from the start menu, short cut, or from a URL provided by your Nintex K2 administrator.)
- Expand the Categories node, then navigate to the Expense Claim Currency Codes SmartObject. (K2 Learning > Expense Claim > SmartObjects)
- Click the Expense Claim Currency Codes SmartObject to expose the properties and methods in the central pane. Scroll down to the Methods section, select the List Currencies method, then click Execute.
- You don't need any filters for this method, so click Execute once again. Confirm you have results returned displaying currency codes. These are the values you will use for the currency code drop-down list on the Expense Claim form. Click Done to exit the results screen.
- Now test the currency calculation method. Select the Calculate USD Amount method, then click the Execute button. This time, you must provide the input parameters you just configured, so that the calculation can be executed and return the result. You must provide values for the FromCurrencyCode, FromAmount and Date parameters. (Technically, the ToCurrencyCode parameter is also required, but recall that you hard-coded it for US dollars, so you don't need to provide that input manually.)
- For the FromCurrencyCode value, enter
GBP
then for the FromAmount, enter
100
and finally, for the Date, enter today's date. Click Execute. Confirm you have a result returned. Click Done to exit the results screen.
Review
In this step, you created an advanced SmartObject from scratch, that connects to a REST service to work with currency data. You configured two methods. The first returns a list of currency codes, which you will use to populate a drop-down list. The second calculates the US dollars equivalent for a given currency and source amount, for a specific date. You will use this method to convert amounts entered with a non-US country code. This makes it easier for your users to submit travel expenses since they don't have to do the currency conversion by hand. It also allows for easy calculation of total expense claim amounts.
Remember, Nintex K2 can generate SmartObjects for you from a service instance. You can also create SmartObjects from scratch, as you did in this step. Creating a SmartObject from scratch allows you full control over the properties and methods for that SmartObject.