Tuesday 25 July 2017

Handling with Models and Model Files in AX 2012



SQL Server-based Model Store

The earlier versions of Microsoft Dynamics AX like AX 4.0 and AX 2009, the metadata stores in the application files which is .AOD files.   In AX 2012 all the file based AOD files are replaced by SQL Server database based, which are called by Model Store.

Models and Model Files

Models are logical group of element like tables and classes.   In AX 2012 elements can be group and store in the model file.  Model files are easy to create, export, import and this can be uninstalled from the system when not required.
Models can manage through following tools.
  1. AXUtil.exe command-line utility.
  2. Programmatically; using axutilib.dll
  3. PowerShell.
Creating Model

Step 1: Click Start ->Click Run ->type “cmd” and Press End; you will get command prompt screen.
 
Step 2 : Type following command in command prompt.

“c:\Program Files\Microsoft Dynamics AX\60\Server\MicrosoftDynamicsAX\Bin”


Basically we are changing current directory to AOS Server folder.  The above path will change depends upon your current installation.

Step 3: Type following command in command prompt.
         AxUtil.exe create /model:CustCustomer /layer:USR

 Basically using AxUtil we are creating model name call “CustCustomer” in USR Layer
After successful creation of model file; you will see below out in command prompt.


Changing Current Model in AX
 Step 4: Click on Current Model in the status bar; system will display all the model available in current layer.
 
List of model available in Current Layer
Step 5:  Select “Cust Customer” model and click ok.
(System will change current model in the status bar; refer below screen shot)
Creating/Changing Object in current Model

Step 6:  Create Table called “ModelExample” and Save the table.

Step 7: Right click on “ModelExample” Table and select Properties.  In the properties you can notice that newly created object belongs to “CustCustomer” model

Exporting Model

Step 8: Type following command in command prompt.

AxUtil.exe export /file:c:\CustCustomerModel.axmodel /model:CustCustomer

You will see following output message in the screen.

Confirm Model files exported correctly by verifying physical folder

c
Uninstalling Model

Step 9: Type following command in command prompt.

              AxUtil.exe delete /model:CustCustomer

You will see following output message in the screen

Check Object Model in AX, You will not see the CustCustomer Model in Model List window.  All the object belongs to this model will be deleted from AOT.

Tuesday 18 July 2017

How to Fix SSRS Timeouts in AX 2012

As we all know that the AX 2012 reporting solution has been migrated to SQL Server Reporting Services. Reporting timeouts can cause lots of frustration for users and impact critical business processes such as month-end, year-end, etc.

In this blog post we will discuss the causes of timeouts in AX 2012 SQL Server Reporting Services (SSRS) reports. The intended audience of this blog post are Dynamics AX developers and other technical professionals. If you are seeing errors like this, read on to discover some common ways to fix reporting timeout issues in AX 2012

We have separated this blog post into two main sections:
  • Timeout Troubleshooting
  • Timeout Settings
Error message:

The report execution <Report_Name> has expired or cannot be found

Timeout Troubleshooting

SSRS report timeouts in AX 2012 are often caused by inefficient code/query design in AX. To identify where the bottleneck is, we need to debug and explore the SSRS logs.

Debugging

Before we get started, you need to make sure you have the right components to debug.
To debug Report Data Provider (RDP) classes, you must be on the Application Object Server (AOS) machine and that same machine must have the SSRS instance installed. Also the AOS must be setup for debugging.
Microsoft Dynamics AX Server Configuration Utility
SSRS reports in AX can get data from one (or more) places:
  • AX query
  • AX Report Data Provider
  • An outside data source
Debugging a Query can be tough, as it simply pulls data from the AX database from an AX query object in the Application Object Tree. See the section below on query optimization for techniques on improving query performance.
To debug a report based on an RDP, set a breakpoint in that report’s processReport method.
Microsoft Dynamics AX RDP
Observe the behavior of the RDP class using the same parameters as your report users to see where your bottlenecks are.

SSRS Logs

You can view the logs on the SSRS server to get more details on a timed out report. The logs can be found on the SSRS Server at \Program Files\Microsoft SQL Server\MSRS10.<Instance Name>\Reporting Services\LogFiles.
Reviewing SSRS logs will provide more information than AX infolog dialogs in many cases.
Microsoft Dynamics AX SSRS Logs

If the log doesn’t provide enough information, you can increase the DefaultTraceSwitch setting in the ReportingServicesService.exe.config file. The file can be found at \Program Files\Microsoft SQL Server\MSRS10.<Instance Name>\Reporting Services\ReportServer\bin.

Microsoft Dynamics AX Reportingservicesservice

Timeout Solutions

