
{"id":33,"date":"2017-04-04T04:39:20","date_gmt":"2017-04-04T04:39:20","guid":{"rendered":"http:\/\/bakke.online\/?p=33"},"modified":"2017-04-04T04:39:20","modified_gmt":"2017-04-04T04:39:20","slug":"another-form-trick-copy-down-in-grids","status":"publish","type":"post","link":"https:\/\/www.bakke.online\/index.php\/2017\/04\/04\/another-form-trick-copy-down-in-grids\/","title":{"rendered":"Another form trick, copy-down in grids"},"content":{"rendered":"<p>A function I have often been asked for, but which does not exist in standard Dynamics AX 2012 is copy-down, or the ability to press a key on the keyboard to copy values into the currently selected field from the row immediately above it. \u00a0In my implementation, I have chosen the F11 key, as it was not used for anything else.<\/p>\n<p>The function will be available on all grid-based forms, even in the table browser.<\/p>\n<p><!--more-->To implement this, modify the SysSetupFormRun class to add a new methods called EB_copyDown. \u00a0(As usual, the prefix is up to you and the naming conventions you follow)<\/p>\n<pre><em><span style=\"color: #008000;\">\/\/ EB 11\/03\/2016: Enable copy-down on F11 key<\/span><\/em>\n<span style=\"color: #0000ff;\"><strong>private<\/strong> <strong>void<\/strong><\/span> EB_copyDown( FormDataSource _fds, FieldId _fieldId )\n{\n  Common prevRecord = _fds.cursor( _fds.getPosition() - <strong><span style=\"color: #ff0000;\">1<\/span><\/strong> );\n  Common common = _fds.cursor();\n\n  common.( _fieldId ) = prevRecord.( _fieldId );\n}<\/pre>\n<p>This sets prevRecord to the record immediately above\u00a0the current record, by subtracting one from the value returned by getPosition(), and\u00a0sets common to the current record. \u00a0Next, it just copies the requested field from one record to the other. \u00a0(This is an example of late-binding fields, or dynamic binding.)<\/p>\n<p>In order for this to work, I&#8217;ll need to modify the task() method as well.<br \/>\nI start by adding a number of local variables I&#8217;ll need.<\/p>\n<pre><span style=\"color: #008000;\"><em> \/\/ EB 11\/03\/2016: Enable copy-down on F11 key ==&gt;<\/em><\/span>\n FieldBinding binding;\n FormDataSource fds;\n FormControl ctrl;\n FieldId fieldId;\n <strong><span style=\"color: #0000ff;\">container<\/span> <\/strong>dependentFields;\n <strong><span style=\"color: #0000ff;\">int<\/span> <\/strong>i;\n Object fds_Object;\n FormDataObject o;\n <span style=\"color: #008000;\"><em>\/\/ EB 11\/03\/2016: Enable copy-down on F11 key &lt;==<\/em><\/span><\/pre>\n<p>Then we add an else-if branch to the if-statement.<\/p>\n<pre><span style=\"color: #008000;\"><em>\/\/ EB 11\/03\/2016: Enable copy-down on F11 key ==&gt;<\/em><\/span>\n<strong><span style=\"color: #0000ff;\">else if<\/span><\/strong>( _p1 == <span style=\"color: #ff0000;\"><strong>357<\/strong> <\/span>) <span style=\"color: #008000;\"><em>\/\/ F11<\/em><\/span>\n{\n  ctrl = this.selectedControl();\n  <strong><span style=\"color: #0000ff;\">if<\/span><\/strong>( ( ctrl != <strong><span style=\"color: #0000ff;\">null<\/span> <\/strong>) &amp;&amp; ctrl.enabled() &amp;&amp; ctrl.allowEdit() )\n  {\n    binding = ctrl.fieldBinding();\n    fieldId = binding.fieldId();\n    fds = ctrl.dataSourceObject();\n\n    <strong><span style=\"color: #0000ff;\">if<\/span><\/strong>( fds.getPosition() &gt; <strong><span style=\"color: #ff0000;\">1<\/span><\/strong> )\n    {\n      this.lock();\n      o = fds.object( fieldId );\n      <span style=\"color: #0000ff;\"><strong>if<\/strong><\/span>( o.allowEdit() )\n      {\n        fds_Object = fds <strong><span style=\"color: #0000ff;\">as<\/span> <\/strong>Object;\n        this.EB_copyDown( fds, fieldId );\n        <span style=\"color: #0000ff;\"><strong>if<\/strong><\/span>( formDataSourceHasMethod( fds, <strong><span style=\"color: #0000ff;\">identifierStr<\/span><\/strong>( EB_dependentFields ) ) )\n        {\n          dependentFields = fds_Object.EB_dependentFields( fieldId );\n        }\n        <span style=\"color: #0000ff;\"><strong>for<\/strong><\/span>( i = <strong><span style=\"color: #ff0000;\">1<\/span><\/strong>; i &lt;= <span style=\"color: #0000ff;\"><strong>conlen<\/strong><\/span>( dependentFields ); i++ )\n        {\n          this.EB_copyDown( fds, <span style=\"color: #0000ff;\"><strong>conPeek<\/strong><\/span>( dependentFields, i ) );\n        }\n\n        o.modified();\n        <span style=\"color: #0000ff;\"><strong>for<\/strong><\/span>( i = <span style=\"color: #ff0000;\"><strong>1<\/strong><\/span>; i &lt;= <span style=\"color: #0000ff;\"><strong>conlen<\/strong><\/span>( dependentFields ); i++ )\n        {\n          o = fds.object( fieldId );\n          o.modified();\n        }\n      }\n      this.unLock();\n      fds.refresh();\n    }\n    <span style=\"color: #0000ff;\"><strong>else<\/strong><\/span>\n    {\n      warning( <span style=\"color: #993300;\">\"Not copying down. You had to try, didn't you?\"<\/span> );\n    }\n  }\n}\n<span style=\"color: #008000;\"><em>\/\/ EB 11\/03\/2016: Enable copy-down on F11 key &lt;==<\/em><\/span><\/pre>\n<p>I&#8217;m checking to make sure that the user is not on the first row in the grid. \u00a0If he\/she is, I show a suitable message. \u00a0Of course, sensibilities and political correctness may mandate that you don&#8217;t show what I did. \u00a0I will not be held responsible for any consequences if you do&#8230;<\/p>\n<p>Some fields will depend on others. \u00a0An example may be a subcategory, which depends on a major category. \u00a0When copying the subcategory, it would be useful to also copy the major category, so\u00a0I have added support for also copying such dependent fields. \u00a0To do this, add a form-level method to the relevant form(s). \u00a0The method should be called EB_dependentFields and take a fieldId as single parameter. \u00a0It will return a container filled with the fieldIds of all other fields that also need to be copied. \u00a0You can change the name of the method to suit whatever naming convention you follow, just make sure that the method is public and follows the API described here. \u00a0Also, make sure the code above is updated to match the method name you choose.<\/p>\n<p>As usual, an XPO containing these changes are available here:\u00a0<a href=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/Class_SysSetupFormRun-2.zip\">Class_SysSetupFormRun (2).zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A function I have often been asked for, but which does not exist in standard Dynamics AX 2012 is copy-down, or the ability to press a key on the keyboard to copy values into the currently selected field from the row immediately above it. \u00a0In my implementation, I have chosen the F11 key, as it &hellip; <a href=\"https:\/\/www.bakke.online\/index.php\/2017\/04\/04\/another-form-trick-copy-down-in-grids\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Another form trick, copy-down in grids&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,1],"tags":[2,3,5],"class_list":["post-33","post","type-post","status-publish","format-standard","hentry","category-dynamics-ax","category-uncategorized","tag-ax2012r3","tag-dynamics-ax","tag-user-interface"],"_links":{"self":[{"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/posts\/33","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/comments?post=33"}],"version-history":[{"count":0,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/posts\/33\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/media?parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/categories?post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/tags?post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}