<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.3.2" --><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">

<channel>
	<title>SysDictCoder</title>
	<link>http://sysdictcoder.com/blog</link>
	<description>Dynamics Ax stuff</description>
	<pubDate>Mon, 18 Feb 2008 09:25:23 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.2</generator>
	<language>en</language>
			<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" /><item>
		<title>Changing properties of AOT objects in code</title>
		<link>http://sysdictcoder.com/blog/changing-properties-of-aot-objects-in-code/</link>
		<comments>http://sysdictcoder.com/blog/changing-properties-of-aot-objects-in-code/#comments</comments>
		<pubDate>Mon, 18 Feb 2008 08:51:33 +0000</pubDate>
		<dc:creator>Björn</dc:creator>
		
		<category><![CDATA[Dynamics Ax (Axapta)]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[X++]]></category>

		<guid isPermaLink="false">http://sysdictcoder.com/blog/changing-properties-of-aot-objects-in-code/</guid>
		<description><![CDATA[A while ago I wrote about reflection in Dynamics Ax.  In the article I mentioned that it was also possible to modify the AOT and that I&#8217;d give some examples.  Well, I kind of forgot.  Sorry about that.  Todd Hensley was kind enough to remind me of that promise and presented [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago I wrote about <a href="http://sysdictcoder.com/blog/reflection-with-the-dictionary/">reflection in Dynamics Ax</a>.  In the article I mentioned that it was also possible to modify the AOT and that I&#8217;d give some examples.  Well, I kind of forgot.  Sorry about that.  Todd Hensley was kind enough to remind me of that promise and presented an interesting example problem.</p>
<blockquote><p>
I have a list of tables where I want to set the ClusterIndex to be the same as the PrimaryIndex.</p>
<p>I&#8217;ve been able to loop through an array of the table names, look them up using DictTable, and examine the PrimaryIndex and ClusterIndex values.</p>
<p>But I can&#8217;t figure out how I would say:<br />
myTable.clusterIndex = myTable.primaryIndex;</p>
<p>The properties all appear to be read-only.</p>
<p>Any ideas?
</p></blockquote>
<p>Thank you for the question and, yes, I do have an idea.</p>
<h3>Enter the TreeNode</h3>
<p>The key to solving this is knowing that there is more than one way to do reflection.  As Todd found out, the Dictionary API is read-only.  But the TreeNode API is not.</p>
<p>TreeNode is exactly what its name says: a node in a tree.<br />
I can hear you thinking: &#8220;Nodes? Tree? What are you talking about?&#8221;</p>
<p>I&#8217;m talking about the Application Object Tree.  The AOT has a bunch of nodes and a TreeNode object can represent any one of those nodes, like Forms, a class method or a table.</p>
<p>Whenever you&#8217;re using the AOT, clicking on an object and setting properties you&#8217;re using TreeNodes.  And the best part is you can do it in code too!</p>
<h3>Getting the node</h3>
<p>Enough talk.  Time to show the code.  First you need to get hold of a node.  Let&#8217;s use CustTable as an example.</p>

<div class="wp_syntax"><div class="code"><pre class="xpp">    TreeNode    node = TreeNode::<span style="color: #000000;">findNode</span><span style="color: #000000;">&#40;</span>@<span style="color: #ff0000;">'<span style="color: #000000;">\D</span>ata dictionary<span style="color: #000000;">\T</span>ables<span style="color: #000000;">\C</span>ustTable'</span><span style="color: #000000;">&#41;</span>;
    ;
    info<span style="color: #000000;">&#40;</span>node.<span style="color: #000000;">treeNodePath</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
    info<span style="color: #000000;">&#40;</span>node.<span style="color: #000000;">AOTname</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>As you can see, you need to enter the path similar to the way you open the nodes AOT.  If findNode() fails it will return null.</p>
<p>Once you have the node the fun starts.  In this case I&#8217;m just dumping something in the infolog to show you it works.</p>
<h3>Properties</h3>
<p>Now let&#8217;s do what we really want.  We&#8217;re going to set the primary index as the cluster index.</p>

<div class="wp_syntax"><div class="code"><pre class="xpp">    TreeNode    node = TreeNode::<span style="color: #000000;">findNode</span><span style="color: #000000;">&#40;</span>@<span style="color: #ff0000;">'<span style="color: #000000;">\D</span>ata dictionary<span style="color: #000000;">\T</span>ables<span style="color: #000000;">\C</span>ustTable'</span><span style="color: #000000;">&#41;</span>;
&nbsp;
    <span style="color: #0000ff;">str</span> indexName;
&nbsp;
    #Properties
    ;
&nbsp;
    indexName = node.<span style="color: #000000;">AOTgetProperty</span><span style="color: #000000;">&#40;</span>#PropertyPrimaryIndex<span style="color: #000000;">&#41;</span>;
    node.<span style="color: #000000;">AOTsetProperty</span><span style="color: #000000;">&#40;</span>#PropertyClusterIndex, indexName<span style="color: #000000;">&#41;</span>;
    node.<span style="color: #000000;">AOTsave</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
    node.<span style="color: #000000;">treeNodeRelease</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    node = <span style="color: #0000ff;">null</span>;</pre></div></div>

<p>There are a couple of things to explain here.  First of all the macro #Properties contains the names of all properties you can read and write.  These are the same as what you get in the property window.  Use the macro instead of literal strings, it&#8217;s safer and doesn&#8217;t violate best practices.</p>
<p>Next you can see there is a set of methods to get and set properties.  After modifying anything you need to save the changes, just like you would when changing an AOT object in the property window.  TreeNode works just like doing it manually.</p>
<p>Finally you need to release the memory held by the TreeNode.  The garbage collector cannot clean up TreeNode objects by itself.  It needs a little help from you by calling <em>treeNodeRelease()</em> when you&#8217;re done.  In this example nothing bad would happen if you don&#8217;t.  However, if you run this in a loop a few thousand times Ax will run out of memory.  Not something you really want.</p>
<h3>Fitting it in with DictTable</h3>
<p>Todd already did a lot of work with DictTable to find the tables he needs to change.  And now I tell him DictTable won&#8217;t cut it.    Not really helping.</p>
<p>Not all is lost.  DictTable has a method to get a TreeNode object.  How convenient.  If we change the above example to start with a DictTable object we end up with this:</p>

<div class="wp_syntax"><div class="code"><pre class="xpp">    SysDictTable    dictTable;
    TreeNode        node;
&nbsp;
    <span style="color: #0000ff;">str</span> indexName;
&nbsp;
    #Properties
    ;
&nbsp;
    dictTable = SysDictTable::<span style="color: #000000;">newTableId</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">tableNum</span><span style="color: #000000;">&#40;</span>CustTable<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
    node = dictTable.<span style="color: #000000;">treeNode</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
    indexName = node.<span style="color: #000000;">AOTgetProperty</span><span style="color: #000000;">&#40;</span>#PropertyPrimaryIndex<span style="color: #000000;">&#41;</span>;
    node.<span style="color: #000000;">AOTsetProperty</span><span style="color: #000000;">&#40;</span>#PropertyClusterIndex, indexName<span style="color: #000000;">&#41;</span>;
    node.<span style="color: #000000;">AOTsave</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
    node.<span style="color: #000000;">treeNodeRelease</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    node = <span style="color: #0000ff;">null</span>;</pre></div></div>

<p>This can be adapted to fit in a loop over several tables and Todd&#8217;s problem is solved.</p>
<p>You can do a lot more fun stuff with the TreeNode API.  I&#8217;ll cover that in another installment.  I won&#8217;t forget this time.  I promise :)</p>
<p>Also, if you have questions or ideas for articles don&#8217;t hesitate to <a href="http://sysdictcoder.com/blog/contact/">let me know</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://sysdictcoder.com/blog/changing-properties-of-aot-objects-in-code/feed/</wfw:commentRss>
		</item>
		<item>
		<title>A quick fix for unbalanced TTS</title>
		<link>http://sysdictcoder.com/blog/a-quick-fix-for-unbalanced-tts/</link>
		<comments>http://sysdictcoder.com/blog/a-quick-fix-for-unbalanced-tts/#comments</comments>
		<pubDate>Tue, 29 Jan 2008 13:35:50 +0000</pubDate>
		<dc:creator>Björn</dc:creator>
		
		<category><![CDATA[Debugging]]></category>

		<category><![CDATA[Dynamics Ax (Axapta)]]></category>

		<guid isPermaLink="false">http://sysdictcoder.com/blog/a-quick-fix-for-unbalanced-tts/</guid>
		<description><![CDATA[During development you may encounter one of these.

This usually leaves your Ax session in an unusable state that you can&#8217;t close properly.  However, there&#8217;s no need to get out the big guns and kill the process with the Task Manager.
Instead, if possible, open the AOT and run this job:

static void resetTTS&#40;Args _args&#41;
&#123;
   [...]]]></description>
			<content:encoded><![CDATA[<p>During development you may encounter one of these.<br />
<a href='http://sysdictcoder.com/blog/wp-content/uploads/unbalanced-tts-error.png' title='Unbalanced TTS error'><img src='http://sysdictcoder.com/blog/wp-content/uploads/unbalanced-tts-error.png' alt='Unbalanced TTS error' /></a></p>
<p>This usually leaves your Ax session in an unusable state that you can&#8217;t close properly.  However, there&#8217;s no need to get out the big guns and kill the process with the Task Manager.</p>
<p>Instead, if possible, open the AOT and run this job:</p>

<div class="wp_syntax"><div class="code"><pre class="xpp"><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> resetTTS<span style="color: #000000;">&#40;</span>Args _args<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0000ff;">while</span> <span style="color: #000000;">&#40;</span>appl.<span style="color: #000000;">ttsLevel</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> &gt; <span style="color: #000000;">0</span><span style="color: #000000;">&#41;</span>
        <span style="color: #0000ff;">ttsAbort</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>It simply rolls back any pending transactions until the TTS level is back at zero.  Now, this doesn&#8217;t fix the cause of the problem but it makes life easier trying to iron out the bug.</p>
]]></content:encoded>
			<wfw:commentRss>http://sysdictcoder.com/blog/a-quick-fix-for-unbalanced-tts/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Dealing with containers</title>
		<link>http://sysdictcoder.com/blog/dealing-with-containers/</link>
		<comments>http://sysdictcoder.com/blog/dealing-with-containers/#comments</comments>
		<pubDate>Mon, 14 Jan 2008 11:10:40 +0000</pubDate>
		<dc:creator>Björn</dc:creator>
		
		<category><![CDATA[Dynamics Ax (Axapta)]]></category>

		<category><![CDATA[X++]]></category>

		<guid isPermaLink="false">http://sysdictcoder.com/blog/dealing-with-containers/</guid>
		<description><![CDATA[The X++ container is a powerful and somewhat odd datatype.  It&#8217;s like an array but supports different data types in the same instance and it can grow infinitely large.  Also containers don&#8217;t have methods like real objects; you need to call special functions and pass your container to them.
Many data structures in X++ [...]]]></description>
			<content:encoded><![CDATA[<p>The X++ container is a powerful and somewhat odd datatype.  It&#8217;s like an array but supports different data types in the same instance and it can grow infinitely large.  Also containers don&#8217;t have methods like real objects; you need to call special functions and pass your container to them.</p>
<p>Many data structures in X++ are serialized (packed) into containers before they&#8217;re sent across the network or stored in the database.  The entire SysLastValue framework runs on it.</p>
<p>Unfortunately this kind of flexibility is often abused.  It&#8217;s very easy to abuse containers and I see it happen too often.  In this article I&#8217;ll go over some  good practices for containers.  I won&#8217;t go into basics of containers.  You can find basic documentation on <a href="http://msdn2.microsoft.com/en-us/library/aa856741.aspx">MSDN</a> (methods starting with <em>con</em>).</p>
<h3>Appending to containers</h3>
<p>Most of the time you want to append something to a container.  Typically I come across code like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xpp"><span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>i=<span style="color: #000000;">1</span>; i&lt;=maxNum; i++<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    con = <span style="color: #0000ff;">conIns</span><span style="color: #000000;">&#40;</span>con, i, theValue<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Don&#8217;t do that.  The conIns() function is slow because it copies the original container to a new one before returning.</p>
<p>The above should be rewritten as:</p>

<div class="wp_syntax"><div class="code"><pre class="xpp"><span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>i=<span style="color: #000000;">1</span>; i&lt;=maxNum; i++<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    con += theValue;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>It&#8217;s a lot faster and easier to read as well.</p>
<p>Only use conIns() if there&#8217;s no other option, i.e. for inserting values not at the end.  Even adding elements to the front of the container is slow because the existing container is still copied.  If at all possible write your code in such a way that you only append elements using +=.  This is the fastest way to do it as it modifies the original container in place. And it&#8217;s easier to read as well.</p>
<h3>Initialising</h3>
<p>Whenever you&#8217;re going to insert a fixed set of variables into the container you can add them all at once.  A typical example when writing something to a file.</p>

<div class="wp_syntax"><div class="code"><pre class="xpp"><span style="color: #0000ff;">while</span><span style="color: #000000;">&#40;</span>someCondition<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    con = <span style="color: #0000ff;">conNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    con += <span style="color: #000000;">1</span>;
    con += someValue;
    con += otherValue;
    con += <span style="color: #ff0000;">&quot;X&quot;</span>;
    outFile.<span style="color: #000000;">write</span><span style="color: #000000;">&#40;</span>con<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This is works but there&#8217;s a better way.  You can assign a bunch of values to the container in a single line.  Try this instead:</p>

<div class="wp_syntax"><div class="code"><pre class="xpp"><span style="color: #0000ff;">while</span><span style="color: #000000;">&#40;</span>someCondition<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
     con = <span style="color: #000000;">&#91;</span><span style="color: #000000;">1</span>, someValue, otherValue, <span style="color: #ff0000;">&quot;X&quot;</span><span style="color: #000000;">&#93;</span>;
     outFile.<span style="color: #000000;">write</span><span style="color: #000000;">&#40;</span>con<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This is a contrived example and often the complexity filling the container can be pushed into a separate method.  Usually hiding details like that increases readability of the code.  It isn&#8217;t always possible to do it like this but simplify your code whenever possible.</p>
<h3>Extracting values</h3>
<p>The above trick works the other way around too.  You can write this:</p>

<div class="wp_syntax"><div class="code"><pre class="xpp">a = <span style="color: #0000ff;">conPeek</span><span style="color: #000000;">&#40;</span>con, <span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span>;
b = <span style="color: #0000ff;">conPeek</span><span style="color: #000000;">&#40;</span>con, <span style="color: #000000;">2</span><span style="color: #000000;">&#41;</span>;
c = <span style="color: #0000ff;">conPeek</span><span style="color: #000000;">&#40;</span>con, <span style="color: #000000;">3</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>This can be done in a simpler way:</p>

<div class="wp_syntax"><div class="code"><pre class="xpp"><span style="color: #000000;">&#91;</span>a, b, c<span style="color: #000000;">&#93;</span> = con;</pre></div></div>

<p>Bonus points if you can explain this:</p>

<div class="wp_syntax"><div class="code"><pre class="xpp"><span style="color: #000000;">&#91;</span>a, b<span style="color: #000000;">&#93;</span> = <span style="color: #000000;">&#91;</span>b, a<span style="color: #000000;">&#93;</span>;</pre></div></div>

<h3>Passed by reference</h3>
<p>Containers are a primitive data type in Ax, so they&#8217;re passed by value.  That means containers will be copied when passed as a function argument.  Keep that in mind when you&#8217;re dealing with containers that can become large.  For small containers the performance hit is negligible but at some point you&#8217;re going to notice it.</p>
<h3>Don&#8217;t mimic other data structures with containers</h3>
<p>Too often I see a mess of containers that could be replaced by a much more appropriate data structure.  </p>
<p>Although it&#8217;s very easy and tempting to use containers for everything, again, it&#8217;s not a good idea.  Other developers may have a hard time understanding these ad hoc data structures.  Even understanding your own code can be a problem if you haven&#8217;t worked on it for a while.</p>
<p>There are alternatives to containers.  Take a look at the Foundation Classes.  They&#8217;re somewhat like the .NET collections.  You can use a List, Map, Set, Array and Struct.  They can be packed into containers when necessary.  </p>
<p>If all variables are of the same type, consider using a List (ordered) or Set (unordered).  If you need to track related values use Maps (key/value pairs) or Structs (supports different complex types).  Please don&#8217;t use nested containers or keep values together in several containers at the same index.  It&#8217;s hard to read and easy to break.</p>
]]></content:encoded>
			<wfw:commentRss>http://sysdictcoder.com/blog/dealing-with-containers/feed/</wfw:commentRss>
		</item>
		<item>
		<title>X++ method call performance</title>
		<link>http://sysdictcoder.com/blog/x-method-call-performance/</link>
		<comments>http://sysdictcoder.com/blog/x-method-call-performance/#comments</comments>
		<pubDate>Mon, 24 Dec 2007 12:24:12 +0000</pubDate>
		<dc:creator>Björn</dc:creator>
		
		<category><![CDATA[Dynamics Ax (Axapta)]]></category>

		<category><![CDATA[Performance]]></category>

		<category><![CDATA[X++]]></category>

		<guid isPermaLink="false">http://sysdictcoder.com/blog/x-method-call-performance/</guid>
		<description><![CDATA[Microsoft&#8217;s recommendations for performance optimizations are interesting:  

Calls between tiers (client/server) are slow.
Method calls are expensive. Try to reduce the number of calls. For example, don&#8217;t call the same method several times if you can store a value from the method and instead use that value.


These statements are probably true but as with all [...]]]></description>
			<content:encoded><![CDATA[<p>Microsoft&#8217;s recommendations for <a href="http://msdn2.microsoft.com/EN-US/library/aa597597.aspx">performance optimizations</a> are interesting:  </p>
<blockquote><ul>
<li>Calls between tiers (client/server) are slow.</li>
<li>Method calls are expensive. Try to reduce the number of calls. For example, don&#8217;t call the same method several times if you can store a value from the method and instead use that value.</li>
</ul>
</blockquote>
<p>These statements are probably true but as with all good advice it is important to know the context in which it makes sense.  How slow is slow and how expensive is expensive?  And should you worry about it?  Let&#8217;s find out.</p>
<h2>Test cases</h2>
<p>In X++ there are different kinds of methods and different ways to call them.  There are some interesting cases I can think of right away.</p>
<ul>
<li>Static versus instance methods</li>
<li>Staying on the same tier versus calling methods across tiers</li>
<li>Calling a method on an object of a supertype (Object) instead of the exact subtype</li>
<li>Calling a method through reflection (SysDict* classes)</li>
</ul>
<h2>Test setup</h2>
<p>Methods calls may be slow, but they&#8217;re certainly not slow enough to accurately measure the time of a single call.  Repeating the exact same call a large number of times should average out any measurement errors.</p>
<p>The timing code is basically this:</p>

<div class="wp_syntax"><div class="code"><pre class="xpp">        timer.<span style="color: #000000;">start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>i=<span style="color: #000000;">1</span>; i&lt;=maxLoop; ++i<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #007f00;">// method call here</span>
        <span style="color: #000000;">&#125;</span>
        timer.<span style="color: #000000;">stop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>Note that the loop overhead will be included in the measurement as well.  This is not really a problem because we will use the same setup for all test cases, replacing the loop body.  We&#8217;re not interested in exactly how long it takes to call a method, only in performance of the cases relative to each other.  Absolute execution times depend on the configuration of the system while relative differences don&#8217;t change much between different setups.  It&#8217;s a bit crude but I think it will suffice.</p>
<p>MaxLoop is set at 100,000 and all cases are executed sequentially.  To further minimize the effect of other random system activity, all tests are repeated 20 times.  This means every method call is executed 2 million times.  This should give a pretty good indication of relative performance.</p>
<p>Overhead, e.g. for constructing object, is placed outside the timer statements.  All methods are empty, take no input and return nothing.  That means there&#8217;s nothing but the call going on.  I&#8217;m assuming that the compiler will not perform any optimizations, like inlining functions.  If it does, we should see weird results.</p>
<p>If you want to try this yourself get the full source <a href="http://sysdictcoder.com/blog/wp-content/uploads/class_methodcalltiming.xpo">here</a> (4.0 SP2).</p>
<h2>The results</h2>
<p>After running the test I got these results.<br />
</p>
<table class="wptable rowstyle-alt" id="wptable-2"  cellspacing="1" cellpadding="1">
	<thead>
	<tr>
		<th class="sortable" style="width:20px" align="left">#</th>
		<th class="sortable" style="width:180px" align="left">Case</th>
		<th class="sortable" style="width:100px" align="right">Avg. time (ms) for 100,000 calls</th>
	</tr>
	</thead>
	<tr>
		<td style="width:20px" align="left">1</td>
		<td style="width:180px" align="left">No method (empty loop)</td>
		<td style="width:100px" align="right">481</td>
	</tr>
	<tr class="alt">
		<td style="width:20px" align="left">2</td>
		<td style="width:180px" align="left">Instance method client</td>
		<td style="width:100px" align="right">2,347</td>
	</tr>
	<tr>
		<td style="width:20px" align="left">3</td>
		<td style="width:180px" align="left">Instance method server</td>
		<td style="width:100px" align="right">14,809</td>
	</tr>
	<tr class="alt">
		<td style="width:20px" align="left">4</td>
		<td style="width:180px" align="left">Static method client</td>
		<td style="width:100px" align="right">2,298</td>
	</tr>
	<tr>
		<td style="width:20px" align="left">5</td>
		<td style="width:180px" align="left">Static method server</td>
		<td style="width:100px" align="right">14,825</td>
	</tr>
	<tr class="alt">
		<td style="width:20px" align="left">6</td>
		<td style="width:180px" align="left">SysDictClass</td>
		<td style="width:100px" align="right">3,095</td>
	</tr>
	<tr>
		<td style="width:20px" align="left">7</td>
		<td style="width:180px" align="left">Call on Object on client</td>
		<td style="width:100px" align="right">2,367</td>
	</tr>
</table><p>
</p>
<p>As expected they confirm the Best Practice recommendations.  Calling a method on the same tier in a loop is about 3 times slower than an empty loop.  It doesn&#8217;t really matter if it&#8217;s an instance or static method, or if the variable is of the generic Object type.  The values are too close together to be statistically significant.</p>
<p>SysDictClass is a bit slower.  That makes sense because it uses 2 methods: callObject() on SysDictClass and the method on the object itself.  Surprisingly it is a lot faster than twice the value of calling an object method on the same tier.  It is closer to a combination of no method and a method call on the same tier.  If I had to guess I would say it is because callObject() is a method in the kernel, written in C/C++ instead of X++.  Maybe there is less overhead in dealing with kernel functions.  Too bad we can&#8217;t include an empty kernel method in the test to verify this.</p>
<p>Finally, this proves methods calls across tiers are slow.  Extremely slow compared to anything else.  In this setup crossing a tier is about 6 to 7 times slower than staying on the same tier.  Keep in mind that these methods do not take any input and return nothing.  Also, the AOS and client are on the same machine.  In real code with parameters, return types and network latency it will be worse.</p>
<h2>Conclusion</h2>
<p>The optimization guidelines are correct.  Now should you worry about it and avoid methods?  Generally not.  Breaking up code in methods improves readability of the code.  Adding too many temporary variables to avoid calling the same method again can become annoying too.  Usually a programmer&#8217;s time is more important (and more expensive) than execution time.  It&#8217;s not worth it to optimize everything. </p>
<p>If you stay on the same tier you can do a lot of method calls per second.  Unlike this test setup, real methods have code that takes time to execute as well  The time spent performing the actual method call will be insignificant in most situations.  Readability and clean design trumps performance.  If X++ would support some <a href="http://en.wikipedia.org/wiki/Inline_function">function inlining</a> we could have the best of both worlds with minimal effort.</p>
<p>When crossing from client to server or vice versa, things are more complicated.  As the results show this causes a significant performance hit.  From the start your code should be designed to minimize traffic between tiers.  A single call won&#8217;t hurt but often bad code contains a lot of subsequent calls to objects that live on the other tier, like when constructing an object and setting it up with accessor methods.  In these cases it&#8217;s better to use containers and provide methods to pass all data in a single call.  Simple client/server optimizations can make a huge difference.  Improvements in this area are something users could actually notice themselves.</p>
]]></content:encoded>
			<wfw:commentRss>http://sysdictcoder.com/blog/x-method-call-performance/feed/</wfw:commentRss>
		</item>
		<item>
		<title>A glimpse of version control options for Ax 5.0</title>
		<link>http://sysdictcoder.com/blog/a-glimpse-of-version-control-options-for-ax-50/</link>
		<comments>http://sysdictcoder.com/blog/a-glimpse-of-version-control-options-for-ax-50/#comments</comments>
		<pubDate>Wed, 19 Dec 2007 19:09:48 +0000</pubDate>
		<dc:creator>Björn</dc:creator>
		
		<category><![CDATA[Best practice]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://sysdictcoder.com/blog/a-glimpse-of-version-control-options-for-ax-50/</guid>
		<description><![CDATA[Michael Fruergaard Pontoppidan created a screencast about the version control options for the next major Ax release.  In addition to Visual Sourcesafe, Ax will also support Team Foundation Server and a new source control system in Ax itself.
It looks promising but so was the prospect of Sourcesafe integration for 4.0. Unfortunately it turned out [...]]]></description>
			<content:encoded><![CDATA[<p>Michael Fruergaard Pontoppidan created a <a href="http://blogs.msdn.com/mfp/archive/2007/12/19/version-control-for-everyone.aspx">screencast</a> about the version control options for the next major Ax release.  In addition to Visual Sourcesafe, Ax will also support Team Foundation Server and a new source control system in Ax itself.</p>
<p>It looks promising but so was the prospect of Sourcesafe integration for 4.0. Unfortunately it turned out to be only marginally usable.  The requirement to have a complete database and AOS setup per developer makes it hard to handle multiple projects or do development at the customer&#8217;s site.  </p>
<p>The video seems to acknowledge this.  Sourcesafe and TFS are clearly positioned as tools for ISVs that make modules.  Developing modules is different than doing a customer specific implementation and can more easily incorporate VSS or TFS as part of their toolbox.  Although I&#8217;m skeptical about this, I really hope the new Morphx VCS will provide a decent source control solution for all of us.  Ax needs more tools like that to make good development practices less cumbersome.</p>
]]></content:encoded>
			<wfw:commentRss>http://sysdictcoder.com/blog/a-glimpse-of-version-control-options-for-ax-50/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Installing Dynamics Ax 4.0 SP1 and SP2 side by side</title>
		<link>http://sysdictcoder.com/blog/installing-dynamics-ax-40-sp1-and-sp2-side-by-side/</link>
		<comments>http://sysdictcoder.com/blog/installing-dynamics-ax-40-sp1-and-sp2-side-by-side/#comments</comments>
		<pubDate>Mon, 06 Aug 2007 18:03:02 +0000</pubDate>
		<dc:creator>Björn</dc:creator>
		
		<category><![CDATA[Dynamics Ax (Axapta)]]></category>

		<category><![CDATA[Installation]]></category>

		<guid isPermaLink="false">http://sysdictcoder.com/blog/installing-dynamics-ax-40-sp1-and-sp2-side-by-side/</guid>
		<description><![CDATA[When a new service pack is released I like to install it next to all the other versions I have on my machine.  This makes it easy to test new service packs and compare it to previous versions.  In 3.0 this was quite easy to do because you could choose which directory you [...]]]></description>
			<content:encoded><![CDATA[<p>When a new service pack is released I like to install it next to all the other versions I have on my machine.  This makes it easy to test new service packs and compare it to previous versions.  In 3.0 this was quite easy to do because you could choose which directory you wanted to upgrade.  The new installation procedure and requirements for version 4.0 makes this a lot harder.  Fortunately it&#8217;s not entirely impossible.</p>
<p>The installer doesn&#8217;t give you much choice about what to upgrade, so you&#8217;ll have to copy files and modify registry keys manually.  Below are the steps to make it work <b>on my machine</b>.  Beware that this setup is not something you should do on a production environment because it&#8217;s not supported by MBS.  Don&#8217;t try this if you don&#8217;t feel comfortable editing the Windows registry. </p>
<p>I&#8217;m starting with SP1 because I don&#8217;t have to support environments without a service pack.  This procedure probably works for a regular 4.0 as well.  Everything is installed on a single machine, starting from scratch.  </p>
<ol>
<li>Install SP1 database, client, server, and application.  I used <em>Client</em>, <em>Application</em>, and <em>Server</em>.  Pick a good name for the AOS (I used DAX_401).  Don&#8217;t start it yet.</li>
<li>Copy the three directories. I used <em>Client SP1</em>, <em>Application SP1</em>, and <em>Server SP1</em>.  You should have 6 directories now.</li>
<li>Modify permissions on the SP1 directories to give the account the AOS uses all permissions except for Full Access and Special Permissions.  The default account is Network Service.</li>
<li>Delete the DAX_401 AOS files and install a second SP1 AOS in the <em>Server</em> directory.  You need to do this to get the necessary registry entries.  I named the instance DAX_402.</li>
<li>Install a second SP1 database or copy the one created during the first installation.</li>
<li>Install SP2.  This will upgrade the everything created in step 1.</li>
<li>With the config utilities, create server and client configurations.  At this point the SP2 part should work.</li>
<li>Open Regedit and search for the key <em>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\AOS$01</em>.  This is the service information SP1 AOS.  Change the ImagePath key to point to the executable in <em>Server SP1</em>.  In the Management Console for Windows services the path to the executable for AOS01 should point to the SP1 directory.</li>
<li>In Regedit, go to <em>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Dynamics Server\4.0\01\</em>.  Below this are the server configurations.  Several keys have a path that point to the SP2 directories for application and server.  Change everything to the SP1 directory.</li>
<li>The SP1 AOS should now work.</li>
<li>Optionally, create shortcuts to the SP1 and SP2 clients.  The SP2 client is backward compatible.  It can connect to an SP1 AOS but it the SP2 AOS won&#8217;t accept SP1 clients.</li>
</ol>
<p>I don&#8217;t know if it&#8217;s possible to have 2 versions of the Business connector.  I upgraded mine to SP2.  Like the client it will probably be able to connect to an SP1 application.</p>
<p>If you&#8217;re not interested in using the SP1 binaries you can probably copy an SP1 application to the SP2 Appl directory and configure the AOS to use the old application.  I haven&#8217;t tested it yet so I don&#8217;t know if it actually works.</p>
]]></content:encoded>
			<wfw:commentRss>http://sysdictcoder.com/blog/installing-dynamics-ax-40-sp1-and-sp2-side-by-side/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The lone semicolon</title>
		<link>http://sysdictcoder.com/blog/the-lone-semicolon/</link>
		<comments>http://sysdictcoder.com/blog/the-lone-semicolon/#comments</comments>
		<pubDate>Tue, 31 Jul 2007 09:23:48 +0000</pubDate>
		<dc:creator>Björn</dc:creator>
		
		<category><![CDATA[Dynamics Ax (Axapta)]]></category>

		<category><![CDATA[X++]]></category>

		<guid isPermaLink="false">http://sysdictcoder.com/blog/the-lone-semicolon/</guid>
		<description><![CDATA[Something that confuses and annoys people when they&#8217;re learning X++, is the semicolon after variable declarations.
As you know all local variables have to be declared at the start of the function and statements are placed after that.  Then comes the important part: don&#8217;t forget to always put a semicolon on a single line before [...]]]></description>
			<content:encoded><![CDATA[<p>Something that confuses and annoys people when they&#8217;re learning X++, is the semicolon after variable declarations.</p>
<p>As you know all local variables have to be declared at the start of the function and statements are placed after that.  Then comes the important part: don&#8217;t forget to <b>always put a semicolon on a single line before the first statement.</b></p>
<p>If you don&#8217;t, your code might not compile.  Sometimes it will, sometimes it won&#8217;t.  And code that works now can be broken later, without touching it.  A semicolon is not always required, but instead of worrying if it&#8217;s required, it&#8217;s easier to make a habit of just writing it.  Even if you don&#8217;t have any local variables.  Unfortunately even standard X++ code doesn&#8217;t always have this.</p>
<p>The reason you need that extra semicolon is because the compiler can&#8217;t always see where the variable declarations end.  If you don&#8217;t help a little, it will make a guess.  And it&#8217;s not very good at guessing.</p>
<p>While the compiler is analyzing the code it checks if the first word on a line matches the name of a type (AOT object).  If it&#8217;s a type name the compiler treats the line as a variable declaration.  In this case a variable name should be next.</p>
<p>If the first word is not a variable the compiler considers the line to be the start of a statement.  In some cases it treats statements as class declarations and that causes compiler errors.</p>
<p>Let me explain this with some examples from the standard application (4.0 SP1).  This is a method from the BOMSearch class.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="xpp"><span style="color: #0000ff;">server</span> <span style="color: #0000ff;">static</span> BOMSearch newProdTable<span style="color: #000000;">&#40;</span>ProdTable prodTable<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    BOMSearch BOMSearch = <span style="color: #0000ff;">new</span> BOMSearch<span style="color: #000000;">&#40;</span>
                            prodTable.<span style="color: #000000;">BOMId</span>,
                            prodTable.<span style="color: #000000;">BOMDate</span>,
                            prodTable.<span style="color: #000000;">ItemId</span>,
                            prodTable.<span style="color: #000000;">inventDim</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #000000;">ConfigId</span>,
                            <span style="color: #0000ff;">false</span><span style="color: #000000;">&#41;</span>;
    ;
    BOMSearch.<span style="color: #000000;">init</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
    <span style="color: #0000ff;">return</span> BOMSearch;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>If you remove the semicolon from line 9 you will get an error on the next line.  The first line is never a problem for the compiler: a variable BOMSearch of type BOMSearch is created.  </p>
<p>Next the compiler sees the word BOMSearch and figures, <em>&#8216;I know that, it&#8217;s a class name.  So this must be a variable declaration.  But what&#8217;s with the .init()?  That&#8217;s not allowed here.&#8217;</em></p>
<p>And there&#8217;s the compilation error.</p>
<p>This method from CustTable is an example of bad standard code.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="xpp"><span style="color: #0000ff;">void</span> initFromCustGroup<span style="color: #000000;">&#40;</span>CustGroup _custGroup<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    this.<span style="color: #000000;">PaymTermId</span> = _custGroup.<span style="color: #000000;">PaymTermId</span>;
    this.<span style="color: #000000;">PaymSched</span>  = PaymTerm::<span style="color: #000000;">find</span><span style="color: #000000;">&#40;</span>this.<span style="color: #000000;">PaymTermId</span><span style="color: #000000;">&#41;</span>.<span style="color: #000000;">PaymSched</span>;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><em>&#8216;What do you mean, bad code? This is standard Axapta.  It works just fine!&#8217;.</em><br />
Yes it does&#8230; for now.  Create an EDT named <em>this</em> and compile again.  It&#8217;s broken for the same reason as the previous example.</p>
<p>To avoid headaches, repeat after me: <b>always put a semicolon on a single line before the first statement.</b></p>
<p>Too bad the compiler isn&#8217;t smarter.  A Best Practice check would be nice too.</p>
]]></content:encoded>
			<wfw:commentRss>http://sysdictcoder.com/blog/the-lone-semicolon/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The X++ way to getters and setters</title>
		<link>http://sysdictcoder.com/blog/the-x-way-to-getters-and-setters/</link>
		<comments>http://sysdictcoder.com/blog/the-x-way-to-getters-and-setters/#comments</comments>
		<pubDate>Mon, 16 Jul 2007 10:15:05 +0000</pubDate>
		<dc:creator>Björn</dc:creator>
		
		<category><![CDATA[Best practice]]></category>

		<category><![CDATA[Dynamics Ax (Axapta)]]></category>

		<category><![CDATA[X++]]></category>

		<guid isPermaLink="false">http://sysdictcoder.com/blog/the-x-way-to-getters-and-setters/</guid>
		<description><![CDATA[This one is for all those who are new to X++ development.  When learning OO programming we&#8217;re told that encapsulation is important.  Data members (variables) should be hidden and any access should go through methods.  Then the typical combination of getters and setters is introduced.  You&#8217;re supposed to write methods for [...]]]></description>
			<content:encoded><![CDATA[<p>This one is for all those who are new to X++ development.  When learning OO programming we&#8217;re told that encapsulation is important.  Data members (variables) should be hidden and any access should go through methods.  Then the typical combination of getters and setters is introduced.  You&#8217;re supposed to write methods for each thing you want to expose: getSomething() and setSomething(someValue).  </p>
<p>In X++ we do this too&#8230; with a twist.  For starters, all data members of a class (variables in the ClassDeclaration) are protected.  So only the class itself and subclasses can access them directly.  This means X++ forces you to write methods to access data from outside the class hierarchy.  Unlike other mainstream languages such as Java, C# or C++ there is nothing you can do about it.  Keywords such as private, protected or public are simply not allowed in a ClassDeclaration.</p>
<p>And there&#8217;s more, there are no getters and setters either.  It&#8217;s not that writing getters and setters is impossible, it&#8217;s just not the way things are done.  Everything is rolled into a single method, with a name starting with <em>parm</em>.  Sometimes they&#8217;re called property methods or parm-methods.</p>
<p>A typical example looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xpp">CustAccount parmCustAccount<span style="color: #000000;">&#40;</span>CustAccount _custAccount = custAccount<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    ;
    custAccount = _custAccount;
    <span style="color: #0000ff;">return</span> custAccount;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>It may look confusing but it is actually quite simple.  If you use it as a setter, the new value is passed to the object&#8217;s data member (custAccount) and the return value is never used.  When used as a getter, _custAccount gets the current data member as a default value and this value is eventually returned.</p>
<p>The important thing is that you can use it just like getters and setters you might know.  Instead of getCustAccount(), use parmCustAccount().  Instead of setCustAccount(&#8217;1234&#8242;), use parmCustAccount(&#8217;1234&#8242;).</p>
<p>I recommend you do it this way for any new classes you create.  This is something I need to point out in code reviews with new X++ developers all the time.  You could argue that the function does two things and in theory it should only do one thing.  That&#8217;s true.  However, this is a minor offense that will not make or break your application.  Adhering to the existing code style is important too.  Being consistent improves overall readability and usability of the code.  When in Rome do as the Romans do.</p>
]]></content:encoded>
			<wfw:commentRss>http://sysdictcoder.com/blog/the-x-way-to-getters-and-setters/feed/</wfw:commentRss>
		</item>
		<item>
		<title>A quick tip for enums</title>
		<link>http://sysdictcoder.com/blog/a-quick-tip-for-enums/</link>
		<comments>http://sysdictcoder.com/blog/a-quick-tip-for-enums/#comments</comments>
		<pubDate>Wed, 11 Jul 2007 06:30:41 +0000</pubDate>
		<dc:creator>Björn</dc:creator>
		
		<category><![CDATA[Best practice]]></category>

		<category><![CDATA[Dynamics Ax (Axapta)]]></category>

		<category><![CDATA[X++]]></category>

		<guid isPermaLink="false">http://sysdictcoder.com/blog/a-quick-tip-for-enums/</guid>
		<description><![CDATA[When you&#8217;re creating a new base enum consider using multiples of 10 for values.  This makes extending enums a lot easier, especially if it indicates some kind of status.  Those of you who once programmed in BASIC, or any other language with line numbers, can probably guess where I&#8217;m going with this.  [...]]]></description>
			<content:encoded><![CDATA[<p>When you&#8217;re creating a new base enum consider using multiples of 10 for values.  This makes extending enums a lot easier, especially if it indicates some kind of status.  Those of you who once programmed in BASIC, or any other language with line numbers, can probably guess where I&#8217;m going with this.  My apologies for any painful flashbacks this may trigger.</p>
<p>Suppose you&#8217;re creating an interface with another system.  It turns out you need a new enum, MessageStatus.  Values are assigned automatically, so Received = 0, Started = 1, Processed = 2, and Archived = 3.</p>
<p>Of course, at some point you need to do different things depending on the status.  Let&#8217;s say you have written this function.</p>

<div class="wp_syntax"><div class="code"><pre class="xpp"><span style="color: #0000ff;">boolean</span> isOpen<span style="color: #000000;">&#40;</span>MessageStatus _status<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0000ff;">return</span> _status &lt; MessageStatus::<span style="color: #000000;">Processed</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This is a simple way to indicate at which point the message is no longer considered open.  Standard Dynamics Ax contains similar examples, e.g. with the inventory status (StatusIssue and StatusReceipt).</p>
<p>Your message handling system goes live and works perfectly.  Users are happy, birds are singing, the sun is shining.  </p>
<p>After a while users decide they need a verification step before moving to Started.  You extend the enum and end up with MessageStatus::Verified = 4.  And now things are broken.  Verified messages aren&#8217;t handled correctly.</p>
<p>You need to check all places where the status is used.  You run across the isOpen() method and change it.</p>

<div class="wp_syntax"><div class="code"><pre class="xpp"><span style="color: #0000ff;">boolean</span> isOpen<span style="color: #000000;">&#40;</span>MessageStatus _status<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0000ff;">return</span> _status &lt; MessageStatus::<span style="color: #000000;">Processed</span> || _status == MessageStatus::<span style="color: #000000;">Verified</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Not so readable anymore but it works.  For now.  Things can get out of hand quickly if more than 1 status is added.</p>
<p>This could have been avoided from the start if the enum elements were assigned specific values.</p>
<table>
<tr>
<td>Received</td>
<td>10</td>
</tr>
<tr>
<td>Started</td>
<td>20</td>
</tr>
<tr>
<td>Processed</td>
<td>30</td>
</tr>
<tr>
<td>Archived</td>
<td>40</td>
</tr>
</table>
<p></p>
<p>Adding a new step somewhere in the middle is easy.  Create Verified with value 15 and isOpen() doesn&#8217;t need to be modified.</p>
<p>Now you may think this is a great idea (It is.  Thank you.) but don&#8217;t go changing all your enums now.  Changing values means you also need to modify all fields that use it, as they still contain the old numbers.  This is the reason you need to think about enum values in advance.  Once a system is deployed, it&#8217;s usually better to leave these things as they are to avoid even more trouble.</p>
<p>In practice starting with multiples of 10 is enough to deal with any reasonable modifications.  I have yet to run into a situation where a gap of 9 values isn&#8217;t enough.  Unfortunately standard Dynamics Ax enums have continuous values starting at 0.  If you need to insert something in between you still have to check all code that covers a range of enum elements.</p>
]]></content:encoded>
			<wfw:commentRss>http://sysdictcoder.com/blog/a-quick-tip-for-enums/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Inventory marking issue in Axapta 3.0</title>
		<link>http://sysdictcoder.com/blog/inventory-marking-issue-in-axapta-30/</link>
		<comments>http://sysdictcoder.com/blog/inventory-marking-issue-in-axapta-30/#comments</comments>
		<pubDate>Sat, 07 Jul 2007 10:54:18 +0000</pubDate>
		<dc:creator>Björn</dc:creator>
		
		<category><![CDATA[Bug]]></category>

		<category><![CDATA[Dynamics Ax (Axapta)]]></category>

		<guid isPermaLink="false">http://sysdictcoder.com/blog/inventory-marking-issue-in-axapta-30/</guid>
		<description><![CDATA[There&#8217;s a problem when modifying marked transactions.  It&#8217;s possible to end up with a transaction that is not reserved but has a reference lot ID anyway, i.e. is marked to another transaction.  This can lead to hard to explain problems down the line, e.g. after running the master planning planned orders are missing [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s a problem when modifying marked transactions.  It&#8217;s possible to end up with a transaction that is not reserved but has a reference lot ID anyway, i.e. is marked to another transaction.  This can lead to hard to explain problems down the line, e.g. after running the master planning planned orders are missing or incorrectly matched to outgoing transactions.</p>
<h2>Scenario</h2>
<p>There is an easy way to trigger this.  Create a sales order for an item.  Then create a linked purchase order for the amount sold.  This marks both inventory transactions to each other.  The inventory transaction for the sales line issue is in status reserved ordered and contains the reference lot ID of the purchase order line.</p>
<p>Now increase the quantity on the sales line.  This creates a second inventory transaction for the new quantity in issue status on order and with the same reference lot ID.  This means the new quantity is also marked to the same purchase order.  This is obviously not correct.  </p>
<p>Marking implies reserved transactions (physically or ordered) and the quantities on both sides should match.  In this case the purchase order is not sufficient for the quantity sold.  That&#8217;s why the new transaction is put in status on order in the first place.  Unfortunately the marking is not fixed.</p>
<p>Something similar happens when you increase the purchase quantity instead of the sales quantity.</p>
<h2>Fix</h2>
<p>This behavior is fixed in Dynamics Ax 4.0 SP1.  And the good news is it can be backported. The bug resides in InventUpd_Estimated.createEstimatedInventTrans().  This is where a new InventTrans record is created for the added quantity.  It correctly determines the issue status and then stops.  In 4.0 the marking is checked there as well.</p>
<p>The end of the method looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>63
64
65
66
</pre></td><td class="code"><pre class="xpp"><span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>movement_Orig<span style="color: #000000;">&#41;</span>
    inventTrans.<span style="color: #000000;">updateSumUp</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
updEstimated += qty;</pre></td></tr></table></div>

<p>Replace it with this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
</pre></td><td class="code"><pre class="xpp">updEstimated += qty;
&nbsp;
<span style="color: #007f00;">// Fix 4.0 SP1 &gt;&gt;</span>
<span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>movement.<span style="color: #000000;">inventRefTransId</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #007f00;">// Marking for entire lotId exists =&gt; additional should also be marked</span>
<span style="color: #000000;">&#123;</span>
    markNow = InventTrans::<span style="color: #000000;">updateMarking</span><span style="color: #000000;">&#40;</span>movement.<span style="color: #000000;">inventRefTransId</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, movement.<span style="color: #000000;">transId</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, -qty,  <span style="color: #ff0000;">''</span>, SortOrder::<span style="color: #000000;">Descending</span><span style="color: #000000;">&#41;</span>;
&nbsp;
    <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>markNow<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0000ff;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">abs</span><span style="color: #000000;">&#40;</span>markNow<span style="color: #000000;">&#41;</span> &lt; <span style="color: #0000ff;">abs</span><span style="color: #000000;">&#40;</span>inventTrans.<span style="color: #000000;">Qty</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            inventTrans.<span style="color: #000000;">updateSplit</span><span style="color: #000000;">&#40;</span>inventTrans.<span style="color: #000000;">Qty</span> &gt; <span style="color: #000000;">0</span> ? <span style="color: #0000ff;">abs</span><span style="color: #000000;">&#40;</span>markNow<span style="color: #000000;">&#41;</span> : - <span style="color: #0000ff;">abs</span><span style="color: #000000;">&#40;</span>markNow<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
        inventTrans.<span style="color: #000000;">InventRefTransId</span> =  movement.<span style="color: #000000;">inventRefTransId</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
        inventTrans.<span style="color: #000000;">update</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
        InventTrans::<span style="color: #000000;">findTransId</span><span style="color: #000000;">&#40;</span>inventTrans.<span style="color: #000000;">InventRefTransId</span>, <span style="color: #0000ff;">true</span><span style="color: #000000;">&#41;</span>.<span style="color: #000000;">updateSumUp</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>qty &lt; <span style="color: #000000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #007f00;">// issue</span>
    <span style="color: #000000;">&#123;</span>
        InventUpd_Reservation::<span style="color: #000000;">updateReserveRefTransId</span><span style="color: #000000;">&#40;</span>movement<span style="color: #000000;">&#41;</span>;   <span style="color: #007f00;">// try to make reservation according to marking -</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0000ff;">else</span>
    <span style="color: #000000;">&#123;</span>
        inventTransMovement = InventTrans::<span style="color: #000000;">findTransId</span><span style="color: #000000;">&#40;</span>movement.<span style="color: #000000;">inventRefTransId</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
        <span style="color: #0000ff;">if</span><span style="color: #000000;">&#40;</span>inventTransMovement<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            movementIssue = inventTransMovement.<span style="color: #000000;">inventMovement</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">true</span><span style="color: #000000;">&#41;</span>;  <span style="color: #007f00;">// no Throw if not initiated</span>
&nbsp;
            <span style="color: #0000ff;">if</span><span style="color: #000000;">&#40;</span>movementIssue<span style="color: #000000;">&#41;</span>
                InventUpd_Reservation::<span style="color: #000000;">updateReserveRefTransId</span><span style="color: #000000;">&#40;</span>movementIssue<span style="color: #000000;">&#41;</span>;  <span style="color: #007f00;">// try to make reservation according to marking -</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>!markNow &amp;&amp; inventTrans.<span style="color: #000000;">InventRefTransId</span><span style="color: #000000;">&#41;</span> <span style="color: #007f00;">// reset InventRefTransId if no marking could be made</span>
    <span style="color: #000000;">&#123;</span>
        inventTrans.<span style="color: #000000;">InventRefTransId</span> = <span style="color: #ff0000;">''</span>;
        inventTrans.<span style="color: #000000;">update</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #007f00;">// &lt;&lt;</span>
&nbsp;
<span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>movement_Orig<span style="color: #000000;">&#41;</span>
    inventTrans.<span style="color: #000000;">updateSumUp</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</pre></td></tr></table></div>

<p>You&#8217;ll also need to add these variables at the top:</p>

<div class="wp_syntax"><div class="code"><pre class="xpp">InventQty           markNow;
InventMovement      movementIssue;
InventTrans         inventTransMovement;</pre></div></div>

<h2>Final remarks</h2>
<p>I could reproduce this on a standard 3.0 SP5 installation.  I was a bit surprised to see that this has gone unnoticed for so long.  However it is a subtle bug that is hard to spot.  The transaction status is correct and the effects of marking aren&#8217;t immediately visible.  </p>
<p>This kind of inconsistency can really mess up the InventTrans table.  Without the fix try, increasing the purchase quantity and then on the sales order remove the inventory marking completely.  You end up with several inventory transactions for the purchase order.  Some of which are still marked to the sales order.</p>
]]></content:encoded>
			<wfw:commentRss>http://sysdictcoder.com/blog/inventory-marking-issue-in-axapta-30/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
