Posts Tagged ‘Coding’

Productivity tip: Do you need the under score when naming – Dynamics Ax

October 4, 2013 Leave a comment

Well When we sat through the product review there was one simple point that caught our attention, surprisingly it helped us improve our productivity. This post is to share on what we learnt.

Typically i have seen several implementation and product development teams adopting the ‘_'(underscore) naming convention. In fact we followed it through out Dynamics AX 2009. Though at the very first look it appears helpful in hindsight it deters productivity.

How ?

1. While you type holding a shift key is not making typing faster.

2. Every time you declare a variable you are again held up with underscores

3. On the AOT node navigating is definitely not easier.

If you look all these points at a component level it can be ignored but when you do it over and over it adds up to a significant time.

Next time you start a new project or product consider if you need to use the underscore ‘_’ again.

Producvity tip: Don’t scroll use Ctrl + G in your Editor – Dynamics Ax

September 27, 2013 Leave a comment

The other day as i was reviewing a code modification to a long method from my team mate he had to walk me up and down through the code scrolling.

Just then the Ctrl + G flashed in me. Yes It takes a lot less effort to type in the line number then scrolling up and down. Even if you are not sure of the line number make it a guess then it is always a few lines up or down.


If you are newbie here is how you can find the line number

On AX 2009 

You can identify the line number in the status bar.


On AX 2012

Activate the toggle line number in the tool bar or identify the line number from the status bar


Best practice

Ideally keeping best practices in mind you must right methods that have limited number of lines and break them and keep it modular.

SSRS Tip: Prevent report execution through controller – Dynamics Ax 2012

September 25, 2013 Leave a comment

What a user might hate to see is a blank report. No matter where the mistake lies the earlier we react the happier the user is. Here today i’m sharing a tip on how you could stop/warn/show a info after the report execution is initiated  after the user clicks the “Ok” button in the dialog.

In the example discussed here, the idea is to check preemptively if a query will return a record or not. If no then the report aborts execution.

To implement this, open the controller of your report or create one.

On the controller class, override the method “preRunValidate”

Place the code as shown here. This method is invoked after the user clicks the “Ok” button and before the report is executed. Do not block the super call in this method as it does few other important validations. This method can return an error/warning/info. If it is a warning or error the report aborts further execution.

protected container preRunValidate()
    container   validateResult = super();
    Query       firstQuery;
    int         recordcount;

    firstQuery          = this.getFirstQuery();
    custStatementCount  = QueryRun::getQueryRowCount(firstQuery, 6);
    if (custStatementCount > 5)
        validateResult = [SrsReportPreRunState::Error, "No valid records found for the specified range"];

    return validateResult;

Standard recommends the method to be used for validating if the report returns a large amount of data.For reference see \Classes\CustAgingReportController\preRunValidate\
For more tips and learning about SSRS – AX 2012 order the book Dynamics AX 2012 Reporting Cookbook authored by me.


Dev tip: Finding if a range is specified in a query – Dynamics Ax

August 26, 2013 2 comments

Here is a simple tip.

Recently i had a requirement which wanted to validate if a range has been entered in a query. The obvious approach would be running through the ranges in the query data sources and verify if each of them has a range. When you have multiple data source this is obviously not an interesting way to solve it and can be fooled around by just entering a value like ‘*’. So i was thinking out on a better approach and luckily i seemed to find it. Here it goes, the idea is to get the count of the records through the Query and get the count of the records directly through a DML, when compared if they are equal then there is no range entered.

InventTable inventTable;
int cntTotal;

cntTotal = SysQuery::countTotal(gQueryRun);

select count(recid) from inventTable;

if (inventTable.RecId == cntTotal)
throw error('Atleast one range must be specified for the item selection.');

Where can this be used ?

You can put it to use to prevent long running reports to ensure that there are proper ranges.

Related articles

Dev Tip: Finding test data across companies – Dynamics Ax

August 14, 2013 4 comments

This is going to be an obvious tip but the application is really very useful..

Most of the time as developers we get in to the daunting task of preparing test data to validate our changes. As developers we might either find it a mammoth task to fill all the data and do a test. The easiest shortcut we would look for is finding an existing data. The challenge we get in to often is a strange table which we don’t know how it gets filled even and we need to find if there is a data in any company. Searching the data comes with the difficulty to switch across 10 companies, opening the table browser and look for data. Here is a quick way to cut down your efforts.

Idea 1: Go to the SQL server management studio and right a simple group by statement.

Draw back: As a developer not all times i have the comfort of access to the SQL server

Idea 2: No issues. we have the power 🙂 Use the cross company feature :)…

In the example shown here we are searching for the list of companies that have record for tax trans.

    TaxTrans taxTrans;

    while select crosscompany  *  from taxtrans
    group by dataareaid

