Welcome to MSDN Blogs Sign in | Join | Help

Code Samples for basic operations performed by Snap-ins for Microsoft Dynamics Ax

This post provides a few C# code snippets to demonstrate the programming methodology used within Snap-ins for Microsoft Dynamics Ax in performing the following basic functions:

 

·        Login to Microsoft Dynamics Ax

·        Retrieve table data from Microsoft Dynamics Ax

·        Create data records in Microsoft Dynamics Ax:

·        Update data in Microsoft Dynamics Ax

·        Delete data in Microsoft Dynamics Ax

 

Note: All the functions described below need AxaptaComConnector 1.2 Library as their reference and the following statements should be included in the beginning of the code:

using AxaptaCOMConnector;

using Microsoft.Win32;

 

Login to Microsoft Dynamics Ax:

 

There are two ways of Login supported by the Microsoft Dynamics Ax COM Connector:

 

1. Using the Logon2 method of Axapta2Class

 

This method mainly expects a username, password, language, Server Manager, Object Server and Configuration details pertaining to the Microsoft Dynamics Ax Server among other things. These can be obtained from the HKEY_CurrentUser registry key that stores the required information when Microsoft Dynamics Ax is installed. Given below is a sample code for a class called CAxConnection containing a method Login that accepts a username and password and in turn makes a call to Logon2:

 

/// <summary>

       /// Provides a connection to Axapta System

       /// </summary>

       public class CAxConnection

       {

              private Axapta2 axConn = new Axapta2Class();;

 

              /// <summary>

              /// Login to the Axapta system using user name and password

              /// </summary>

              /// <param name="userName"></param>

              /// <param name="Password"></param>

              public void Login(string userName, string passwd)

              {

                     object company = null;

                     object lang = null;

                     object servManager = null;

                     object objServ = null;

                     object config = null;

RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\Navision\\Axapta\\3.0");

                     if(key != null)

                     {

                           config = key.GetValue("Current");

                           RegistryKey configKey = key.OpenSubKey(

                                   config.ToString());                         

                           if(configKey != null)

                           {

                                  company = configKey.GetValue("company");

                                  lang = configKey.GetValue("language");

                                  objServ = configKey.GetValue("servermask");

                                  servManager = configKey.GetValue("internet");

                            }

                           axConn.Logon2((object)userName, (object)passwd,

                                          company, lang, servManager, objServ,

                                          config,     (object)false,

(object)" ", (object)" ");

                     }

              }

       }

 

2. Using the Logon method of  Axapta2Class

 

The other way is to login to Ax using the Windows username of the current user. For this, the user should follow the steps given below:

 

           a) In the Ax Client go to the Administration module.

           b) Go into the Users section and select any user.

                 c) Go to the General tab.

                 d)  Fill the ‘Network Account Name’ field with the current user’s Windows Username.

 

Now passing empty strings to the Logon method, makes it use the Windows Login information of the current user to automatically detect the Ax User ID, password and other default values from the Ax Configuration and use them for login, as shown below:

 

//Login to Ax

public void Login()

{

axConn.Logon("", "", "", "");           

}

 

Similarly, To Logoff the user can make a call to the Logoff method:

 

//Logoff from Ax

public void Logoff()

{

       axConn.Logoff();

}

 

Retrieve table data from Microsoft Dynamics Ax

 

Given below is the code for a Retrieve function that demonstrates how to execute an SQL query and retrieve data from a table in Microsoft Dynamics Ax using the methods provided by the Microsoft Dynamics Ax COM Connector:

 

private void Retrieve(string tableName, string fieldName)

{

        IAxaptaRecord retrieveRecordSet;

        retrieveRecordSet = axConn.CreateRecord(tableName);

                       

        string query = "select " + fieldName + " from %1";

        //query can be constructed with more fields seperated by commas

        //and filter conditions can also be added with the use of WHERE clause.

           

        retrieveRecordSet.ExecuteStmt(query);

 

        //retrieveRecord now contains values of the field from all the rows

        //To fetch each one of them loop through the retrieveRecord.

 

        while (retrieveRecordSet.Found)

        {

string retrievedValue = retrieveRecordSet.get_field(fieldName).ToString();

               

               //Make use of the value obtained in retrievedValue and go to the next row(record).

              

               retrieveRecordSet.Next();

        }           

}

 

Create a new data record in Microsoft Dynamics Ax

 

Given below is a Create function which creates (or inserts) a new record in a given table in Microsoft Dynamics Ax. It accepts a field name and a field value along with the table name. It then uses the DoInsert method to insert a new record in the table with the field’s value set to fieldValue:

 

private void Create(string fieldName, string fieldValue, string tableName)

{

        IAxaptaRecord createRecord;

        createRecord = axConn.CreateRecord(tableName);

       

        //Initialize the table for insertion

        createRecord.Call("initValue",null,null,null,null,null,null);

           

        //set the value to the field and insert the record.

        axConn.TTSBegin();

        createRecord.set_field(fieldName, fieldValue);

        createRecord.DoInsert();

        axConn.TTSCommit();

}

 

Update data in Microsoft Dynamics Ax

 

The Update function given below takes a field name, its old value, a new value and a table name and updates all records in the specified table that have oldValue in the field given by fieldName replacing it with newValue. It uses the DoUpdate method to do this:

 

private void Update(string fieldName, string oldValue, string newValue, string tableName)

{

       IAxaptaRecord updateRecord;

 

       //Note the 'FORUPDATE' clause.

       string query = "select FORUPDATE " + fieldName + "from %1 where %1." + fieldName +

                           " == '" + oldValue + "'";

 

       updateRecord = axConn.CreateRecord(tableName);

       updateRecord.ExecuteStmt(query);

 

       while (updateRecord.Found)

       {

           updateRecord.set_field(fieldName, newValue);

           updateRecord.Next();

       }

 

       axConn.TTSBegin();

       updateRecord.DoUpdate();

       axConn.TTSCommit();

}

 

Delete data in Microsoft Dynamics Ax

 

The Delete function given below uses the DoDelete method to delete all records which have delValue in the field specified by fieldname:

 

private void Delete(string fieldName, string delValue, string tableName)

{

       IAxaptaRecord delRecord;

 

       //Note the 'FORUPDATE' clause.

       string query = "select FORUPDATE * " + "from %1 where %1." + fieldName +

                           " == '" + delValue + "'";

 

       delRecord = axConn.CreateRecord(tableName);

       delRecord.ExecuteStmt(query);

 

       if(delRecord.Found)

       {

       axConn.TTSBegin();

       delRecord.DoDelete();

       axConn.TTSCommit();

       }

}

 

 

Published Wednesday, April 12, 2006 11:14 AM by Dynamics Snap Team

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Trading Strategy &raquo; Code Samples for basic operations performed by Snap-ins for Microsoft Dynamics Ax

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker