Posts

Writing to the Windows event log from within Dynamics AX.

Image
The workflow runtime in AX 2009 will log any error it encounters to the Windows event log. We can log to the event log using .Net classes. The System.Diagnostics.EventLog class provides methods to do this. To create an event log instance, we write the following line of code ev = new System.Diagnostics.EventLog("Application"); ev.set_Source("Dynamics Server 01"); Here Application is the log we want to write into and Dynamics Server 01 is the source of the message. You write a message to the log using the following line ev.WriteEntry(msg, entryType); In the above line, msg is the message you want to output and entryType is an enum value of type System.Diagnostics.EventLogEntryType which can be a warning, error or Information. You can download the job for this from here .

How to find if a table is mapped in a map?

Sometimes we would like to know if a particular table is mapped in a map. To achieve this, SysDictTable::isTableMapped() method can be used. This method takes two parameters, the map name and the table which we want to check if it is mapped. SysDictTable::isTableMapped(tablenum(AddressMap), tablenum(LedgerTable)); SysDictTable::isTableMapped(tablenum(AddressMap), tablenum(CustTable)); The first statement will return false since LedgerTable does not have a mapping in AddressMap map. The second statement will return true since the CustTable fields have a mapping in the AddressMap map. Note how the tablenum() function can also be used to return the id of a map.

SysCompareText class – text comparison with white spaces.

Text comparison in Dynamics AX is very easy and is provided by the == operator and the strcmp() method. The difference between these two is that == is case insensitive whereas strcmp() is case sensitive. Let us suppose we have a case where we need to compare two strings Text1=’A new string’; Text2=’A new string’; Now logically these two strings are same semantically if not for the extra white spaces. So how can we compare them? This is where the SysCompareText class can be helpful. The static run() method takes a parameter _suppressWhiteSpaces which can control if white spaces within string should be taken into account while comparing strings. A value of true which by the way is the default value for this will ensure that white spaces are not considered while comparing. So text1 & Text2 are shown as identical. A value of false will show that Text1 & Text2 in the above case are not equal. You can download the code for this from here .

Create() method in the AX foundation classes.

Image
When it comes to creating and using Maps, Lists or Arrays, we normally initialize them using the new() method. Then we use the respective methods on these to insert the data. All these classes have a static method create() which can both initialize and insert values into these at the same time. Even though, the method takes a container parameter in all the three cases, the content of the containers are very different from each other. Let us look at all the three in detail. Download the XPO for the source code from here . Code for Array class. static void ArrayInit(Args _args) { Array ageArray, nameArray; container conValues; counter i; ; conValues = [1, 1, 3, 10, 20, 30, 40]; ageArray = Array::create(conValues); print "Printing age array (consecutive values) ..."; for(i=1; i { print ageArray.value(i); } conValues = [2, 1, 3, 2, 20, 4, 40]; ageArray = Array::create(conValues); print...

Spell Checker in Dynamics AX

Dynamics AX has a neat little SpellChecker class that can help in catching those spelling mistakes. This can be helpful when you are creating parser which needs to check words for correct spellings. It is very simple to use. Here is a sample job. static void SpellChecker(Args _args) { Array array; List list; ListIterator listIterator; SysSpellChecker sysSpellChecker; int i; ; array = new Array(Types::String); array.value(1,"Hello"); array.value(2,"Werld"); sysSpellChecker = SysSpellChecker::newCurrentDocumentationLanguage(); for(i=1;i { if(!sysSpellChecker.checkSpelling(array.value(i))) { list = SysSpellChecker::newLanguageId("en-us").getSpellingSuggestions(array.value(i)); listIterator=new ListIterator(list); print "Spelling suggestions for '",array.value(i),"'"; while(listIterator...

Task method on Form

Suppose you have a form with a grid control which displays some record. Now through code, you want to move ahead or back in the grid, similar to like moving to next record or previous record. That can be done using the task method on the form. The task method takes an int argument that defines the task to be performed. For moving to the next record, the task id is 2817 and moving to the previous record the task id is 2818. Insert a button, name it Next and put the following code in the clicked method void clicked() { super(); element.task(2817); } So when you click this button, the cursor will move to the next record in the grid. Some of the other tasks and their ids are given below. Tasks Task Ids ======================= New 260 Save 272 Print 278 PrintPreview 299 Cut ...

Add menu items to the context menu in Dynamics

Image
You can add your own menu items to the Context menu in Dynamics by adding a reference of the menu item to the SysContextMenu menu. As seen in the image, I have added a class menu item ShowFormMethods to the sysContextMenu. After I save, I right click on any node in AOT and goto Add-ins, I can see my menu item. You can also add a menu item to Tools -> Development Tools by adding a reference of the menu item to DevelopmentTools menu. I have added a small project which you can look into and see how it has been done. Download it here .