giovedì 25 maggio 2017

Web Services Black Belt: consuming NAV web services using pure C/AL

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:
  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. If you missed the link in the previous sentence, then click here.
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;
Thanks to Vjeko

mercoledì 25 gennaio 2017

How To Kill A Windows Service Which Is Stuck At Stopping

Sometimes as an administrator you may need to kill a service which is stuck at stopping in order to avoid having to reboot a server in the middle of the day.
Here’s what you need to do:

Step 1. Find out the Service Name

To do this, go in to services and double click on the service which has stuck.  Make a note of the “Service Name”.

Step 2. Find out the PID of the service

Open an elevated command prompt and type in:
sc queryex servicename
(where servicename is the name of the service you obtained from Step 1.)
Make note of the PID

Step 3. Kill the PID

From the same command prompt type in:
taskkill /f /pid [PID]
Where [PID] is the service number.
If it is successful you should receive the following message:
SUCCESS: The process with PID XXXX has been terminated.
Be careful of what you are killing though.  If you kill a critical windows service you may end up forcing the machine to reboot on it own.
Note: By forcing a service to stop you can also use these instructions to Kill a Windows Service which is stuck at starting as well.  This will allow you to restart the service.

giovedì 12 gennaio 2017

NAV 2013+: Server Sizing

With the number of new installations we perform, we get asked a lot about what we recommend for the server hardware to run Dynamics NAV well.  If you’ve ever looked at the Microsoft recommended specifications you most likely quickly realized that they are the bare minimum requirements and really don’t apply to 99%+ of all installations. While we have a few “go to” hardware specs that we tend to go with, we have compiled the below guidelines that can get you in the rough ballpark of what should be scoped for any given installation.
Remember these are just baseline guides, there is no substitute for knowing how you utilize the system and what workloads will be run that you should factor in when choosing your specifications. Also, we highly recommend investigating using Azure to host your Dynamics NAV environment versus rolling your own on premise installation.  Implementing in Azure allows for great flexibility in regards to sizing the hardware.  You don’t have to nail the perfect environment in Azure since it is a simple process to dynamically re-size any VM or Azure SQL Database on the fly if you determine you need more resources.  If you do roll your own, be sure to factor in projected future growth and knowing what other loads will be running on a host machine when virtualizing your hardware.

NAV 2016: Connecting web services via PHP

Below is a Zip file that contains Example Code for working with Dynamics NAV Web Services in PHP.

Prerequisites

In order to make Dynamics NAV Web Services work with PHP you need to make sure that SOAP and CURL extensions are installed and enabled in php.ini.
You can check this by running the info.php (included in the Zip file above) and checking the following entries:

NAV 2016: How to use the Force

According to Star Wars:
“The Force is what gives a Jedi his power. It’s an energy field created by all living things. It surrounds us and penetrates us. It binds the galaxy together.” – Obi Wan Kenobiwithyou
According to Microsoft the Force is a Sync-NAVTenant cmdlet Schema Synchronization Option
“Table definition changes are applied to the business data table schema without validation. For destructive changes, data in columns of the business data table that are affected by changes will be deleted.
This option ignores any table synchronization instructions for the table in upgrade code units. You should use this option only when you are sure that there is no risk of unwanted data loss.”

With Dynamics NAV 2016 when you modify a table you must perform Database Schema Synchronization using the following options:

martedì 10 gennaio 2017

NAV 2016: Item Ledger Entry performance

Have you ever faced very bad perfomances opening a page on Item Ledger Entries, even if filtered on a single item?
Well, we faced it after an upgrade from NAV2009 classic client to NAV2016 CU4.

We tried tons of tuning before taking note that.. there was a record with entry number "0".
After deletion of this record, the page opens like a flash!

NAV 2016: How to crash a servicetier with a simple flowfield

It's the first time I encounter this issue and I'm happy to share it with you.
What happen if you run a page with a flowfield who point on another table referring itself?
Take a look on the below example:

if you try to replicate it creating the field 50001 on Customer table you will see your servicetier falling down.

4 hours spent trying to understand why a page can cause a similar damage on servicetier...