Wednesday, May 30, 2012

How to write Queries in Microsoft Dynamics Ax

This article is for beginner level.
This article explains:
How to write a query in Microsoft Dynamics Ax
How to add a DataSource to a Microsoft Dynamics Ax query
How to add a range to a Microsoft Dynamics Ax query
How to use Sum clause to a Microsoft Dynamics Ax query
How to use GroupBy clause to a Microsoft Dynamics Ax query

Instead of write a select statement it's better write queries using query objects.
Prerequisites:
1. You should be familiar with Microsoft Dynamics Ax

2. You should be familiar with X++ language


Following is an example of a query.
Create a new job and test it.

I used a table called Table1 where EmplId(string), BalanceHours(real),TransDate(date) and few more fields are available. I want to write a query where record will be shorted by EmplId in ascending order, sum of BalanceHours to be calculated by EmplId, a range should be on TransDate. 

The job is written below:


static void job1(Args _agrs)
{

Query q;

queryRun qr;

QueryBuildRange qbr;

QueryBuildDataSource qbd;

EmplTable emplTableLoc;

Table1 table1Local;

;


q = new Query();

qbd = q.addDataSource(tableNum(Table1));//Table1 is being added to the datasource

qbd.addSortField(fieldNum(Table1,EmplId),SortOrder::Ascending);//Records will be shorted by EmplId in Ascending order

qbd.addSelectionField(fieldNum(Table1,BalanceHours),SelectionField::Sum);//Sum of balance hours to be calculated.

qbd.orderMode(OrderMode::GroupBy);//Records will be groupby EmplId. If you don't do this then you will get the result of BalanceHours only.

qbr = qbd.addRange(fieldNum(Table1,TransDate));//For Date range. In a normal select statement we use where clause in place of this

qbr.value(SysQuery::range(systemdateget()-29,systemdateget()));//date range values

qr = new queryRun(q);//Assigning query to query run

while(qr.next()) 

{
table1Local = qr.get(tableNum(Table1));//Assigning the selected records to an instance of Table1 called table1Local

info(strfmt('Employee Id: %1 Balance Hours: %2',table1Local.EmplId,table1Local.BalanceHours));

}

}
After execution the job I got the result as shown below:

How to convert Microsoft Dynamics Ax Web form to use ASP.Net-based framework for Ax Enterprise Portal

Disclaimer: There is no warranty for this article. Use at your own risk.
Applied on: Ax 2009 SP1
Prerequisite:

  • You should have Dynamics Ax 2009 SP1 installed with Enterprise Portal
  • Visual Studio 2008 should be installed.
  • You should have basic knowledge of Dynamics Ax, Visual Studio, X++ and C#
  • You should have knowledge of previous version Dynamics Ax Enterprise Portal

This article explains:
  • How to convert Microsoft Dynamics Ax Web form to use ASP.Net-based framework for Ax Enterprise Portal.
  • How to use the Web Form Converter class.
  • How to convert Enterprise Portal Content.
  • How to convert a Web form to user control.
Enterprise Portal for Microsoft Dynamics AX 2009 uses a new framework based on ASP.NET.
User Controls created by using this framework are the primary method for adding functionality to Enterprise Portal. Visual Studio is used to develop or customize User Controls.
In earlier versions of Dynamics AX Enterprise Portal, Web forms and Web controls were the primary method to provide functionality. While Web forms and Web controls are still supported, but Microsoft recommends that any new development for Ax Enterprise Portal to be done with Visual Studio and the ASP.NET-based framework.
If you have created Web forms for an earlier version of Dynamics AX Enterprise Portal, you can convert them to use the new ASP.NET-based framework. A Web Form Converter class is available that can take an existing Web form and convert it into components that can be used with the new Enterprise Portal framework.
To convert a Web form to use the ASP.NET-based framework for Enterprise Portal, you must first add the SysEPWebFormConverter class to your Microsoft Dynamics AX installation. Then you will use methods in this class to perform the conversion.
The SysEPWebFomConverter class contains several methods that are used when you convert a Web form from an earlier version of Enterprise Portal to the components used with the ASP.NET-based framework. You can find the codes of this class from MSDN or you can download the XPO of this class from here. Download this zip file and extract it. Import the extracted XPO file to your application [Note: Applied on Ax 2009 SP1].
To add the SysEPWebFormConverter class
  1. If you use the MSDN code then instructions are there how to do it.
  2. If you download the above said zip file then extract the zip file.
  3. Open your Microsoft Dynamics AX application, open the AOT.
  4. Click the Import button to open the Import form.
  5. Click Browse and select the SharedProject_SysEPWebFomConverter.xpofile that you extracted.
  6. Click OK to start the import process. The class will be added to the AOT.
