{"id":338,"date":"2010-06-18T14:05:54","date_gmt":"2010-06-18T21:05:54","guid":{"rendered":"http:\/\/45.63.48.66\/?p=338"},"modified":"2010-06-18T15:03:27","modified_gmt":"2010-06-18T22:03:27","slug":"evaluate-formulas-in-dynamics-nav-2","status":"publish","type":"post","link":"https:\/\/gotcal.com\/index.php\/2010\/06\/evaluate-formulas-in-dynamics-nav-2\/","title":{"rendered":"Evaluate Formulas in Dynamics NAV"},"content":{"rendered":"<p>ERP software knows math, or do they? <img loading=\"lazy\" decoding=\"async\" class=\"alignright size-medium wp-image-323\" title=\"061510_2223_EvaluateFor1.jpg\" src=\"http:\/\/45.63.48.66\/wp-content\/uploads\/2010\/06\/061510_2223_EvaluateFor1-300x221.jpg\" alt=\"\" width=\"300\" height=\"221\" srcset=\"https:\/\/gotcal.com\/wp-content\/uploads\/2010\/06\/061510_2223_EvaluateFor1-300x221.jpg 300w, https:\/\/gotcal.com\/wp-content\/uploads\/2010\/06\/061510_2223_EvaluateFor1.jpg 649w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/>Dynamics NAV can do simple arithmetic in a decimal field, eg. type &#8220;14*50+10&#8221; and the system will evaluate this to the correct result &#8220;710&#8221;. But if you start nesting your expression with parenthesis it gets a bit confused and tells you &#8220;Please specify a decimal or calculation expression&#8221;. In certain scenarios, you will require complex setups for eg. item pricing or salesperson commissions, that is defined by a formula with certain constants in it (read: other field values).<\/p>\n<p><!--more--><\/p>\n<p>I have as well as others, previously constructed a custom function to handle this within NAV, that evaluates a formula breaking it down to single expressions and calculating the result of this. As some of the other implementations, that are available on <a href=\"http:\/\/mibuso.com\/downloads.asp?category=none\">MIBUSO<\/a>, like <a href=\"http:\/\/mibuso.com\/dlinfo.asp?FileID=820\">Calc by Yaroslav Gaponov<\/a> and <a href=\"http:\/\/mibuso.com\/dlinfo.asp?FileID=782\">Mathematical Expression Parser by Justas Janauskas<\/a>, they usually have certain scenarios where they fail. Or you have to format your expression a certain way for it to work. My scenario was a simple expression like &#8220;20*-10&#8221; that failed. As it expected to have a value between each operator it saw it like this &#8220;20 * 0 &#8211; 10&#8221; which obviously isn&#8217;t correct. I did fix the code i had, but at the same time i found a much easier solution for the problem.<\/p>\n<p>In the programming languages VB and Java you have a function called Eval(). It can take an expression, and evaluate it into a decimal value. With automation in Dynamics NAV you have access to this via the automation control<strong> &#8216;Microsoft Script Control 1.0&#8217;.ScriptControl<\/strong> (<em>GUID: {0E59F1D2-1FBE-11D0-8FF2-00A0D10038BC}<\/em>). Here is the few lines of code needed:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nEvaluateFormula(Formula : Text&#x5B;1000]) : Decimal\r\nCREATE(MSScriptControl);\r\nMSScriptControl.Language('VBScript');\r\nResult := MSScriptControl.Eval(Formula);\r\nCLEAR(MSScriptControl);\r\n<\/pre>\n<p>in addition with a small function to insert parameters you now have a powerful function to do calculations:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nInsertVariableValue(VAR Formula : Code&#x5B;250];VariableName : Code&#x5B;30];VariableValue : Decimal)\r\nWHILE STRPOS(Formula,VariableName) &lt;&gt; 0 DO BEGIN\r\n  Pos := STRPOS(Formula,VariableName);\r\n  Formula := DELSTR(Formula,Pos,STRLEN(VariableName));\r\n  Formula := INSSTR(Formula,FORMAT(VariableValue),Pos);\r\nEND;\r\n<\/pre>\n<p>So lets take at an example formula for Commission, in this scenario it uses fields from Item Ledger Entry and Functions:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n((&#x5B;SALES AMOUNT (ACTUAL)]+{NETFREIGHTBYAMOUNT}-{COMMCOST})-(&#x5B;SALES AMOUNT (ACTUAL)]+{FREIGHTBYAMOUNT})*.005)*{RATE}\r\n<\/pre>\n<p>Fields in [] are fields from the Item Ledger Entry table that are filled with RecRef functionality, and the fields in {} are functions and\/or constants. The functionality should be very easy to extend, and as i just wanted to introduce the Eval() function, this article does not dwelve deeper into specifics. Enjoy.<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>ERP software knows math, or do they? Dynamics NAV can do simple arithmetic in a decimal field, eg. type &#8220;14*50+10&#8221; and the system will evaluate this to the correct result &#8220;710&#8221;. But if you start nesting your expression with parenthesis it gets a bit confused and tells you &#8220;Please specify a decimal or calculation expression&#8221;. [&hellip;]<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[30,77,31,32,33],"class_list":["post-338","post","type-post","status-publish","format-standard","hentry","category-navision","tag-automation","tag-navision","tag-formula","tag-math","tag-script"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/gotcal.com\/index.php\/wp-json\/wp\/v2\/posts\/338","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gotcal.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gotcal.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gotcal.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/gotcal.com\/index.php\/wp-json\/wp\/v2\/comments?post=338"}],"version-history":[{"count":22,"href":"https:\/\/gotcal.com\/index.php\/wp-json\/wp\/v2\/posts\/338\/revisions"}],"predecessor-version":[{"id":372,"href":"https:\/\/gotcal.com\/index.php\/wp-json\/wp\/v2\/posts\/338\/revisions\/372"}],"wp:attachment":[{"href":"https:\/\/gotcal.com\/index.php\/wp-json\/wp\/v2\/media?parent=338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gotcal.com\/index.php\/wp-json\/wp\/v2\/categories?post=338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gotcal.com\/index.php\/wp-json\/wp\/v2\/tags?post=338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}