Once we have identified the error, sometimes we need a little help planning our attack on improving report runtimes. We have provided some common solutions to long running reports here.
Note that these code improvements will only have effect on large data sets. Make sure you know the source of your timeouts before taking the time to redesign code.

Code Review

We all make mistakes. Perhaps the developer who wrote the RDP class made a mistake or the data processing requirements of the report have changed.

Code Review: Query

If a query takes a long time to run, see if you can make it run more efficiently.
Common areas of improvement are:
  • Inner joins vs. exist joins
  • Dynamic field property set to Yes
If the business requirement is to see if a record exists, we can accomplish this through an inner join. However, when we do this we may be pulling unnecessary amounts of data into AX.
For example, the business requirement in the query below is to give all purchase order lines where the vendor’s vendor group has a payment term of 30 days. The join to the VendGroup table is only to filter the results, no VendGroup fields are required in the report.
Microsoft Dynamics AX VendGroup
This query is making two mistakes; the query does not need to perform an inner join and it doesn’t need to pull all the fields for the VendGroup table.
Microsoft Dynamics AX VendGroup 2
Here we have set the JoinMode property of the VendGroup node to ‘ExistsJoin’ and removed all of the unnecessary fields from the VendGroup node.
Now when the query runs, it will run faster due to the reduced amount of data requested from the AX database.

Code Review: RDP Class

If an RDP class is written in an inefficient way, it can greatly impact report running times. Here we will provide an example of an RDP class that can be refactored to improve report performance.
For example, say we have a very simple business requirement to pull the number of invoices that a given vendor has had in a report.
In the code segment below, an extra SQL call is made for each vendor in the report.
Microsoft Dynamics AX RDP Class 1
We can optimize this report by having all the invoice counts done during the initial report query.
In the resulting RDP class, we only make one call to SQL to get the total number of invoices for each vendor. This reduces network traffic, causes the report to run more quickly, and reduces the number of calls to the SQL server during report execution.
Microsoft Dynamics AX RDP Class 2

Special Case: AX Kernel Code