How to using the Web Form Converter Class 
To use the Web Form Converter class, you will specify which Web forms you want to convert.
  1. In the AOT, expand the Classes node.
  2. Locate the SysEPWebFormConverter class. Expand the node for this class.
  3. Right-click the getFormsToConvert method for the class, and then click Edit.
  4. In the script for this method, specify the Web forms that you want to convert. Replace WebForm1 and WebForm2 with the names of the Web forms to be converted. If necessary, add more lines for additional Web forms.
  5. Click Save to save the changes.
  6. In the AOT, right-click the main method for the class, and then click Edit. This is the main code that performs the conversion. It retrieves the list of Web forms to convert, performs the conversion, and then adds the resources that were created to a new project.
  7. Specify the name of the project to be created. By default, the project created will be named WebForm_Conversion. You can change this name if needed.
  8. Click Save to save any changes to the script.
  9. Right-click on the SysEPWebFormConverter class and click on Open to perform the conversion. The new project will be displayed that contains the new resources that were created.
  10. To use the new Web Controls that were created, you must deploy them to the Enterprise Portal server. In the AOT, expand the Web node, and then expand the Web Files node. Right-click the Web Controls node, and then click Deploy.
  11. After the conversion is complete, you should verify that the new components are working correctly.

To view a new User Control in Enterprise Portal, add a Dynamics User Control Web part to an Enterprise Portal page as add Dynamics Web Form Web part. Select the new User Control as the managed content item to display. 

To view a User Control in Visual Studio, create a new project as described below:
  1. Start Visual Studio.
  2. In the File menu, click New, and then click Web Site.
  3. In the New Web Site window, select .NET Framework 2.0 as the framework version to use.
  4. Set the Location to File System and the Language to Visual C#.
  5. Choose Dynamics AX Web Project as the template to use. If you do not see this project template, be sure that you have the Enterprise Portal Tools installed.
  6. Specify the name and location of the folder where you want to store the files for the Web project.
  7. Click OK to create the Web project.
  8. If a dialog box displays asking whether to upgrade the Web site to the .NET Framework version 3.5, click No.
  9. The Web Form Converter provides a starting point for converting Web forms to User Controls for Ax Enterprise Portal. You may have to use Visual Studio to modify the User Controls that were created by the conversion to achieve the results that you want.
Hope this article is helpful for you.
I will post in a separate article how to modify a user control for Ax Enterprise Portal.

How to use User Controls within .Net to develop or customize Microsoft Dynamics Ax 2009 Enterprise Portal -Part I

Note: There is no warranty on this article. Use at your own risk.

This article explains how to develop or customize Microsoft Dynamics Ax 2009 Enterprise Portal using User Control. How to use Visual Studio (.Net) environment to develop/customize Microsoft Dynamics Ax 2009 Enterprise Portal. How to use Microsoft Dynamics Ax 2009 Data Sets within .Net environment. How to develop or customize Dynamics Ax 2009 Enterprise Portal.

To follow this article you should have installed:
  1. Microsoft Dynamics Ax 2009 with Enterprise Portal
  2. Enterprise Portal should be configured with Microsoft Windows Sharepoint Services 3.0 SP1 or Microsoft Windows Sharepoint Server 2007 SP1. A website for Enterprise Portal should be created
  3. Visual Studio 2008.
You should be familiar with 
  • X++ Language
  • Microsoft Dynamics Ax 2009 customization
  • .Net development
  • Microsoft Windows Sharepoint Services product
Now I will explain step by step.

1. Open Microsoft Dynamics Ax 2009 application then AOT. Select Data Sets node, right click on it, and select New Data Set.

