Posts

Showing posts from April, 2010

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 <= ageArray.lastIndex(); i++) { print ageArray.value(i); } conValues = [2, 1, 3, 2, 20, 4, 40]; ageArray = Ar