Part Six Integrate Ecommerce and Dynamics AX 2012 - Ordering

Wednesday, March 17, 2021

Importing Sales Orders using AIF Webservices

The solution for creating orders is going to be a super simpler >NET solution. Instead of using Power Automate again I will create a simple .NET console app which can run on your internal servers. This will ensure it does not have any issues connecting to Dynamics AX.

If you want to run this program you could set it up under windows scheduler and call it on a scheduled basis.

To complete the solution, I will do the following.

  1. Check the AIF Inbounds Ports in Dynamics for the Sales Order Service
  2. Create a project called Ecommerce Orders
  3. Add a Service Reference to the AIF service in Dynamics
  4. Add a class called orders which I will add the AIF code to
  5. Create two simple views in NOPCommerce to return orders and orderlines
  6. Add a simple Data Entity Model called Shopping Cart
  7. That’s all folks  

 

Check the AIF Inbounds Ports in Dynamics for the Sales Order Service

Most of the AX installations have a sales order service enabled by default. I’m going to create one any, it’s simple just create a new Inbound Port called create sales , select NetTcp as the adapter.

Then in the service operations select the following service.

Then activate the service, this is now ready to go.

Once you activate the service you now have a URL to add a service reference in your .NET project

 

Create a project called Ecommerce Orders

Create new project in Visual Studio, select a console application and call it Ecommerce Orders

Add a Service Reference to the AIF service in Dynamics

Now let’s add a service reference, paste in the URL of the inbound port which we have set up in Dynamics.

Create two simple views in NOPCommerce to return orders and orderline

Next two simple views will be created on the Ecommerce side. Why views ? Rather than pulling from lots of different tables. The views give me what I am looking for.

The EcommerceOrders View will pull from the Orders Table and combine this information with the customer and the customer address tables. Below is the SQL For this view.

CREATE VIEW [dbo].[EcommerceOrders]

AS

SELECT ORD.id 'OrderId', ORD.CreatedOnUtc OrderDate, getdate() DateRequired, ADDR.FirstName + ' ' + ADDR.LastName CustomerName

       , ADDR.Address1

       , ADDR.Address2

       , ADDR.City Address3

       FROM [Order] ORD

       JOIN [dbo].[OrderItem] ITM

       ON ITM.OrderID = ORD.ID

       JOIN Customer CUS

       ON CUS.ID = ORD.CustomerID

       JOIN Address ADDR

       ON ORD.ShippingAddressId = ADDR.ID

       Where ORD.OrderImported is null

GO

 

The EcommerceOrderline View will pull from the OrdersLines Table and combine this information with the product tables. Below is the SQL For this view.

 

CREATE VIEW [dbo].[EcommerceOrdersItems]

AS

SELECT ORD.id 'OrderId'

       , ManufacturerPartNumber ProductCode

       , PRD.Name ProductDescription

       , UnitPriceInclTax SellingPrice

       , Quantity

       FROM [Order] ORD

       JOIN [dbo].[OrderItem] ITM

       ON ITM.OrderID = ORD.ID

       JOIN Customer CUS

       ON CUS.ID = ORD.CustomerID

       JOIN Address ADDR

       ON ORD.ShippingAddressId = ADDR.ID

       JOIN Product PRD

       ON ITM.ProductId = PRD.ID

       --Where ORD.OrderImported is null

GO

 

 

Add a simple Data Entity Model called Shopping Cart

We need to access the order and the order lines from the Ecommerce site. The simple way to do this is add a DataEntityModel to the Project. Connect to the Ecommerce database and add the two tables. Called

Add the Model choose EF Designer , next dialog I am skipping but it’s here you set your connection string.

Then add the new views which we have just created. Job done we can now access data

 

Add a class called orders which I will add the AIF code to

Add a class called orders. Create blank method called Import Orders Once the class is created, make a reference to it and call that method.

Next two more methods.

First method Import Orders loops through the Ecommerce Order View and returns the orders.

Second method Import Orders loops through the Ecommerce Order line View and calls the AIF Service

First section creates the sales order header

Second section creates the lines and calls the service to create the order

We are now ready to run and compile the project and start importing orders. I 

That’s all folks  

Hope the articles were useful. This is a simple example and is not a complete solution. I’ve used an existing customer in Dynamics to create the order against. This could be useful in a real-world example if you created a customer called generic Ecommerce Customer. To make this work the code would also the code would need to be enhanced to populated the delivery address.  

Other enhancements needed would be to pass product dimensions like colour or style or size to the order service. Also, attributes could be used on the Ecommerce site to better describe the product.