2. Rename it to SalesInfo (or any other name whatever you wish) as shown below:

3. Add SalesTable and CustTable to the Data Sources of SalesInfo Data Set as shown below. Make a join between these table.


4. Now open your Visual Studio, click on File->New->Web Site… as shown below


5. From web site template select Dynamics Ax Web Project template, Location should be File System, Language should be Visual C#, and .Net Framework 2.0as shown below. Click on OK.


6. Now see the Solution window where a project has been created in the name “AxWebProject1”. You can rename it.



7. Right click on AxWebUserControl.ascx page in the solution window then click onView Designer as shown above.
8. Now open the Toolbox and find Dynamics Ax tool section. Drag and dropAxDataSource control from Toolbox to the body of the AxWebUserControl.ascxpage.

9. Select the AxDataSource1 on the AxWebUserControl.ascx page then assign a DataSet to it from the AxDataSource Tasks dropdown as shown below. Here you need to select SalesInfo DataSet you created in step1.


10. Go to the Toolbox again and add AxGridView control from Toolbox to theAxWebUserControl.ascx form as shown below.



11.Add AxDataSource1 to AxGridView.

12.Now click on Edit Columns… on AxGridView Tasks.



You will get the below field designer. 



13. Add those fields you want to display. I added AccountNum, Name, CustGroup and SalesTable!Status. Later on I modified the fields.


14. Now select the AxWebPartPage.aspx in the solution window. Right click on it. Select View Designer. 



15. Now drag the AxWebUserControl.ascx page from solution window and drop it to the body of the AxWebPartPage.aspx. Press F5 to execute the page. See the data is being displayed from Ax in the internet explorer.


Grid format(Template) is also available. See step 12. Auto Format is there.


Now your newly created user control is available in Microsoft Dynamics Ax 2009 AOT. To check go to AOT->Web->Web Files->Web Controls->User Controls.

If it's not available in the AOT then you can add it to AOT.
Right click on .ascx control in the Sulution explorer then click on Add to AOT as shown below



Hope this will be helpful for you.
This is Part I.
In Part II I will let you know how to use this .Net made user control within Dynamics Ax 2009 Enterprise Portal (EP).

How to debug x++ code for Microsoft Dynamics AX Enterprise Portal

Note: There is no warranty on this article. Use at your own risk. 
I have done this on a single system.
 


This article explains:
 

  • How to debug X++ code for Microsoft Dynamics Ax 2009 Enterprise Portal
  • How to debug X++ code using .Net Business Connector
  • How to setup and configure to debug X++ code for Microsoft Dynamics Ax 2009 Enterprise Portal
  • How to debug Microsoft Dynamics Ax 2009 Enterprise Portal (EP)
1.
Log in to the server that is running the AOS.


2.
Open the Microsoft Dynamics AX Server Configuration utility. To open Start > Control Panel > Administrative Tools > Microsoft Dynamics AX 2009 Server Configuration.

3.
Create a new configuration that allows debugging.


3.a.

Click Manage then Create configuration. Give a name to the new configuration in the Create Configuration window then click OK.









3.b.
On the Application Object Server tab, select Enable breakpoints to debug code X++ code running on this server. Click on Apply button.



4.
Click on OK button to close the configuration window. You will get a message that indicates AOS is going to be restarted.

5.
Actually debugger works on the server where IIS is running and Enterprise Portal is hosted. I used a single system and all the things are on the same system. If you are accessing a system remotely using Terminal Services then from the Start menu click Run. Type 
mstsc /console in the Open text box and click OK
This opens a console session in Terminal Services. Console session is required to debug Ax EP

6.
The World Wide Web Publish Service should be enabled.
6.a.
Open the Services window for the system. To open it Start > Control Panel > Administrative Tools > Services.
6.b.
Right-click the World Wide Web Publishing Service and click Properties.


6.c.
Click the Log On tab.
6.d.
Select Allow service to interact with desktop as shown below.


6.e.
Click on OK button to close the properties window.

