Visma.net with Grails

The e-commerce business is growing very fast, and many companies are looking for solutions that can automate processes, manage complex financial operations and remove repetitive accounting operations. There are many tools available on the market and among the very attractive ones there is “Visma.net Financials”. It is a financial management solution that offers flexible configuration and supports global organizations with multinational financial reporting requirements. For more information about “Visma.net Financials” please check https://www.visma.com/cloud-erp/financials-system. In this article let’s look at how to get the needed data from Visma.

As a backend technology I will use the latest version of Grails framework (which is 3.3.5 at the time of writing this article) and for making HTTP requests I will work with modern Groovy DSL “HttpBuilder-NG”. You can find more information on the Grails framework following the link https://grails.org/.

First, we need include the proper library in our project to create HTTP requests. Grails uses Gradle as the default builder, so just add one line for the dependencies in the build.gradle file.

compile "io.github.http-builder-ng:http-builder-ng-core:1.0.3"

Also, I want to draw your attention to the credentials. In this case company ID and access token are required.

Then we need a service that will collect all the actions to get and put data. This should be something multipurpose because we want to reuse it with various API requests. To check all available APIs please visit https://integration.visma.net/API-index/#. There are a lot of them, but today we will work with two: inventory and attachment. As in this article we aim only to get the data, let’s see for example how to retrieve the list of products your company is selling.

def getFromVisma(String target, def params = [:]) {

   String requestUri = "https://integration.visma.net"

   String requestPath= "/API/controller/api/v1"

   def vismaHeaders = [

           'Accept': "application/json",

           'ipp-company-id': "your company id"

           'ipp-application-type': "Visma.net Financials",

           'Authorization': "your own token"

   ]

   def http = configure {

       request.uri = requestUri

       request.uri.path = requestPath + target

       request.uri.query = params

       request.headers = vismaHeaders

   }

   return http.get {

       response.exception { ex ->

           println ex

       }

   }

}

Controller action for calling service method can look like this:

class AnyController {

    VismaConnectService vismaConnectService

    def getFromVisma() {

        String target = '/inventory'

        def vismaData = vismaConnectService.getFromVisma(target)

        vismaData.each { data ->

            if (data.status == "Active") {
                
                // do something //

            }

        }

    }

}

Our service method has two parameters: the first is the “target”. When requesting the inventory items, for example, the target should be “inventory”. The second one is the “map”, which by default is empty. Generally, in the parameters you can specify all the service fields that Visma offers such as “lastModifiedDateTime”. Since we wrote in the header “application / json”, the result is a list with JSON objects, and it looks like this:

{

        "inventoryId": 00001,

        "inventoryNumber": "00001",

        "status": "Active/Inactive",

        "type": "",

        "description": "",

        "defaultPrice": 0,

        "pendingCost": 0,

        "currentCost": 0,

        "lastCost": 0,

        "lastModifiedDateTime": "2018-01-01T00:00:00.00",

        "attachments": [

            {

                "name": "attachmentName",

                "id": "attachmentId",

                "revision": 1

            }

        ],

        "attributes": [ ],

}

The above is the example only and in your case you can have more or fewer fields. In this example I marked attachmentName and attachmentId. We need these parameters to receive attachments. Let’s look at the code below:

def getAttachment(String attachmentName, String attachmentId) {

   String requestUri = "https://integration.visma.net"

   String requestPath= "/API/controller/api/v1/attachment"

   def vismaHeaders = [

           'ipp-company-id': "your company id",

           'ipp-application-type': "Visma.net Financials",

           'Authorization': "Bearer your own token"

   ]

   def http = configure {

       request.uri = requestUri

       request.uri.path = requestPath + attachmentId

       request.headers = vismaHeaders

   }

   return http.get {

       Download.toFile(delegate, new File("./attachments/${attachmentName}"))

       response.exception { ex ->

           println ex

       }

   }

}

The last piece of code is very similar to the first one. The main difference is that as a result we will have a File not JSON. Also, instead a File we can get a TempFile or a Stream. It depends on your case and your requirements.

Conclusion

The examples considered above are a tip of the iceberg as Visma offers much richer functionality. Still, the above-shown skeleton can be used to create complex solutions for various business tasks. Also, as shown above, the power of Groovy combined with the flexibility of Grails with its numerous plugins will help you work with the data fast and efficient.

Origin: www.linkedin.com

All articles