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(); }
How to reuse table Id in Dynamics AX
Sometime we created tables just for testing purpose within AOT and later on we delete these tables. In such cases we don’t care about the IDs which are created for each table. There is a way to use these system generated IDs for other newly created tables. You can do this by following steps; 1. Export the table with its current id. 2. Delete the table 3. Edit the xpo file and change the id to what you want 4. Import the xpo file with the id option checked. As long as the id is not used by something else, then it should get imported with that id.
Dynamics AX 2009: How to retrieve table properties
Here is the sample code to retrieve table properties in Dynamics AX 2009. static void TableProperties(Args _args) { Dictionary dictionary; TableLabel tableLabel; SysdictTable dTable; TreeNode Tnode; ; //initilize the data dictionary dictionary = new Dictionary(); // Replace _tableId parameter with the actual table id dTable = new SysDictTable(_tableId); // initilize the tree node to traverse each property of the table tNode = dTable.treeNode(); // Replace “Label” with any property name of the table. In this case I am getting table label and it will return label id tNode.AOTgetProperty(“Label”); // Get actual label from label file print sysLabel::labelId2String(tNode.AOTgetProperty(“Label”)); pause; }