you can apply this query with more conditions and joins to narrow down your search.

Read here more on cross company query

Happy finding !!!

Creating Edit methods for DS that don’t allow editing in Dynamics ax – Followup

March 9, 2011 Leave a comment

A while ago i made a post on allowing a edit method to work on a disabled datasource ..

I found a still better idea from one of the comments, this post is to highlight the comment.

I rather would loop trough all data source fields (FormDataObject) and set them allowEdit=false at initalization time of the form/ds. That’s more confortable for the user, because he sees, that he cannot modify other fields than the edit fields. – 

One suggestion that i would add along to this is creating a SysHelper class for this, in case you find this recurring. You can just pass the record or datasource and the method will disable all the fields.

public class SysDictTable extends DictTable implements SysDictXmlReflectionProvider, SysDictXmlDocumentationProvider
    public static void disableDSfields(FormDataSource _fds)
        SysDictTable dictTable;
        int          i;

        dictTable = new SysDictTable(_fds.table());

        for (i = 1; i <= dictTable.fieldCntWithoutSys(); i++)

Running a .Net reference code in the Server in Dynamics Ax

February 8, 2011 2 comments

We have been recently building some .Net solutions for our vertical. I have consolidated my learnings from it and publishing here as a post.

1. Adding a .Net reference can be as easy as just to drop the dll in the client bin folder and then add it. But if you want the code to execute on the server then this wont work. You will have to publish the dll in the server “GAC(Global Assembly Cache)”.  A assembly on the cache executes on the server.  Look here for articles on publishing a dll to the cache (GAC Publishing) and assigning strong name to it (Assigning Strong Key).

2. When you look at for the GACUtil, you will find it in different places in different pc. The general places to look at are,

-> C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin

-> C:\WINDOWS\Microsoft.NET\Framework\

3. Every time you change and build the solution you have to un publish/publish it to the GAC. This can be difficult to do it manually. The easiest way is to add the following lines in your Project -> Properties -> Post-build event command line  in your visual studio project.

//uninstal old version

“gacutil.exe -u $(TargetName)”

//register new version

“gacutil.exe -i $(TargetPath)”

4. Though you run the code on the GAC it is necessary that the dll is added to all the clients just for compilation. It need not be updated as you revise your dll.

5. If the code is present in a server call then it is required to restart the server every time you re publish a new version to the GAC.  What i found better was to test the code by client level execution(Through a job) and once the code is working fine you can move it to the server call.

6. If you are creating a .Net instance in a class and wanted to retain the instance as a global variable then you must declare the variable as CLRObject and not in the actual .Net namespace name(Eg: Microsoft.Dynamics.Csharp). Failing to do so results in a run time marshalling error.

Converting a System.Xml.XmlDocument to XML type in Dynamics ax

April 18, 2010 1 comment

When interacting with .Net assemblies mostly  Xml is used to send/receive outputs from the external system. Here is a code snippet that tells how to convert a System.Xml.XmlDocument to a Xml string in Ax which you can use for further processing by classes like XmlDocument…

//CLR Objects
System.Xml.XmlDocument document;
System.Xml.XmlTextWriter textWriter;
System.IO.StringWriter   stringwriter;

//X++ Objects
Xml                      Xml

//call to the .Net assembly that returns a XML
document    = processINput(...argument..)

stringWriter = new System.IO.StringWriter();
textWriter   = new System.Xml.XmlTextWriter(stringwriter);


//Converted to Ax Xml format here
xml        = stringWriter.ToString();

Debugging form controls in Dynamics Ax

April 15, 2010 3 comments

The X++ debugger doesn’t get invoked when placing breakpoint(ctrl+f9) in form controls.

There have been quite a few suggestions to work around this like the one mentioned one below

I have also a suggestion which i think can be slightly better also.

Add a method called bp() to global.

static void bp()

Now when you want to debug  a formcontrol, type bp() there and  place a breakpoint(ctrl+f9) in the global method.

This will invoke the debugger without any issue.

The advantage here is

>> You need not bother about “breakpoint”  in the code that is shipped.

>> With hardcoded breakpoints you cannot stop them when you want the code to execute, but with this bp() method you can easily remove it  as it is just (ctrl+f9) based.

>> At the end typing bp() is easy then typing breakpoint 😉

Setting up Form size during runtime in Dynamics Ax

April 7, 2010 3 comments

Sometime we change  or construct a form at  runtime. In those situation we end up modifying/setting the width and height at run time itself.

there is one thing you need to keep in mind if you are doing that.

Don’t use the following way to set the size of the controls in forms


then nothing happens for the Form. The form size is actually set to zero.

Instead you must do it the following way….

For Auto/ColumnWidth, height properties, set it like this


For setting manual values set it like this


I’m not sure for what reason it is done like this 😦