Wednesday, October 31, 2012

How to enable / disable form control through code in Ax 2009

Hi Friends,

I was designed new form for one of the functionality and while opening the form i suppose to set the properties of form controls. For this i  used the following line code.

1. Set the Auto Declaration property to true for that form control.
2. In the general methods of the form override the init() method.
3. In Init method after super() i used the following code line to set the properties
    E.g. element.control(control::ToDate).enabled(false);

4. While writing this line of code you will not get any intellisense after Control::
     You have to just type your control name and compile the method and it's done.

Hope this will help anyone.

Kiss'shor.. :)

Monday, October 29, 2012

Date functions in AX 2009


These are some of the functions,from where we can get the day or month or year from the date...
Here is the below example....

static void date_Functions(Args _args)
    Transdate    d;
    d = today();
    info(strfmt("Date - %1",d));
    //Gets the month for the given date...
    info(strfmt("Month - %1",mthofYr(d)));
    //Gets the month name from the given date...
    info(strfmt("Month Name - %1",mthname(mthofYr(d))));
    //Gets the day for the given date...
    info(strfmt("Day - %1",dayOfMth(d)));
    //Gets the day name from the given date...
    info(strfmt("Day Name - %1",dayname(dayOfMth(d))));
    //Gets the year for the given date...
    info(strfmt("Year - %1",year(d)));
    //Gets the current weekday number from the date...
    info(strfmt("Weekday number - %1",dayOfwk(d)));
    //Gets the day of the year from the given date...
    info(strfmt("Day of year - %1",dayOfyr(d)));
    //Gets the week of the year from the given date...
    info(strfmt("Week of the year - %1",wkofyr(d)));

Source :

Sequence of methods in the FORM level in AX


This gives the information of method calls in the form level while
1. Opening the Form.
2. Creating/Updating/Deleting the record in the Form.
3. Closing the Form.
Sequence of Methods calls while opening the Form
Form --- init ()
Form --- Datasource --- init ()
Form --- run ()
Form --- Datasource --- execute Query ()
Form --- Datasource --- active ()

Sequence of Methods calls while closing the Form
Form --- canClose ()
Form --- close ()

Sequence of Methods calls while creating the record in the Form
Form --- Datasource --- create ()
Form --- Datasource --- initValue ()
Table --- initValue ()
Form --- Datasource --- active ()

Sequence of Method calls while saving the record in the Form
Form --- Datasource --- ValidateWrite ()
Table --- ValidateWrite ()
Form --- Datasource --- write ()
Table --- insert ()

Sequence of Method calls while deleting the record in the Form
Form --- Datasource --- validatedelete ()
Table --- validatedelete ()
Table --- delete ()
Form --- Datasource --- active ()

Sequence of Methods calls while modifying the fields in the Form
Table --- validateField ()
Table --- modifiedField ()

How to Create a new “Parameters form” for New Module developed in AX

How to develop a new “Parameters form” in case of New Module developed in AX.
We go through it by considering an example of creating the new module as Payroll.
So now we will create the Payroll Parameters setup form for the payroll module.
Initially create a Parameters table for the Module, the naming convention should be the Module name followed by Parameter as in this case it is PayrollParameters table….and need to have the fields accordingly what is required in the Parameters table.
Set the following properties for the parameter table as…

Create a parameter form by considering the PayrollParameters andNumberSequenceReference as data sources. And set the following properties for the data source in the form level as

First thing is that there shouldn’t be any provision to create the record in the parameters form. The record in the Parameters should be by default and it can be updated as per the functionality required
So when creating the new company in the Axapta, the parameters form should have the default values.
To have the default values in the parameters we need to have the following code in the table  level of find() of PayrollParameters tables .
static PayrollParameters find(boolean _forupdate = false)
    PayrollParameters  parameter;

    if (_forupdate)

    select firstonly parameter
        index Key
        where parameter.Key == 0;

    if (!parameter && !parameter.isTmp())

    return parameter;

Friday, October 26, 2012

Displaying a Message Box

You can display a message in a modal dialog box by using the Box class.

Some methods in the Box class display a dialog box that contains multiple buttons. These methods have a parameter for choosing which button has focus when the dialog box is first displayed. This parameter is of type DialogButtonenum. Most of these multiple-button methods return a value of type DialogButton enum. This returned value enables your program to branch based on which button the user clicked.
The Box application class calls the DialogBox system class. Don't call it directly—always call the Box class instead.

The following are guidelines to help you create an effective message box:
  • Choose a Box method that matches your purpose.
  • Write the information text to match the buttons in the box.
  • Choose the button best suited for having initial focus.
  • Use the returned DialogButton value to direct the branching in your code.

The following table describes the Box class methods and their associated DialogBoxType system enum values.
Box class static method nameAssociatedDialogBoxTypeenum valueDescription
The OK button is the only one that the info method displays in the dialog box.
Use this method to display general information messages rather than for warning or error messages.
The infoOnce method is similar to the info method but with the following differences:
  • Your code cannot set the text in the title bar.
  • An additional header section is displayed under the title bar, and you can set the text that displays in the header.
  • More info button is present. When clicked, it opens a Web browser to the URL string that you supply through a parameter.
The infoOnce method returns void, even though it has two buttons.
The infoOnceEx method is similar to the infoOnce method but with the following differences:
  • The infoOnceEx method has a parameter that can be passed in to set the text in the title bar.
  • The infoOnceEx method has a Boolean parameter for choosing whether to have the caller thread continue processing while the user reads the message in the dialog box.
