Wednesday, May 2, 2012

CRUD operation in Dynamics AX 2009 through .Net business connector

Friends, rather going into more details about "How to's" of accessing Dynamics AX through .Net, my main objective is to share my experience of performing CRUD operations in Dynamics AX 2009 through .Net BC.

Here is a sample code;


    // CREATE OR INSERT SAMPLE
    // Create the .NET Business Connector objects.
    Axapta ax;
    AxaptaRecord axRecord;
    string tableName = “AddressState”;
    try
    {
        // Login to Microsoft Dynamics AX.
        ax = new Axapta();
        ax.Logon(null, null, null, null);
        // Create a new AddressState table record.
        using (axRecord = ax.CreateAxaptaRecord(tableName));
        {
            // Provide values for each of the AddressState record fields.
            axRecord.set_Field(“NAME”, “MyState”);
            axRecord.set_Field(“STATEID”, “MyState”);
            axRecord.set_Field(“COUNTRYREGIONID”, “US”);
            axRecord.set_Field(“INTRASTATCODE”, “”);
            // Commit the record to the database.
            axRecord.Insert();
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(“Error encountered: {0}”, e.Message);
        // Take other error action as needed.
    }



    // READ SAMPLE
    // Create the .NET Business Connector objects.
    Axapta ax;
    AxaptaRecord axRecord;
    string tableName = “AddressState”;
    // The AddressState field names for calls to
    // the AxRecord.get_field method.
    string strNameField = “NAME”;
    string strStateIdField = “STATEID”;
    // The output variables for calls to the
    // AxRecord.get_Field method.
    object fieldName, fieldStateId;
    try
    {
        // Login to Microsoft Dynamics AX.
        ax = new Axapta();
        ax.Logon(null, null, null, null);
        // Create a query using the AxaptaRecord class
        // for the StateAddress table.
        using (axRecord = ax.CreateAxaptaRecord(tableName));
        {
            // Execute the query on the table.
            axRecord.ExecuteStmt(“select * from %1″);
            // Create output with a title and column headings
            // for the returned records.
            Console.WriteLine(“List of selected records from {0}”, tableName);
            Console.WriteLine(“{0}\t{1}”, strNameField, strStateIdField);
            // Loop through the set of retrieved records.
            while (axRecord.Found)
            {
                // Retrieve the record data for the specified fields.
                fieldName = axRecord.get_Field(strNameField);
                fieldStateId = axRecord.get_Field(strStateIdField);
                // Display the retrieved data.
                Console.WriteLine(fieldName + “\t” + fieldStateId);
                // Advance to the next row.
                axRecord.Next();
            }
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(“Error encountered: {0}”, e.Message);
        // Take other error action as needed.
    }


    // UPDATE SAMPLE
    // Create an Axapta record for the StateAddress table.
    axRecord = ax.CreateAxaptaRecord(tableName);
    // Execute a query to retrieve an editable record where the name is MyState.
    axRecord.ExecuteStmt(“select forupdate * from %1 where %1.Name == ‘MyState’”);
    // If the record is found then update the name.
    if (axRecord.Found)
    {
        // Start a transaction that can be committed.
        ax.TTSBegin();
        axRecord.set_Field(“Name”, “MyStateUpdated”);
        axRecord.Update();
        // Commit the transaction.
        ax.TTSCommit();
    }



    // DELETE SAMPLE
    // Execute a query to retrieve an editable record
    // where the name is MyStateUpdated.
    axRecord.ExecuteStmt(“select forupdate * from %1 where %1.Name == ‘MyStateUpdated’”);
    // If the record is found then delete the record.
    if (axRecord.Found)
    {
        // Start a transaction that can be committed.
        ax.TTSBegin();
        axRecord.Delete();
        // Commit the transaction.
        ax.TTSCommit();
    }




No comments:

Post a Comment

I will appreciate your comments !

How to enable new Microsoft teams - Public Preview!

New Microsoft Teams is just AWESOME, quick but useful post below shows how you have this preview feature to make your life EASY!  Open Micr...