
{"id":374,"date":"2017-06-22T11:50:35","date_gmt":"2017-06-22T01:50:35","guid":{"rendered":"http:\/\/bakke.online\/?p=374"},"modified":"2022-01-22T20:10:55","modified_gmt":"2022-01-22T09:10:55","slug":"filtered-form-reference-group-lookup","status":"publish","type":"post","link":"https:\/\/www.bakke.online\/index.php\/2017\/06\/22\/filtered-form-reference-group-lookup\/","title":{"rendered":"Filtered form reference group lookup"},"content":{"rendered":"<p>Dynamics AX 2012 introduces built-in support for resolving and looking up linked records based on reference record IDs.<\/p>\n<p>When adding a reference record ID field to a form, AX will add a reference group control instead of the reference record ID, and will show a selected set of fields from the referenced table. \u00a0See the posts about <a href=\"https:\/\/www.bakke.online\/index.php\/2017\/04\/11\/how-to-tree-lookups-in-dynamics-ax-reference-group-controls-part-1\/\">tree lookups<\/a> for an example.<\/p>\n<p>The reference group control also does lookups into the referenced table for selecting a new value. \u00a0However, by default this lookup is not filtered and will show all records in the referenced table. \u00a0Adding filtering is quite easy to do and it follows a similar pattern as regular table lookups.<\/p>\n<p><!--more--><\/p>\n<h3>The lookupReference() method<\/h3>\n<p>To add a custom lookup, you&#8217;ll need to override the lookupReference() method on the reference group control, not lookup() which is used for regular lookups.<\/p>\n<pre><span style=\"color: #0000ff;\"><strong>public<\/strong> <\/span>Common lookupReference(FormReferenceControl _formReferenceControl)\n{<\/pre>\n<p>lookupReference() is different in that it returns a record buffer, which will be used to populate the individual fields in the reference group. \u00a0Good, so we just need to return a record buffer, but how to select one from a filtered list? \u00a0That&#8217;s where the SysReferenceTableLookup class comes into play.<\/p>\n<h3>The SysReferenceTableLookup class<\/h3>\n<p>This class provides an API which is almost identical to SysTableLookup, so if you are used to that class, this should all look and feel quite similar.<\/p>\n<p>We create a SysReferenceTableLookup object and point it to the table we want to reference. \u00a0(Replace ReferencedTable, OtherTable and their field names with the relevant table and field names from your application)<\/p>\n<pre>SysReferenceTableLookup lookup = SysReferenceTableLookup::newParameters( <span style=\"color: #0000ff;\"><strong>tableNum<\/strong><\/span>( ReferencedTable ), _formReferenceControl );<\/pre>\n<p>Next, we create a query object and add a data source for the same table. \u00a0(You could of course create a query object from an AOT query, as well)<\/p>\n<pre>Query q = <span style=\"color: #0000ff;\"><strong>new<\/strong> <\/span>Query();\nQueryBuildDataSource qbds = q.addDataSource( <span style=\"color: #0000ff;\"><strong>tableNum<\/strong><\/span>( ReferencedTable) );<\/pre>\n<p>We add one or more ranges to restrict the rows that will be shown in the lookup.<\/p>\n<pre>qbds.addRange( <span style=\"color: #0000ff;\"><strong>fieldnum<\/strong><\/span>( ReferencedTable, FieldToFilterOn ) ).value( queryValue( OtherTable.FilterValueField ) );<\/pre>\n<p>We tell the lookup object which query to use and which field(s) to show.<\/p>\n<pre>lookup.parmQuery( q );\nlookup.addLookupfield( <span style=\"color: #0000ff;\"><strong>fieldNum<\/strong><\/span>( ReferencedTable, ID ) );\nlookup.addLookupfield( <span style=\"color: #0000ff;\"><strong>fieldNum<\/strong><\/span>( ReferencedTable, Description ) );<\/pre>\n<p>And finally, we call performFormLookup() to show the actual lookup. \u00a0In SysReferenceTableLookup this method returns a record buffer, which we pass to the return statement.<\/p>\n<pre><span style=\"color: #0000ff;\"><strong>  return<\/strong> <\/span>lookup.performFormLookup();\n}<\/pre>\n<p>The full method would look like this:<\/p>\n<pre><span style=\"color: #0000ff;\"><strong>public<\/strong> <\/span>Common lookupReference(FormReferenceControl _formReferenceControl)\n{\n    SysReferenceTableLookup lookup = SysReferenceTableLookup::newParameters( <span style=\"color: #0000ff;\"><strong>tableNum<\/strong><\/span>( ReferencedTable ), _formReferenceControl );\n    Query q = <span style=\"color: #0000ff;\"><strong>new<\/strong> <\/span>Query();\n    QueryBuildDataSource qbds = q.addDataSource( <span style=\"color: #0000ff;\"><strong>tableNum<\/strong><\/span>( ReferencedTable) );\n\n    qbds.addRange( <span style=\"color: #0000ff;\"><strong>fieldnum<\/strong><\/span>( ReferencedTable, FieldToFilterOn ) ).value( queryValue( OtherTable.FilterValueField ) );\n\n    lookup.parmQuery( q );\n    lookup.addLookupfield( <span style=\"color: #0000ff;\"><strong>fieldNum<\/strong><\/span>( ReferencedTable, ID ) );\n    lookup.addLookupfield( <span style=\"color: #0000ff;\"><strong>fieldNum<\/strong><\/span>( ReferencedTable, Description ) );\n\n<span style=\"color: #0000ff;\"><strong>    return<\/strong> <\/span>lookup.performFormLookup();\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Dynamics AX 2012 introduces built-in support for resolving and looking up linked records based on reference record IDs. When adding a reference record ID field to a form, AX will add a reference group control instead of the reference record ID, and will show a selected set of fields from the referenced table. \u00a0See the &hellip; <a href=\"https:\/\/www.bakke.online\/index.php\/2017\/06\/22\/filtered-form-reference-group-lookup\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Filtered form reference group lookup&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,1],"tags":[2,3,5],"class_list":["post-374","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\/374","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=374"}],"version-history":[{"count":1,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/posts\/374\/revisions"}],"predecessor-version":[{"id":1332,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/posts\/374\/revisions\/1332"}],"wp:attachment":[{"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/media?parent=374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/categories?post=374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/tags?post=374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}