The OK and Cancel buttons are displayed in the dialog box. Call this method when the user must confirm or reject an action.
The OK button is the only one displayed in the dialog box.
Use this method to display a message when the user should stop attempting their action or when a process has stopped running.
The OK button is the only one displayed in the dialog box. Use this dialog box for a warning message.
The buttons displayed in this dialog box are YesYes to allNoNo to all, and Cancel.
The buttons displayed in this dialog box are Yes and No. Call this method when you need the user to make a choice.
The buttons displayed in this dialog box are YesNoNo to all, andCancel.
The yesNoAxaptaForm method is similar to the yesNo method but with the following differences:
  • The yesNoAxaptaForm dialog box has a different image.
  • The yesNoAxaptaForm dialog box is wider.
The buttons displayed in this dialog box are YesNo, and Cancel. Call this method when the yesNo method is insufficient.
The yesNoOnce method is similar to the yesNo method but with the following differences:
  • The yesNoOnce dialog box has a different image.
  • The yesNoOnce dialog box has a check box that the user can select to suppress any further occurrence of an associated message.
The YesYes to allNo, and Cancel buttons are displayed.

The following example displays a message box that contains buttons labeled YesNo, and Cancel. The No button has initial focus. When a button is clicked, a message is displayed in the Print Window to indicate which button was clicked. Run this example as a job in the Application Object Tree (AOT).
static void JobBoxDemo(Args _args)
    DialogButton diagBut;
    str strMessage = "The No button should have initial focus.";
    str strTitle = "Title";
    diagBut = Box::yesNoCancel(
        DialogButton::No, // Initial focus is on the No button.
    if (diagBut == DialogButton::No)
            print "The No button was clicked.";
            print "The button that was clicked was: ", diagBut;
Source :

Friday, October 19, 2012

Create Bat file to execute commands in AX


I wanted to execute the close ax application on certain conditions, so that i have used the .bat file to execute the same.

1) How to create bat file?

  1. Open notepad and enter the command that you have to execute.
  2. As I have to execute the close command for ax i used the following line of code.
         @echo off
          taskkill /F /im Ax32.exe

     3. "Save As" the notepad file with selection of "Save As Type:" : "All files" and enter file name with .bat extension.

2) How to call bat file from ax application ?
    I have executed the "Close application code" on the button click.
        void btnCloseAppl()
       str path;
       //Kindly check the path may be have to add '\' in between the path  


Friday, October 12, 2012

User Login and password Form design in AX 2009

If you need to store passwords in AX there are some application objects, classes and attributes that you can use.  This post details the steps you can take to allow entry of a password in a form, which will be stored in the database.

Password form

1.  Add the password field to your table. This field should be of type ‘CryptoBlob’ which is a container that contains binary data:

Password table field

2. Add an edit method for the password to your table:

01 //BP Deviation Documented
02 edit Password editPassword(boolean _set = false, Password _pwd = '')
03 {
04     CryptoBlob cryptoBlob = connull();
05     ;
07     if (_set)
08     {
09         this.Password = WinapiServer::cryptProtectData(str2cryptoblob(_pwd));
10     }
12     return (this.Password == connull()) ? '' : 'xxxxxxxx';
13 }

3. Drag and drop the edit method to your form and ensure that the attribute ‘PasswordStyle’ is set to ‘Yes’:
Password form control
4. To retrieve the password you will need a method similar to the following:

1 static Password getPassword(UserId _userId)
2 {
3     CryptoBlob cryptoBlob = TutorialPasswordTable::find(_userId).Password;
4     ;
6     return (cryptoBlob == connull()) ? '' :
7                 cryptoblob2str(WinapiServer::cryptUnProtectData(cryptoBlob));
8 }

The safest way to handle passwords is not to store them in the database. The steps described in this post are better than storing the password in the database as plain text, but far from bulletproof. Please ensure that AX security is fully considered if using this method (Table level security, access to code / development etc)
You can download the tutorial as an xpo here from axaptapedia

Restrict multiple times user login in AX 2009


Currently in Ax 2009 the user can login  multiple times in application, so to restrict users to open Ax 2009  multiple times we can use the following code.

** Before implementing this please take backup of your application files
Open Classes --> info --> StartupPost metod 
and copy following code into this method

void startupPost()
// To restrict user login form second login
xSession                    session;
SysClientSessions    SysClientSessions;
UserId                      currentUserId;
int                             counter;
currentUserId = curUserId();
if(currentUserId!=”Admin”)                     // Allow Admin User to login multiple time
    while select SysClientSessions
    where SysClientSessions.userId == currentUserId  
    && SysClientSessions.Status == 1   // 1 : Login 0 : Logout
        session = new xSession(SysClientSessions.SessionId, true);

        if (session && session.userId())
         Box::stop(“Already Logged-in : The same user id can’t log in twice.”);

Thursday, October 11, 2012

Ax 2009 Login or Startup Message

Hi Friends,
Following are the two methods which are useful to show start-up message at the beginning of AX 2009. 
1.  Update the Client configuration Utility option.
    Microsoft Dynamics AX Configuration Utility (client) -> Genereal Tab -> Field "Startup Message"

2. If you have to add the custom message as per users name at the beginning of AX 2009 then update the  following code in Classes---> Info class---> StartUpPost method.
void startupPost()
    UserInfo UserInfo;
    select Name from UserInfo where;
    info('Welcome to ';