7.
Open the web.config file located in :\Inetpub\wwwroot\wss\VirtualDirectories\\, here the port number of the Enterprise Portal site. If you forgot the port then there is a way to check it out. Open Ax client then Administration main menu > Setup > Internet > Enterprise Portal > Web sites, here you can see the port.



8.
Now do the following:
8.a.
Find out the compilation element then set the debug attribute to true.
8.b.
Save the changes.

9.
Reset IIS by typing the iisreset command at the command-line window or typeiisreset in Start > Run > Open text box then click on OK button.

10.
Open the Microsoft Dynamics AX Configuration utility. To open it Start > Control Panel > Administrative Tools > Microsoft Dynamics AX 2009 Configuration.

11.
Set the Application Object Server Instance drop-down menu to Business Connector (non-interactive use only).



12.
Create a new configuration to allow debugging.
12.a.
Click on Manage button then click Create configuration. In the Create Configuration window, name the new configuration then click OK button.
12.b.
On the Developer tab, select Enable user breakpoints to debug code in the Business Connector and Enable global breakpoints to debug code running in the Business Connector or client then click on Apply button.

13.
Click on OK button to close the configuration window.

14.
Open the Microsoft Dynamics AX client.

15.
On the Tools menu, click Options to display the Options window.

16.
On the Development tab, select When Breakpoint from the Debug mode list box, and then click Apply. This enables debugging mode on the client.


17.
Close the Options window.

18.
Open the Microsoft Dynamics AX client.

19.
Now decide the element you want to debug. Lets say you want to debug a class method. Open the method in code editor and write breakpoints where you want to set a breakpoint.

20.
Open the Debugger window manually. To open debugger manually click on Tools > Development Tools > Debugger.

21.
Now execute the web form where the method has been invoked. It should work. On my system it is working perfectly.


If there is any problem please let me know. Definitely I will help you.

How to write data into an Excel sheet from Microsoft Dynamics Ax

Note: There is no warranty on this article. Use at your own risk.

This article explains

  • How to write data into an Excel sheet from Microsoft Dynamics Ax using X++ language.
  • How to write data into an Excel sheet using COM object in Microsoft Dynamics Ax.
  • How to export Microsoft Dynamics Ax data into an Excel sheet at run time / dynamically /programmatically using X++ language.
I applied it on Dynamics Ax 2009. 

Prerequisite: 
  • You should have X++ basic programming knowledge;
Open Ax client > AOT > Jobs node, create a new job and copy the below code segment into your job.

static void writeDataIntoExcelSheetFromAx(Args _args)
{
InventTable inventTable;
Container itemIdCon;
COM comRange;
COM comWorkBook;

COM comWorkBooks;
COM comWorkSheet;
COM comCharacters;
COM comAppl;
str test,test1;
int offset = 65-1; //65 means letter 'A'
str 2 columnId;
str fileName;
str time;
int i;
#define.Excel('Excel.Application')

;

comAppl = new COM(#Excel);
comAppl.visible(true);
comWorkbooks = comAppl.workbooks();
WINAPI::createFile('C:\\test.xls');
comWorkbook = comWorkbooks.open('C:\\test.xls',true,true);
comWorksheet = comWorksheet.new('C:\\test.xls');
comWorksheet = comWorkbook.activeSheet(); //Use 
comWorkbook.activateSheet(); in case of Ax 3.0
comWorksheet.select();

while select inventTable
{
columnId = num2char(offset + 1);
i++;
test = columnId + int2str(i);
comRange = comWorksheet.range(test);
comCharacters = comRange.characters();
comCharacters.insert(inventTable.ItemId);

columnId = num2char(offset + 2);
test = columnId + int2str(i);
comRange = comWorksheet.range(test);

comCharacters = comRange.characters();
comCharacters.insert(inventTable.ItemName);
}

WINAPI::createDirectory('C:\\AxData');
time = time2str(timenow(),1,1);
time = strrem(time,':');
fileName = 'C:\\AxData\\' + curuserid() + date2str(today(),123,2,0,3,0,4)+ time + 'test' + '.xls';
comWorkbook.saveAs(fileName);

}

How to identify the user that was used to change an object from AOT in AX2012

Get the object name for which we need to track these (user and date&time) information's. Login to SQL Server Management Studio an...