Web Services Black Belt: consuming NAV web services using pure C/AL – 9/30, Navigate Into Success

MP900406779[1]Have you ever needed to connect to the Web services of one NAV instance from another one? If so, I bet that the approach was something like this: you created a .NET class where you defined a Web or Service reference to the target instance, and then you consumed that .NET class using .NET Framework interoperability. It was kind of clumsy, inflexible, but it worked.

How cool would it be if you could do something like this:

WITH WebService DO BEGIN
  CONNECT(‘http://localhost:7047/DynamicsNAV70/WS/CRONUS%20International%20Ltd/Page/Customer’);

  INIT;
  SETVALUE(‘Name’,'Test Customer’);
  SETVALUE(‘Blocked’,Cust.Blocked::Ship);
  SETVALUE(‘Credit_Limit_LCY’,10000);
  CREATE;

  MESSAGE(‘I just created Customer No. %1 in another NAV instance.’,GETVALUE(‘No’));
END;

As a matter of fact, you can write something like that. You can write exactly that. And it compiles, runs, and accomplishes exactly what you expect it to do. The most beautiful thing, you don’t need to write a single line of code in Visual Studio, or deploy any external dependencies – it uses pure C/AL, and works equally well in NAV 2009 and NAV 2013.

The only thing you need is a simple codeunit that you can download from Mibuso. (The upload is pending approval at the time of posting of this blog, and I’ll update it as soon as the download becomes available.)

I wrote that codeunit as a part of the demo I presented last Wednesday at Mibuso NAV TechDays 2012 in Antwerp, and as promised – I am making the code available for you to use.

This simple codeunit does no magic, it simply harnesses the power of the features built into the .NET Framework. It builds the proxy class and compiles it on the fly, and then uses reflection to instantiate objects, set properties, and call methods to allow you to interact with any NAV page web service.

Before you can consume a NAV page Web service from C/AL you do not need to know anything about the service, except for its URL. If it’s a page web service, you can use it to read, create, update, and delete data in another NAV instance, simply using C/AL.

At this stage, it supports the following page web service functions:

  • Read
  • ReadMultiple
  • Create
  • CreateMultiple
  • Update
  • UpdateMultiple
  • Delete

Right now, I am providing no documentation for it, but I believe it should not be difficult to figure out what it can do by following these couple of examples.

Creating a customer

That’s the example above. Just declare a variable named WebService of type Codeunit 50113, and you are good to go.

Iterating through a set of customers read, with a filter applied

WITH WebService DO BEGIN
  CONNECT(‘http://localhost:7047/DynamicsNAV70/WS/CRONUS%20International%20Ltd/Page/Customer’);
  SETFILTER(‘Balance_LCY’,'>0′);
  SETFILTER(‘Name’,'A*’);
  IF READMULTIPLE THEN
    REPEAT
      MESSAGE(‘Customer %1 %2 has balance of %3′,GETVALUE(‘No’),GETVALUE(‘Name’),GETVALUE(‘Balance_LCY’));
    UNTIL NEXT = 0;
END;
EXIT;

Updating an item

WITH WebService DO BEGIN
  INIT;
  SETVALUE(‘No’,’1000’);
  READ;
  SETVALUE(‘Description’,’Bicycle 2’);
  UPDATE;
END;

Creating a purchase order from a sales order

WITH WebService DO BEGIN
  CONNECT(‘http://localhost:7047/DynamicsNAV70/WS/CRONUS%20International%20Ltd/Page/PurchOrder’);
  INIT;
  SETVALUE(‘Buy_from_Vendor_No’,Rec."Sell-to Customer No.");
  SalesLine.SETRANGE("Document Type",Rec."Document Type");
  SalesLine.SETRANGE("Document No.",Rec."No.");
  IF SalesLine.FINDSET THEN
    REPEAT
      NEWLINE;
      SETLINEVALUE(‘Type’,FORMAT(SalesLine.Type));
      SETLINEVALUE(‘No’,FORMAT(SalesLine."No."));
      SETLINEVALUE(‘Quantity’,SalesLine.Quantity);
    UNTIL SalesLine.NEXT = 0;
  CREATE;
  MESSAGE(‘Purchase Order No. %1 is created in the vendor”s system.’,GETVALUE(‘No’));
END;

I’ll be updating the functionality of this codeunit and uploading a more comprehensive version, as I find time to do this.

This post is a series of posts where I’ll present most of the stuff I talked about at NAV TechDays 2012, and expand those topics into the areas that I either had to leave out due to time constraints, or that I thought were not as interesting as those that I chose to put into the presentation.

Please, let me know how you like this small gadget.


Read this post at its original location at http://navigateintosuccess.com/blog/web-services-black-belt-consuming-nav-web-services-using-pure-cal, or visit the original blog at http://NavigateIntoSuccess.com. 47fc2f5642456dfd71e5581a3785c625..

Continue reading on Source Blog

Powered by WPeMatico