Share via


Using Axapta Business Connector to Create Microsoft Business Solutions–Axapta Sales Orders

 

Bill Thompson, Axapta Developer Support
Microsoft Business Solutions

March 2006

Applies to:
   Microsoft®Business Solutions–Axapta® 3.0, now part of Microsoft Dynamics™
   Microsoft® Visual Studio® 2005
   Microsoft® Visual Basic® 2005

Summary: Find out how to use Microsoft Axapta Business Connector, custom Microsoft Axapta X++ code, and Visual Basic 2005 to create sales orders within Microsoft Business Solutions–Axapta 3.0. The sales order will consist of one item.

Contents

Introduction
Determining Required Axapta Objects
Visual Basic 2005 Application
   Building the Form
   Making the Connection
   Validating the Customer Number
   Validating the Item Number
   Creating the Sales Order
Where to Go from Here
Conclusion

Introduction

Learn how to use Visual Basic 2005 to develop a Windows-based application in Visual Studio 2005 that will create sales order entries within Microsoft Business Solutions–Axapta. This article differs from my previous Microsoft Business Solutions–Axapta articles in that there is NO wrapper code written within Microsoft Business Solutions–Axapta for data manipulation. All data is entered into Microsoft Business Solutions–Axapta by using objects that are contained within the Visual Basic application. You will see that, because of the way this application is created, the transferring of Windows application logic to a Web-based application is likely to be an uncomplicated exercise.

Readers should be familiar with Microsoft Business Solutions–Axapta, Visual Studio 2005, and Visual Basic 2005.

When you complete all the steps described in this paper, your finished application should resemble the Create Sales Order in the following figure.

Figure 1. Finished Windows application

Determining Required Axapta Objects

The first step in the project is to determine which Axapta objects are needed to perform the required operations. By examining Axapta processes, we can determine that the classes AxSalesTable and AxSalesLine are to be used.

  • AxSalesTable will create the header information.
  • AxSalesLine will create the line information.

Visual Basic 2005 Application

The first step in creating this application is to create a new project within Visual Studio 2005. Call the project "AxSalesDemo." After you create the project, you must create a reference to the Axapta Business Connector in the project. In Visual Studio, navigate to the Project menu, click "Add Reference," and then click the "COM" tab.

Locate the Business Connector by finding Axapta COM Connector 1.2 Type Library in the list. Highlight this item, and then click "Select." After the component appears in the Selected Components list, click "OK."

Building the Form

On the form, create the following controls, by dragging them from the Toolbox, and changing the properties of each control to match those in the following table:

Element Name Default Text Description
Text box txCustNo Blank Contains the customer number that is to be billed for the Sales Order
Button Button1 Validate Contains code that tests to validate the customer number.
Text box txDelName Blank Populated with the Delivery Name of the customer when the Validate button is clicked.
Text box txDelStreet Blank Populated with the Deliver Street of the customer when the Validate button is clicked.
Text box txDelCity Blank Populated with the Deliver City of the customer when the Validate button is clicked.
Text box txDelState Blank Populated with the Deliver State of the customer when the Validate button is clicked.
Text box txDelZip Blank Populated with the Deliver Zip Code of the customer when the Validate button is clicked.
Text box txItemId Blank Contains the ID of the item that will be used to populate the sales order line.
Button btValidItem Get Info Used to verify that the item number is for a valid item.
Text box Name Blank Populated with the item name when the Get Info button is clicked.
Text box txQty Blank Populated with the quantity when the Get Info button is clicked.
Text box txUnit Blank Populated with the unit description of the item when the Get Info button is clicked.
Text box txPrice Blank Populated with the item price when the Get Info button is clicked.
Button Button2 Create Sales Order Initiates Sales Order creation within Microsoft Business Solutions–Axapta.
Text box txResults Blank Contains the order number created within Microsoft Business Solutions–Axapta.

Making the Connection

The first step is to effect recognition of the business connector object within the code portion of the Windows application. To do so, make the following declaration:

Dim Axapta As AxaptaCOMConnector.Axapta

This allows the application to use the Axapta variable to refer to the business connector objects.

Validating the Customer Number

The first step in the process is to validate the customer number. This is done by the button1 Click method. This is the code for that method:

Dim custTable As AxaptaCOMConnector.IAxaptaRecord

Try
  Axapta = New AxaptaCOMConnector.Axapta
  Axapta.Logon("Admin", "", "", "SP4")
  custTable = Axapta.CallStaticRecordMethod("CustTable", "find", Me.txCustNo.Text)
  If (custTable.field("Name") <> "") Then
    txDelName.Text = custTable.field("Name").ToString
    txDelStreet.Text = custTable.field("Street").ToString
    txDelCity.Text = custTable.field("City").ToString
    txDelState.Text = custTable.field("State").ToString
    txDelZip.Text = custTable.field("ZipCode").ToString
    enableItem()
  End If
  Axapta.Logoff()
