Thursday, 26 April 2012

The form for currency conversion basically consists of the following components:
  • An input value
  • A from currency
  • A to currency
  • A submit button
This can be seen in the following screenshot:

2012-04-23-InfoPathCurrencyConverter-Part01-01.png
In this Part i will first demonstrate how we build the required form in InfoPath. I will use a static rate of exchange to prove the calculations embedded in the form are functioning correctly. Then i will demonstrate how we can use a Web Service to provide the rate for the specific currencies selected.
  1. Create a new Blank Form in InfoPath 2010.
  2. Add the following controls to the form:
    • A text field called inputAmount, configure this as a Decimal (double).
    • A text field called outputAmount, configure this as a Decimal (double).
    • A button called convert 
You should end up with something similar to this:

2012-04-23-InfoPathCurrencyConverter-Part01-02.png

In the fields pane right click myFields and select Add, call this new field exchangeRate. For now give this a default value of 1.5.

2012-04-23-InfoPathCurrencyConverter-Part01-03.png

Select the convert button, and from the ribbon select Manage Rules. Click Add and then select set a fields value. Set the outputAmount field to the value of inputAmount x Rate.

2012-04-23-InfoPathCurrencyConverter-Part01-04.png

Preview the form and you should now have a functioning multiplication calculation, that is updated each time convert is pressed.

2012-04-23-InfoPathCurrencyConverter-Part01-05.png

Right click the outputAmount text field select Change Control and choose Calculated Field. This is so the user cannot edit the amount. We don’t want them tampering with the amount of money they are going to get paid!

Finally select the inputAmount text field, and add an Action rule, the rule is to set the outputAmount to be blank, so that the values are always correct.

2012-04-23-InfoPathCurrencyConverter-Part01-06.png

To recap, we have created a new form that has the components required for us to create a currency conversion component. We have a working calculation working from a button located on the form; however this is using a static rate.

Before Part 2, give your form a bit of jazzing up and you’ll end up with something like this:

2012-04-23-InfoPathCurrencyConverter-Part01-07.png


Part 2


In Part 1, we created a new form that has the components required for us to create a currency conversion component. We have a working calculation working from a button located on the form, however this is using a static rate.
At the end of Part 1 we ended up with a form that looked a little like this:
2012-04-24-InfoPathCurrencyConverter-Part02-01.png
We now are going to continue and achieve the following:
  • Connect to a Currency Conversion Web Service.
  • Provide drop down lists of available currencies.
  • Set the Exchange Rate relevant to our selected values.
After a bit of hunting I found a web service that would suit the needs of this project. It can be accessed using the following URL: http://www.webservicex.net/CurrencyConvertor.asmx
2012-04-24-InfoPathCurrencyConverter-Part02-02.png
From the description this does exactly what we need it to. Adding this in to InfoPath couldn’t be simpler. But as this web service is out on the web we need to change our form in to full trust mode. To do this click File -> Form Options and from the left select Security and Trust, from here you can uncheck Automatically determine security level and select Full Trust.
2012-04-24-InfoPathCurrencyConverter-Part02-03.png
Now we are ready to add our web service. On the Fields pane click Manage Data Connections. Click on Add and then follow the wizard as illustrated.
2012-04-24-InfoPathCurrencyConverter-Part02-04.png
Leave the defaults and click Next.
2012-04-24-InfoPathCurrencyConverter-Part02-05.png
Leave SOAP Web Service selected and click Next.
2012-04-24-InfoPathCurrencyConverter-Part02-06.png
Enter the URL of the web service into the location box and click Next.
2012-04-24-InfoPathCurrencyConverter-Part02-07.png
The web service, has only a single method ConversionRate select it and click Next.
2012-04-24-InfoPathCurrencyConverter-Part02-08.png
On this screen you are able to set default values, notice that when you do you get a great drop down list of valid items that will come in handy later. I’m from Australia so have set both values to AUD to begin with. Click Next, Next and finally Finish.
You have now successfully added the web service in to InfoPath. The next step will be to add those useful drop downs we just saw in to the form for the end user to utilise in selecting valid currencies.
In the Fields pane select the drop down that currently has Main selected; select the secondary data source of ConversionRate. Expand the structure, and inside queryFields you can see the two values we set earlier, FromCurrency and ToCurrency.
Simply drag and drop these on to the form, a menu will appear and you will want to select drop down box as the type of field to utilise. If you preview the form now your drop down lists should be functional.
2012-04-24-InfoPathCurrencyConverter-Part02-09.png
Brilliant, we have all the values the web service support so we should be safe from errors. Next open the properties of the exchangeRate field, set the default value to the ConversionRateResult as shown below. Ensure that Refresh value when formula is recalculated is checked.
2012-04-24-InfoPathCurrencyConverter-Part02-10.png
Finally we just need to edit the rules on our Convert button, select the Convert button and then select Manage Rules select the rule we created earlier, most likely with its default name Rule 1. Add a new action of type Query for Data. We only have one web service currently so by default the data connection selected is ConversionRate click OK. We need to ensure that we query for data before setting the outputAmount there for highlight the action that we just added and select Move Up.
2012-04-24-InfoPathCurrencyConverter-Part02-11.png
You should end up with the two actions like this.
2012-04-24-InfoPathCurrencyConverter-Part02-12.png
That’s It! Preview your form, enter a value and select your from and to currency and hit the convert button. We now have the value coming back that we expect.
2012-04-24-InfoPathCurrencyConverter-Part02-13.png
To recap, we created a new InfoPath form, laid out some controls with a couple of rules, connected a conversion rate web service and hooked it up all together to make a InfoPath currency conversion form. These components can now be used various ways in many other forms that would require currency conversion. 

No comments:

Post a Comment