Saturday, October 22, 2016

Using AOT Query object from X++

1) Create a AOT Query Object as your requirement.
2) Create a Job and paste the below code to check the Query Object.
 static void ExecuteAOTQuery(Args _args)  
 {  
 QueryRun queryRun;  
 Counter totalRecords;  
 ;  
 queryRun = new QueryRun(queryStr(CreatedAOTQueryName));  
 if (queryRun.prompt())  
 {  
 while (queryRun.next())  
 {  
 totalRecords++;  
 }  
 }  
 info(strFmt(“Total Records : %1”, totalRecords));  
 }  

Thursday, October 20, 2016

Return DataTable From X++ to .Net Business Connector in AX2012

If you want to return daatatable from x++ to .net business connector follow the below instruction

X++

Create X++ Class and create method and paste the bellow code
1:  private System.Data.DataTable method1()  
2:  {  
3:    EcoResProductTranslation productTran;  
4:    InventTable inventTbl;  
5:    EcoResProduct product;  
6:     Query query;  
7:    QueryRun queryRun;  
8:    QueryBuildDataSource queryBuildDataSource1, queryBuildDataSource2;  
9:    QueryBuildRange queryBuildRange;  
10:    QueryBuildLink queryBuildLink;  
11:    System.Data.DataRowCollection dataRowCol; 
13:    System.Data.DataTable table1 = new System.Data.DataTable('Table1');  
14:    System.Data.DataColumn column1 = new System.Data.DataColumn('ItemNumber', System.Type::GetType('System.String'));  
15:    System.Data.DataColumn column2 = new System.Data.DataColumn('Name', System.Type::GetType('System.String'));  
16:    System.Data.DataColumnCollection columnCollection = table1.get_Columns();  
17:    System.Data.DataRow dataRow1;  
18:     dataRowCol = table1.get_Rows();  
19:    column1.set_DefaultValue(0);  
20:    columnCollection.Add(column1);  
21:    column2.set_DefaultValue('');  
22:    columnCollection.Add(column2);  
23:    // Create a new query object  
24:    query = new Query();  
25:    // Add the first data source to the query  
26:    queryBuildDataSource1 = query.addDataSource(tableNum(EcoResProductTranslation));  
27:    // Add the range to this first data source  
28:    queryBuildRange = queryBuildDataSource1. addRange(fieldnum(EcoResProductTranslation,LanguageId));  
29:    queryBuildRange.value("en-US");  
30:    // Add the second datasource to the first data source  
31:    queryBuildDataSource2 = queryBuildDataSource1.addDataSource(tableNum(InventTable));  
32:    // Add the link from the child data source to the  
33:    //parent data source  
34:    queryBuildLink = queryBuildDataSource2.addLink(fieldnum(EcoResProductTranslation,RecId),fieldnum(InventTable,Product));  
35:      queryRun = new QueryRun(query);  
36:    // Loop through all the records returned by the query  
37:    while (queryRun.next())  
38:    {  
39:      // Get the table data by using the get() method  
40:      productTran = queryRun.get(tablenum(EcoResProductTranslation));  
41:      inventTbl = queryRun.get(tablenum(InventTable));  
42:      info (strfmt("ItemNumber %1, Name %2", inventTbl.ItemId, productTran.Name));  
43:      dataRow1 = table1.NewRow();  
44:      dataRow1.set_Item('ItemNumber', inventTbl.ItemId);  
45:      dataRow1.set_Item('Name', productTran.Name);  
46:      dataRowCol.Add(dataRow1);  
47:    }  
48:    table1.AcceptChanges();  
49:    return table1;  
50:    } 

C#



1:  Axapta Ax;  
2:  AxaptaObject AxObject;  
3:  System.Net.NetworkCredential creds = new System.Net.NetworkCredential("admin","pass@word1", "CONTOSO.com");  
4:  Ax = new Axapta();  
5:  this.Ax.LogonAs("admin", "", creds, "demf", "", "ax2012r2a:2712", Application.StartupPath + "\\bcConfig.axc");  
6:  AxObject = Ax.CreateAxaptaObject("HelloWorld");  
7:  DataTable dt = (DataTable)AxObject.Call("method1");  
8:   dataGridView1.DataSource = dt;