{"id":872,"date":"2020-08-07T13:31:27","date_gmt":"2020-08-07T11:31:27","guid":{"rendered":"http:\/\/daxvisionerp.com\/?p=872"},"modified":"2025-10-22T15:01:04","modified_gmt":"2025-10-22T15:01:04","slug":"passing-a-record-and-filtering-the-query","status":"publish","type":"post","link":"https:\/\/daxvisionerp.com\/home\/passing-a-record-and-filtering-the-query\/","title":{"rendered":"Passing a record and filtering the query"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Task<\/h2>\n\n\n\n<p>Passing a record from &#8220;<em>MCROrderNotes<\/em>&#8221; form and filtering the query by it on &#8220;<em>SysOutgoingEmailTable<\/em>&#8221; form.<\/p>\n\n\n\n<p>Since both of the objects were standard forms, I had to use extensions.<\/p>\n\n\n\n<p>That is why passing a record by code slightly changed since the previous versions of Dynamics AX. <br>However, passing it by a menu item button stayed the same as before.<\/p>\n\n\n\n<p>To get the record and filter the query by it also got modified a bit. <br>You cannot use the &#8220;element&#8221; related methods, because you are writing the code in classes, instead of form methods.<br>For classes you have the help of the &#8220;sender&#8221; parameter and the FormRun class to achieve the desired functionality.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Complication<\/h2>\n\n\n\n<p>Modifying a standard form in D365 F&amp;O is simple. You just have to create an extension and you can add more datasources and form controls. Adding or modifying methods is a bit more complicated.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Solution<\/h2>\n\n\n\n<p>For that, you have to create a class. <br>It is advised to name it the same as the form with the model&#8217;s name somewhere in it and an \u201c_Extension\u201d postfix. <br>You have to specify that it is an extension of a form. Also, the class must be \u201cfinal\u201d.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Passing a record<\/h3>\n\n\n\n<p>I created a menu item button. I filled out the Data Source property with the DocuRef table &#8211; since I want to pass the RefRecId and RefTableId fields. Note that the DocuRef table is the data source of the form.<\/p>\n\n\n\n<p>With a menu item button, passing a record is that easy.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img decoding=\"async\" src=\"http:\/\/daxvisionerp.com\/wp-content\/uploads\/2020\/08\/image.png\" alt=\"\" class=\"wp-image-932\"\/><figcaption class=\"wp-element-caption\">Pass DocuRef record<\/figcaption><\/figure>\n\n\n\n<p><strong>Alternatively, you can also pass a record by code:<\/strong><\/p>\n\n\n\n<p>After creating the class, copy the event handler method that you want to subscribe to. There are event handler methods for a lot of scenarios. Though, there are not as many as overridable methods.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"http:\/\/daxvisionerp.com\/wp-content\/uploads\/2020\/04\/image-1.png\" alt=\"This image has an empty alt attribute; its file name is image-1.png\"\/><figcaption class=\"wp-element-caption\"> Copy OnClicked event handler method <\/figcaption><\/figure>\n\n\n\n<p>I pasted my button\u2019s OnClicked method to the class and added the logic.<br>It opens the SysOutgoingEmailTable form and passes the current DocuRef table record.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[ExtensionOf(formstr(MCROrderNotes))]\nfinal class MCROrderNotes_Extension\n{\n    \/\/\/ <summary>\n    \/\/\/\n    \/\/\/ <\/summary>\n    \/\/\/ \n    \/\/\/ \n    [FormControlEventHandler(formControlStr(MCROrderNotes, \n        OutgoingEmailBtn), FormControlEventType::Clicked)]\n\n    public static void OutgoingEmailBtn_OnClicked(FormControl sender, \n        FormControlEventArgs e)\n    {\n        FormRun fr = sender.formRun();\n        FormDataSource docuRef_ds = \n            fr.dataSource(formDataSourceStr(MCROrderNotes, DocuRef));\n        DocuRef docuRef = docuRef_ds.cursor();\n\n        Args args = new Args();\n        args = fr.args();\n        args.record(docuRef);\n        new MenuFunction(menuItemDisplayStr(SysOutgoingEmailTable), \n            MenuItemType::Display).run(args);\n    }\n}<\/pre>\n\n\n\n<p>As you can see, passing a record by code is more complicated than using a menu item button for it. <br>Also, you can set up security permissions on menu items. <br>Because of these reasons, I strongly advise using menu item buttons in this scenario, instead of basic buttons if possible.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Filtering the query<\/h3>\n\n\n\n<p>I pasted the \u201cOutgoingEmail\u201d datasource\u2019s \u201cOnQueryExecuting\u201d event handler method to my class.<br>Note that RefRecId and RefTableId fields exist on this table only because I\u2019m using an extension of SysOutgoingEmailTable.<br>The code filters the form datasource\u2019s query by the received record\u2019s fields.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[ExtensionOf(formstr(SysOutgoingEmailTable))]\n final class SysOutgoingEmailTable_Extension\n {\n     \/\/\/      \/\/\/     \/\/\/ \n     \/\/\/ \n     \/\/\/ \n     [FormDataSourceEventHandler(formDataSourceStr(SysOutgoingEmailTable, \n         OutgoingEmail), FormDataSourceEventType::QueryExecuting)]\n\n     public static void OutgoingEmail_OnQueryExecuting(FormDataSource \n         sender, FormDataSourceEventArgs e)\n     {\n         RefRecId refRecId;\n         RefTableId refTableId;\n         DocuRef docuRef;\n         FormRun fr = sender.formRun(); \n\n         if(fr.args() &amp;&amp; fr.args().record())\n         {\n             docuRef = fr.args().record();\n             refRecId = docuRef.RefRecId;\n             refTableId = docuRef.RefTableId;\n         }\n\n         sender.query().dataSourceName(sender.name()).addRange(fieldnum\n             (SysOutgoingEmailTable, RefRecId)).value(queryValue(refRecId));\n\n          sender.query().dataSourceName(sender.name()).addRange(fieldnum\n             (SysOutgoingEmailTable, RefTableId)).value(queryValue(refTableId));\n     }\n}<\/pre>\n\n\n\n<p>Additionally, you can also use <strong>Chain of Command<\/strong> to achieve this functionality.<br>You can find some articles about it on our blog on the following link:<br> <a href=\"http:\/\/daxvisionerp.com\/tag\/chain-of-command\/\">http:\/\/daxvisionerp.com\/tag\/chain-of-command\/<\/a> <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Summary<\/h3>\n\n\n\n<p>In previous Dynamics AX versions, you could pass a record similarly with a menu item button. <br>If you want to do it by code, you have to do it differently now, in case you are using extended forms.<\/p>\n\n\n\n<p>Getting the record and using it for filtering the query is also a bit different now. <br>You cannot use the &#8220;element.args()&#8221;, because you are extending form methods with classes, instead of just adding a new method to the form. <br>You have to use the &#8220;sender&#8221; parameter and the FormRun class in order to achieve the same functionality.<\/p>\n\n\n\n<p><\/p>\n\n\n<div class=\"taxonomy-post_tag wp-block-post-terms\"><a href=\"https:\/\/daxvisionerp.com\/home\/tag\/msdyn365fo\/\" rel=\"tag\">#MSDyn365FO<\/a><span class=\"wp-block-post-terms__separator\">, <\/span><a href=\"https:\/\/daxvisionerp.com\/home\/tag\/filter-query-by-parameter\/\" rel=\"tag\">Filter query by parameter<\/a><span class=\"wp-block-post-terms__separator\">, <\/span><a href=\"https:\/\/daxvisionerp.com\/home\/tag\/passing-a-record\/\" rel=\"tag\">Passing a record<\/a><\/div>","protected":false},"excerpt":{"rendered":"<p>Task Passing a record from &#8220;MCROrderNotes&#8221; form and filtering the query by it on &#8220;SysOutgoingEmailTable&#8221; form. Since both of the objects were standard forms, I had to use extensions. That is why passing a record by code slightly changed since the previous versions of Dynamics AX. However, passing it by a menu item button stayed [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"1080","footnotes":""},"categories":[19],"tags":[25,115,117],"class_list":["post-872","post","type-post","status-publish","format-standard","hentry","category-dynamics-365fo","tag-msdyn365fo","tag-filter-query-by-parameter","tag-passing-a-record"],"_links":{"self":[{"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/posts\/872","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/comments?post=872"}],"version-history":[{"count":1,"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/posts\/872\/revisions"}],"predecessor-version":[{"id":1369,"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/posts\/872\/revisions\/1369"}],"wp:attachment":[{"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/media?parent=872"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/categories?post=872"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/tags?post=872"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}