Wednesday, May 2, 2012

InfoPath Currency Converter - Part 2


You may also be interested in: the Outlook sidebar by harmon.ie


Editor's note: Contributor Chris Grist is a Senior Technical Consultant for Loftus IT. Follow him @gristdog
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. If you got stuck I have provided the .xsn file below for you to use.
InfoPath Currency Conversion: InfoPath Currency Conversion (2)

No comments:

Post a Comment