Create a European VAT ID checker (also works for Zoho CRM!)

Create a European VAT ID checker (also works for Zoho CRM!)

For invoicing to other businesses within the EU, it is mandatory to check whether the VAT ID of your customer is correct, in order to invoice using a 0% tax rate. I wrote a script that checks the VAT number that you have from your customer against the EU database of VAT IDs. The easiest way to use this script is to put it in a function and call that function from a workflow or button.

The function is as follows:

  1. String checkVAT(String vat_id)
  2. {
  3.       if(vat_id == "" || vat_id == null)
  4.       {
  5.        return "Enter number in VAT first!";
  6.       }
  7.       else
  8.       {
  9.              vat_id = getAlphaNumeric(vat_id);
  10.              countryCode = vat_id.subString(0,2);
  11.       vatNumber = vat_id.remove(countryCode);
  12.       url = "http://ec.europa.eu/taxation_customs/vies/services/checkVatService";
  13.       //Uncomment next three lines for testing
  14.       // countryCode = "NL";
  15.       // vatNumber = "200";
  16.       // url = "http://ec.europa.eu/taxation_customs/vies/services/checkVatTestService";
  17.       request = "<Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\">    <Body>        <checkVat xmlns=\"urn:ec.europa.eu:taxud:vies:services:checkVat:types\">";
  18.       request = request + "<countryCode>" + countryCode + "</countryCode>";
  19.       request = request + "   <vatNumber>" + vatNumber + "</vatNumber>";
  20.       request = request + "    </checkVat>    </Body></Envelope>";
  21.               headers = Map();
  22.       headers.put("Content-Type","text/xml");
  23.       vat_data = invokeurl
  24.       [
  25.       url :url
  26.       type :POST
  27.       parameters:request
  28.                       headers:headers
  29.       ];
  30.       validity = vat_data.getsuffix("valid>");
  31.       validity = validity.getprefix("</");
  32.       if(validity == "true")
  33.       {
  34.       validity = "valid";
  35.       }
  36.       else
  37.       {
  38.       validity = "INVALID";
  39.       }
  40.       name = vat_data.getsuffix("name>");
  41.       name = name.getprefix("</");
  42.       address = vat_data.getsuffix("address>");
  43.       address = address.getprefix("</");
  44.               return "The VAT number is " + validity + if(validity=="valid",". The registered name is " + name + " and the registered address is " + address + ".","");
  45.       }
  46. }
Here's what the script does:
  1. It uses the VAT number that was included as parameter vat_id. This can be gathered for instance from a field in your form.
  2. It returns a string: this is either
    1. a message that the VAT number was not filled in (first IF statement)
    2. a message that the number is invalid (row 41)
    3. a message that the number is valid, including the registered name and address (the IF statement in row 41)
  3. The service needs the country code and VAT number separately. This is extracted in rows 9-11.
  4. The request to the service is prepared (row 12-20) and sent (row 21-26). If you want to test without an actual number, uncomment lines 14-16: it will use the test service.
  5. The returned message is checked for validity of the number (row 27-28) and the name and address are extracted (rows 37-40)
You can extend this script in whichever way you like. For instance, you could write a script that checks whether the name and address match with what is filled in.

    • Related Articles

    • Add a custom logo to an app

      Zoho Creator gives you a couple of options for the logo that is displayed in the top left corner of an app: Your company logo (the same for all your apps) An icon from the predefined icon list A 1-3 letter combination Now, you might have different ...
    • Sending images to imagekit.io using API

      imagekit.io is a service for optimising media content for websites. It's API is pretty straightforward, but it's always a bit finicky to get a connection going the way you want it. To make an API call, first follow the steps in "Authorisation" and ...
    • Creating a Function

      When you have a piece of scripting code that you use frequently, it can be better to create a function for this. Instead of copying this piece of code (and having to update each and every instance when you need to change it!), you have it in one ...
    • Limitations in CSS styling

      Styling forms in Zoho Creator using CSS styling in a Notes field brings many possibilities, but unfortunately not all CSS properties and functions work: some are simply stripped out. Whenever I come across a limitation, I add it to the list below, ...