Some time ago, I did a session at Direction EMEA in Berlin. Apparently, some people were inspired with this. Not only did I introduce "cannibalism" (never expected this to be something "hot, new and inspiring" but rather something "obvious" - let's spend a blogpost on that as well.. :) ), but also I showed an example of consuming a third party web service. And some time ago (quite some time, actually), I promised one of the attendees to blog about the VAT example I showed there.
In fact, you can see this as "consuming a third party web service". There are tons of those out there .. and this example is quite useful, if you ask me (more useful than getting the weather info, or a list of songs of a certain genre, is it.. ;°) ).
What Web Service?
A functional consultant pointed me to a certain official webservice that had some promising capabilities. This is the website that uses that same webservice:
As you can see, you can validate the VAT Number in quite some European countries. This is something customers definitely want .. so what if we could build such a functionality in NAV. We would need a web service, right? Well, here it is, just for free:
Basically, using this web service, is kind of the same way as using any web service - so also quite the same as I described in this blogpost: NAV 2009 R2 .Net Interop: Calling a Web Service (come to think of it - I can make this blog quite short and let's share the objects I created here).
Building the Proxy
This will take the majority of your time - at least 2 minutes (because Visual Studio doesn't load very fast). I'm not going to create a service reference, but a web reference.
Now, right click "References" and click "Service Reference":
As I mentioned, I'm going to use a web reference, so let's click Advanced / Add Web Reference and provide the url I mentioned above. Give it a proper name (I used "VATValidationService") and click "Add Reference":
That's it, people! Build you're solution and get the hell out of Visual Studio!
You're safe now .. you're almost back in you familiar habitat (we like to call C/AL). Just first copy the dll (which is the proxy class you'll need in NAV) to your classic client and server:
(in my case, the server is running on my laptop, but obviously, you should put the dll wherever it will be used - NST, failover, ...). In this case, my NST will be executing the business logic, so I don't have to put it client-side.
Let's go to nav and write some very simple code there - it's not functional in any way, it's just a test-codeunit that uses the service. You can build the business logic any way you like.
First declare some variables:
'VATValidation, Version=126.96.36.199, Culture=neutral, PublicKeyToken=null'.VATValidation.VATValidationService.checkVatService
Then, OnRun, I wrote these lines of code:
CheckVATService := CheckVATService.checkVatService(); //the constructorCountryCode := 'BE';VATNumber := '0885815777';//VATNumber := '0863844386';//VATNumber := '0891609053';CheckVATService.checkVat(CountryCode,VATNumber,Success,Name,Address);IF Success THENMESSAGE(Name + '\' + Address)ELSEMESSAGE('No Success');
I agree, the business logic is not useable at all .. but that was not the intention. I just provided you the way to use the CheckVat function .. and now it's up to you.
The download contains my test-objects from my demo-db. Take them "as is" .. so if you want to implement this at a customer, do that on your own risk - and reconsider the code (make it "partner ready" :-) ).
The download contains:
That might explain my own problems sometimes when I'm unable to connect to the service.. :-).
the weakeness of the system is that each country has responsiblity to have servers up. Some countries are better than others...
eg. my own country, Denmark, have an instable server sadly.
thanks for mentioning that!
Good article, just one point:
We use this Web Service (not in Navision) for quite a time now.
If you can't reach the web service for some reason the result is not valid - although the VAT No. might be correct. You will definetly have to deal with that.