Showing posts with label MorphX. Show all posts
Showing posts with label MorphX. Show all posts

Run External Application from Dynamics Ax

March 20, 2008

Calling up external program from Dynamics Ax can be something very interesting to audience during introductory training and demo. Once in a while, I will encounter associates asking if it is possible to run external program, open word document, open a URL in an external browser, etc. from within Dynamics Ax. Usually they will show a sign of immense enthusiasm when I show them how it is achieved. It is like their imagination is exploring all sort of creative ways to make use of this facility.

The interesting part is that executing external application is fairly effortless in Dynamics Ax. X++ is capable of calling Microsoft Windows Application Programming Interface (API). The common functionalities of the Win API have been built in classes WinAPI, WinAPIServer, WinGDI and WinInet. Running external application could be achieved through the static method WinAPI::shellExecute.

Static Method WinAPI::shellExecute

This method takes six parameters where five of them are optional parameters. The following code segment shows the interface of this method.

client static int shellExecute(
    Filename _lpFile,
    str      _lpParameters      = '',
    str      _lpDirectory       = '',
    str      _lpOperation       = #ShellExeOpen,
    int      _show              = #SW_SHOWNORMAL,
    boolean  _waitForCompletion = false
    )

The interface might look complicated but the first parameter is usually all we need to assign. It is sufficient to achieve most of the scenarios. The second parameter allows us to execute an executable with parameters. We will look at examples where this second parameter comes into play later.

Class SysShellExecute

The class SysShellExecute facilitates calling WinAPI::shellExecute. This class has a main method that call the method WinAPI::shellExecute using args.parm() as the first parameter. This enables WinAPI::shellExecute to be called from menu item with ease. This is important Dynamics Ax bring up windows through menu item. Menu item works with buttons with ease.

The following figure shows the property dialog of a menu item using SysShellExecute. The menu item shown will open an Internet Explorer browser when executed.

SysShellExecute Menu Item Property

Opening a File or URL

You shall not encounter any issue running application with class SysShellExecute. However, I have received enquiry when it comes to opening a file or a URL. I do not want to go into the ways they have tried. Basically opening file and URL are equally simple.

1. Default Application

Windows has associated different file type to a default application. We just need to execute the file name in order to open that file with the default application. The following figure shows the property page of a menu item that opens the website Dynamics Ax Associate in the default browser.

Open URL with SysShellExecute Menu Item

2. Specific Application

The previous approach opens the file or URL in the default application. There are cases where you need to specify the application to open the file with. You may achieve this with static method WinAPI::shellExecute.

Let say the default browser for your computer is FireFox and the website you are opening requires Internet Explorer. You may use the following code to open the URL with Internet Explorer.

WinAPI::shellExecute("IEXPLORE.EXE",
    "http://axassociate.blogspot.com");

Conclusion

The examples given above cover the execution of Windows Internet Explorer and opening of URL. They work similarly with a Word Document, Excel Spreadsheet, etc.

Base Enum without Default Value

December 12, 2007

Ever come across the need to capture enumerated data without the value filled in by default? We are going to look at two approaches and how they behave in different controls.

Enumerated data without default value here refers to fields with data type Base Enum that is not filled in when a new record is created. In other words, you would like the user to deliberately or implicitly select a value. The following figure shows a grid with new record. There are two columns both are of enumerated data type. The first column has no default value whereas the second has Normal as its default value.

Base Enum with and without default value

Approach #1: Blank Element Zero

One way to achieve this is to have the default element (value 0) configured with blank label. The following figure shows the setting for this approach. Element named “None” is the default element. Note that the label of this element is blank.

Base Enum with Blank Element Zero

This figure shows Base Enum FreightSlipType. You could see the corresponding field in Sales Order form. There is a field labeled Call tag type in the Delivery tab based on this Base Enum.

Approach #2: No Element Zero

The other way this is achieved is have no element with the value 0 in the Base Enum. The default value for an enumeration is zero. This will have the default value refers to none of the members of the enumeration.

The following figure shows a Base Enum with member elements starting from value 1. This Base Enum is created to illustrate this approach.

Base Enum with No Element Zero

Approach #1 vs Approach #2

I have created a form with the two methods of achieving no default value and a field with default value to show how they behave in different controls. The following figure shows the three types of Base Enum in three type of controls; combo box, radio button and list box.

Base Enum approach Comparison

In combo boxes, you see that both approaches started without a value. The drop down list for approach #1 (Call tag type) contains an empty line. This does not happen to the drop down list of approach #2 (No default). This means approach #1 enables the user to revert the value to empty but not the product of approach #2.

When the Base Enums are shown with radio button and list box, you will get an empty radio button and empty list entry respectively. Naturally, this does not affect radio button and list box based on approach #1.

Conclusion

Both approaches have their pros and cons. There are conditions where one is more suitable than the other.

Having blank element 0 produces a field that requires the user to choose a value and can be reverted to blank. This approach looks fine on a combo box but does not look good on radio button and list box. It is suitable for cases where it is optional.

Having no element 0 on the other hand produces a field that forces the user to pick a value. Once picked, the value cannot be reverted to blank. It can only be changed to some other value in the enumeration. This approach looks fine in all controls. It works very well to enforce mandatory field.