Catch ex As System.Exception
  MsgBox("Error:" + ex.ToString, MsgBoxStyle.Exclamation, "Application Error")
End Try

The first line of this method declares a variable that is actually an Axapta record. This will be used to store information that is retrieved from Axapta.

Dim custTable As AxaptaCOMConnector.IAxaptaRecord

The next line instantiates the Microsoft Business Solutions–Axapta business connector.

Axapta = New AxaptaCOMConnector.Axapta

The third line is the call to log on to Microsoft Business Solutions–Axapta. The first parameter designates the logon of the user, and the last parameter specifies the configuration to use.

Axapta.Logon("Admin", "", "", "SP4")

Note   More information on the Axapta.logon can be found within the Axapta Developers Guide (found within Microsoft Business Solutions–Axapta by navigating to Help from the main menu and clicking **"**Axapta Developers Guide") and then using the search parameter IAxapta.Logon.

The next line attempts to locate the customer information within Microsoft Business Solutions–Axapta by calling the CustTable static method find. The customer number is passed as a parameter to this method.

custTable = Axapta.CallStaticRecordMethod("CustTable", "find", Me.txCustNo.Text)

The rest of the logic will populate the fields on the form with the name and address of the company, if found within Microsoft Business Solutions–Axapta. Once this is done, the Axapta.logoff() method is called to close the connection to Microsoft Business Solutions–Axapta.

A Try — Catch loop is used to trap any errors that are thrown by Microsoft Business Solutions–Axapta, or Visual Basic 2005.

Validating the Item Number

The next step is to validate the item number. The logic is very similar to the customer number validation done previously. This is done by using the following code in the Click method of the button btValidItem.

Dim inventTable As AxaptaCOMConnector.IAxaptaRecord
Dim inventTableModule As AxaptaCOMConnector.IAxaptaRecord

Try
  Axapta = New AxaptaCOMConnector.Axapta
  Axapta.Logon("Admin", "", "", "SP4")
  inventTable = Axapta.CallStaticRecordMethod("InventTable", "find",_ txItemId.Text)
  If (inventTable.field("ItemId") <> "") Then
    enableItemInfo()
    txItemName.Text = inventTable.field("ItemName").ToString
    txQty.Text = "1.0"
    ' last parameter in find call for inventTableModule should be a 2 
    ' which represents ModuleInventPurchSales::Sales
    inventTableModule = Axapta.CallStaticRecordMethod(
_"InventTableModule","find", txItemId.Text, "2")
    If (inventTableModule.field("ItemId") <> "") Then
      txPrice.Text = inventTableModule.field("Price").ToString
      txUnit.Text = inventTableModule.field("UnitId").ToString
    End If

  End If
  Axapta.Logoff()
  Me.Button2.Enabled = True
Catch ex As System.Exception
  MsgBox("Error:" + ex.ToString, MsgBoxStyle.Exclamation, "Application Error")
End Try

The only real difference between this code and the previous one is the added step of using the InventTableModule table to retrieve the item price. Otherwise, the logic holds the same as that for the customer validation. The InventTable find() method is called to determine if the item exists. If the item is found, the rest of the parameters are populated, and the price is determined.

Creating the Sales Order

The order creation is done by the Click method of Button2. Here is the code:

Dim axCustAccount As String
Dim axDeliveryName As String
Dim axDeliveryStreet As String
Dim axDeliveryCountry As String
Dim axDeliveryCounty As String
Dim axDeliveryState As String
Dim axDeliveryCity As String
Dim axDeliveryZipCode As String
Dim axSave As Boolean
Dim newSalesID As String

Dim axSalesId_Line As String
Dim axItemID_Line As String
Dim axSalesQty_Line As String
Dim axSave_Line As Boolean

Dim axSalesTable As AxaptaCOMConnector.IAxaptaObject
Dim axSalesLine As AxaptaCOMConnector.IAxaptaObject