This scenario is a *last resort* for troubleshooting. Once you have exhausted query/RDP re-design as well as AX/SSRS settings you will most likely need to submit a support request with Microsoft. You can contact your Partner to do this and they will contact Microsoft on your behalf to resolve the issue.
To give an example of this type of scenario, myself and my team had a customer that was printing check runs of 1,000+ checks. The customer was running AX 2012 R2 CU 7. This is not terribly uncommon and AX should be able to handle these types of reporting scenarios. To add to the complexity, the ChequeDP RDP class and SSRS report (along with accompanying C# code) was heavily customized by a previous Partner and we had no documentation. To resolve the issue we tried the following:
  • We re-designed the ChequeDP class to extend the SrsReportDataProviderPreProcess
  • We increased timeouts in the Reporting Services AXC file as well as an isolated AX Client AXC file
  • We increased timeouts in the Production instance of SSRS
  • We increased timeouts in the Ax32Serv.exe.config file for the AOS handling reporting requests
Even after increasing all available timeouts to over an hour, the report was timing out against the SSRS instance after approximately an hour. Nothing seemed to work and we reached out to Microsoft. After troubleshooting it turned out to be an AX Kernel issue. As a Partner we do not have visibility into the AX Kernel code. Microsoft provided a (at the time unpublished) hotfix. We are providing the KB number and title here for reference purposes. If you are seeing timeouts after implementing the suggestions in this blog post and are running AX 2012 R2 CU7 (we cannot attest to other versions of AX 2012) you may want to reach out to Microsoft to resolve this issue and/or log into your Life Cycle Services account to download the latest hotfix for this issue.
KB 2936794 – Printing reports to printer is failing without timeout error for larger reports

Timeout Settings

Now that we’ve explored timeout troubleshooting, let’s take a look at the settings that can affect report timeouts.

Be careful of “Just increase the timeouts!”

Before we start, when running into timeouts in SSRS a common answer is, “Just increase the timeouts!” This kind of thinking is commonly a band-aid for poor report design. If you must increase timeouts as a troubleshooting measure, remember to set them back to previous levels after finding out why your reports are timing out! Long timeouts can cause the [ReportServer]TempDb database on the SSRS database server to grow very large when many users have open sessions (among other challenges).

SSRS Settings

Below are some common SSRS settings you can extend as your troubleshoot long running reports.

Report Execution Timeout: Site Level

To extend this timeout, open the SSRS Home Page and navigate to the Site Settings page (top right).
The default timeout is 30 minutes (1800 seconds).
SQL Server Reporting Services Site Settings

Report Execution Timeout: Report Level

By default, all reports use the Site level Report Timeout value. However, you can change the Report Timeout setting on a per-report basis.
SQL Server Reporting Services Dynamics AX
SQL Server Reporting Services Site Settings Report Timeout

SessionTimeout & SessionAccessTimeout

The SQL Server instance has two settings that may cause timeouts if the user session used to render SSRS reports takes too long.
The SessionTimeout and SessionAccessTimeout settings are in seconds.
By default, the SessionAccessTimeout setting is not visible in the SSRS settings. You can view them in SQL Server Management Studio by connecting to the SSRS instance and viewing the Properties of the instance.
SQL Server Management Studio Settings
SQL Server Management Studio Settings 2
To change the SessionAccessTimeout setting for the first time, you must run a script against rs.exe. This executable is typically located in \Program Files\Microsoft SQL Server\110\Tools\Binn (SQL 2008) or \Program Files (x86)\Microsoft SQL Server\120\Tools\Binn (SQL 2012).
Create a text file the following and save it with the .rss extension.
Public Sub Main()
Dim props() as [Property]
props = new [Property] () { new [Property](), new [Property]() }
props(0).Name = "SessionTimeout"
props(0).Value = timeout
props(1).Name = "SessionAccessTimeout"
props(1).Value = timeout
rs.SetSystemProperties(props)
End Sub
Now you can run the following command against rs.exe to update the timeout settings.
"C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\rs.exe" -i c:\temp\extendTimeouts.rss -s http://localhost/reportserver -v timeout="6000"
This example will set both user session timeouts to approximately an hour and a half.
Microsoft Dynamics AX CMD Timeout
For future changes to these timeouts, you can change them from the SQL Server Management Studio.
Server Properties

AX Settings

Since SSRS uses the AX Query Service, we may run into timeout issues due to these settings.

AX Settings: Server-side

We can make changes to the WCF thresholds in the AX32Serv.config. This file is typically located at \Program Files\Microsoft Dynamics AX\60\Server\MicrosoftDynamicsAX\bin on the AOS server.
Since the Query Service sends data to SSRS when requested, we can increase the sendTimeout attribute of the binding element with the QueryServiceBinding name attribute to handle requests that take longer than the default 10 minutes.
Microsoft Dynamics AX Settings Server Side

AX Settings: SSRS Host

The AX Reporting Extensions use the Business Connector settings for the AX Client installed on the SSRS host computer. Alternatively, SSRS can use an AXC file placed in \ReportServer\bin directory of the SSRS instance. View Create a new Microsoft Dynamics AX configuration [AX 2012] if you have this sort of configuration as you will need to edit that AXC file instead of the Business Connector configuration. These instructions are for a Business Connector configuration that is saved in the registry.
On the SSRS host server, open the Dynamics AX 2012 Configuration Utility as an Administrator (otherwise your changes may not be saved due to User Account Control) and navigate to the Business Connector configuration.
Microsoft Dynamics AX Configuration Utility 1
If there are no configurations other than the Original (installed configuration) create a new one.
Microsoft Dynamics AX Configuration Utility 2
Open Configure Services and acknowledge your settings will overwrite the default WCF settings.
Microsoft Dynamics AX Configuration Utility 3
If you see this message, you can download and install the Windows SDK for your version of Windows Server and the SDK for .NET Framework 4.0. But this is not required.
Microsoft Dynamics AX Configuration Utility 4
If you do not have the Windows SDKs installed, you will be presented with a text file. Search for QueryServiceEndpoint and increase the sendTimeout and maxReceivedMessageSize attributes of the binding element with the QueryServiceEndpoint name attribute.
When troubleshooting, we typically increase sendTimeout to 00:20:00 (20 minutes) and maxReceivedMessageSize to 4294967294 (twice as big).
Microsoft Dynamics AX Text File sendtimeout

Thursday 13 July 2017

Improve performance to display methods on forms by using “Caching display methods”

The performance of display methods can be improved by caching them if they are calculated on Application Object Server (AOS).  Caching display methods can also improve performance when records are transferred from the server to the client.

Syntax

Formdatasource.cacheAddMethod(tableMethodStr(<Table_Name>, <DisplayMethod>));

The value of all cached methods is set when data is fetched from the back-end database. In addition, the value is refreshed when the reread() method is called on the form data source. 

Example:

AOT -> Forms -> CustTable -> Datasources -> CustTable  -> init()

you can see  cacheAddMethod() 

Caching Display Method

AX2009 and AX2012 both supports the above method, but in AX2012 we have one more way to caching display method by using SysClientCacheDataMethodAttribute attribute in the display method declaration, Its perform automatically be cached on  any form where it’s used

Syntax
[SysClientCacheDataMethodAttribute([_updateOnWrite])]
display type methodName (parameters) 
{
}
Example
[SysClientCacheDataMethodAttribute(true)]
display CustAccount displayCustAccount()
{
    return (select firstOnly accountNum from custTable
                where custTable.accountnum == this.custAccount).accountNum ;
}