Quantcast
Channel: Microsoft Dynamics 365 Community
Viewing all articles
Browse latest Browse all 10657

Creating flexible RESTful Services for Dynamics Part 4

$
0
0

This is a continuation from my previous post: [http://community.dynamics.com/ax/b/dynamicsax_wpfandnetinnovations/archive/2014/03/08/creating-flexible-restful-services-for-dynamics.aspx] where I introduce a flexible REST service that works with non-active directory sources.

 
In this article, I’ll focus on some on some client-side examples of the RESTful service and illustrate the rapid-application-development advantages for this type of framework. A video walkthrough of these client-side examples can be found here: [https://www.youtube.com/watch?v=c_jT0bDz4es&hd=1]
 

MS Office:

Both MS Word and Excel have an inbuilt simple VBA macro language with an internal IDE that allows you to develop simple customisations without the need for Visual Studio. In this example I’m going to construct a simple VBA function that will retrieve the current customer balance from Dynamics. I’ll then use this function on the worksheet as if it was one of the native Excel functions.
 
The following SYS layer method on the [CustTable] will return the balance in the required currency:
 
staticvoid Job1(Args _args)
{
    info(strFmt("%1",CustTable::find("00000065").balanceAllCurrency("GBP")));
}
 
 
We can invoke this functionality using our RESTful service by issuing the following HTTP GET request:
 
http://[webserver]/GenericBroker/Service.asmx/ProcessImmediate?requestXml=<brokerRequest><direction>Excel->L1</direction><serviceClass>DynamicsClass</serviceClass><invocationMethod>ExecuteScript</invocationMethod><data>return num2str(CustTable::find('0000000065').balanceAllCurrency('GBP'), 0, 2, 1, 0);</data><resultOnly>true</resultOnly></brokerRequest>
 
 
In order to make a HTTP request from VBA you need to create a “WinHttp.WinHttpRequest.5.1” object. The following simple function combines the creation of this object with the web-request and parameterises it so that the customer account code can be used from the worksheet:
 
Option Explicit
 
Public Function GetBalance(CustCode As String) As String
 
    ' create request object
    Dim MyRequest As Object
    Set MyRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
   
    ' send the request
    MyRequest.Open "GET", _ "http://[webserver]/GenericBroker/Service.asmx/ProcessImmediate?requestXml=<brokerRequest><direction>Excel->L1</direction><serviceClass>DynamicsClass</serviceClass><invocationMethod>ExecuteScript</invocationMethod><data>return num2str(CustTable::find('" & CustCode & "').balanceAllCurrency('GBP'), 0, 2, 1, 0);</data><resultOnly>true</resultOnly></brokerRequest>"
    MyRequest.Send
 
    ' handle the response
    GetBalance = MyRequest.ResponseText
 
End Function
 
Now in order to use the function on the worksheet all you need to do is pass in the cell reference that contains the customer account code e.g. “=GetBalance(A6)”
 
 

Javascript:

The same principle can be applied in javascript to make AJAX client-side calls to the REST service. The following rudimentary javascript function performs a synchronous AJAX call:
 
<scripttype="text/javascript">
    var xmlHttp = null;
    function httpGet(Url) {
        xmlHttp = new XMLHttpRequest();
        xmlHttp.open("GET", Url, false);
        xmlHttp.send();
        return xmlHttp.responseText;
    }
</script>
 
 
This then needs to be combined with a HTML data entry form to enter and receive the run-parameters, construct the right URL and then assign the result to a DIV element:
 
<body>
    <h2>Enter customer account</h2>
    <inputtype="text"id="AccountNum"/>
    <inputtype="button"id="GetBalance"value="GO"onclick="javascript: document.getElementById('Result').innerHTML = httpGet('http://vmssp-01/GenericBroker/Service.asmx/ProcessImmediate?requestXml=<brokerRequest><direction>JS->L1</direction><serviceClass>DynamicsClass</serviceClass><invocationMethod>ExecuteScript</invocationMethod><data>return num2str(CustTable::find(\'' + document.getElementById('AccountNum').value + '\').balanceAllCurrency(\'GBP\'), 0, 2, 1, 0);</data><resultOnly>true</resultOnly></brokerRequest>');"/>
    <br/>
    <br/>
    Balance:
    <h2><divid="Result"/></h2>
</body>
 
 
The results are as follows:
 
 

Summary

As mentioned, you can see a practical demo of the speed and ease-of-development for these kind of solutions using REST here: [https://www.youtube.com/watch?v=c_jT0bDz4es&hd=1]
 
So long as the client-application has the ability to make a HTTP call, then it has the ability to interact directly with the RESTful service and consequentially retrieve Dynamics data and invoke business-logic.
 
These examples, however, are just for demonstration to illustrate the flexibility and ease-of-use of a communication framework like this. Best practices dictate that you would never release this type of client-side inter-system data communication without the necessary security-safeguards. The real power of REST lies in “live” server-to-server data communication, especially in scenarios where the Requester is either outside of the trusted domain, or simply cannot communicate using the standard AIF protocols. Currently, applications like the Dynamics Connector only offer replication of data which fundamentally means “duplicating-data”. This can often be time-consuming and resource-intensive. In my next article, I’ll demonstrate how data can be harvested (synchronously) from Dynamics from a 3rd party server based systems using REST without the need to go down the route of asynchronous scheduled data replication.
 
 
REGARDS
 
 

Viewing all articles
Browse latest Browse all 10657

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>