Try
  Axapta = New AxaptaCOMConnector.Axapta
  Axapta.Logon("Admin", "", "", "SP4")
  axSalesTable = Axapta.CreateObject("AxSalesTable")
  axCustAccount = axSalesTable.Call("custAccount", Me.txCustNo.Text)
  axDeliveryName = axSalesTable.Call("deliveryName", Me.txDelName.Text)
  axDeliveryStreet = axSalesTable.Call("deliveryStreet", Me.txDelStreet.Text)
  axDeliveryCountry = axSalesTable.Call("deliveryStreet", "")
  axDeliveryCounty = axSalesTable.Call("deliveryStreet", "")
  axDeliveryState = axSalesTable.Call("deliveryStreet", Me.txDelState.Text)
  axDeliveryCity = axSalesTable.Call("deliveryStreet", Me.txDelCity.Text)
  axDeliveryZipCode = axSalesTable.Call("deliveryStreet", Me.txDelZip.Text)
  axSave = axSalesTable.Call("save")
  newSalesID = axSalesTable.Call("salesID")

  axSalesLine = Axapta.CreateObject("AxSalesLine")
  axSalesId_Line = axSalesLine.Call("salesID", newSalesID)
  axItemID_Line = axSalesLine.Call("itemID", Me.txItemId.Text)
  axSalesQty_Line = axSalesLine.Call("salesQty", Me.txQty.Text)
  axSave_Line = axSalesLine.Call("save")
  txResults.Text = newSalesID + " has been created within Axapta."
  Axapta.Logoff()
Catch ex As Exception
  MsgBox(ex.ToString, MsgBoxStyle.Exclamation, "Error")
End Try

By examining the preceding code, the reader can see that many variables are defined. These variables actually are used to catch the information returned by Microsoft Business Solutions–Axapta existing methods. This returned information shows that the method successfully populated the designated field.

So, when analyzing the code, you will see that the following lines again instantiate the Axapta COM object, and log on to Axapta:

Axapta = New AxaptaCOMConnector.Axapta
Axapta.Logon("Admin", "", "", "SP4")

After the connection is made, the following code creates the Microsoft Business Solutions–Axapta object AxSalesTable, and calls methods that are used to populate and save the header information:

axSalesTable = Axapta.CreateObject("AxSalesTable")
axCustAccount = axSalesTable.Call("custAccount", Me.txCustNo.Text)
axDeliveryName = axSalesTable.Call("deliveryName", Me.txDelName.Text)
xDeliveryStreet = axSalesTable.Call("deliveryStreet", Me.txDelStreet.Text)
axDeliveryCountry = axSalesTable.Call("deliveryStreet", "")
axDeliveryCounty = axSalesTable.Call("deliveryStreet", "")
axDeliveryState = axSalesTable.Call("deliveryStreet", Me.txDelState.Text)
axDeliveryCity = axSalesTable.Call("deliveryStreet", Me.txDelCity.Text)
axDeliveryZipCode = axSalesTable.Call("deliveryStreet", Me.txDelZip.Text)
axSave = axSalesTable.Call("save")

After the information is saved, salesID is created by using the following call:

newSalesID = axSalesTable.Call("salesID")

The line information is populated, the sales line information is saved, and the connection to Microsoft Business Solutions–Axapta is closed. Here is the code:

axSalesLine = Axapta.CreateObject("AxSalesLine")
axSalesId_Line = axSalesLine.Call("salesID", newSalesID)
axItemID_Line = axSalesLine.Call("itemID", Me.txItemId.Text)
axSalesQty_Line = axSalesLine.Call("salesQty", Me.txQty.Text)
axSave_Line = axSalesLine.Call("save")
txResults.Text = newSalesID + " has been created within Axapta."
Axapta.Logoff()

Finally, the following is the code for two other subroutines that were called by the preceding code:

Private Sub enableItem()
  ' enable the next section
  txItemId.Enabled = True
  btValidItem.Enabled = True
End Sub
Private Sub enableItemInfo()
  txItemName.Enabled = True
  txQty.Enabled = True
  txUnit.Enabled = True
  txPrice.Enabled = True
End Sub

At this point, the Sales Order should exist within Microsoft Business Solutions–Axapta. The information in the text box txResults will display the Sales Order ID number.

Where to Go from Here

This paper has introduced you to the concept of creating sales orders within Microsoft Business Solutions–Axapta. The methodology that is used to create this Windows application is readily transferable to creation of Web applications. The potential is virtually unlimited.

As a caution, we need to remind you that virtually no error checking was done within this application, and only very rudimentary error trapping. To make this a truly usable application, you need to greatly expand both the error checking and error trapping routines. Our purpose here was just to show you, as simply as possible, how to use an external application to create a Windows application that can create sales orders within Microsoft Business Solutions–Axapta.

As you can see, at this point, there is no posting of this Sales Order. This means that an invoice is not generated. That may be the next logical step in working with this application.

Conclusion

Integration into Axapta can take many different forms, and can be accomplished by many different methods. This paper briefly demonstrated concepts that are viable options between both Windows and Web applications. The most critical step during the integration is determining which objects in Microsoft Business Solutions–Axapta are required to perform the operations that you want to implement. When that is done, the rest of the steps are straightforward code generation and form design.

This paper also demonstrates how to directly access Microsoft Business Solutions–Axapta objects without writing any "wrapper" code within Microsoft Business Solutions–Axapta.