Wednesday, March 20, 2013

Custom Lookup in Dynamics AX 2012

As a Dynamics AX developer, you'll often have to perform custom lookups. Meaning that the user may only select records in another table depending on some condition on the form, or elsewhere.


Now, to create a custom lookup, we'll use the class SysTableLookup as it provides us a set of methods to do so.

Here's our method, to be created directly on the table CustTable, with some CustGroup.


With that little piece of code, Dynamics AX will already display a lookup form with the table you've specified as a DataSource, displaying the fields you specified with the method addLookupField and following the results criteria of the Query you've passed to it.

Now all we have to do is actually call our lookup method, by overriding the lookup of the correct field in our Form's Data Source:


And that's it! The lookup will be performed. But there's one additional step we'll have to take here...

Dynamics AX will display the results of the query, based on the filter we've created. So the user will only be able to see what we want him to see, and so he'll only be able to select what we want. The problem is that if he types in an existing record for the CustTable in the control, even if does not get shown in our lookup, Dynamics AX will accept that.

To avoid that the user does in fact type in something we don't want, we'll have to override the validate method for our field in our Data Source. The simplest logic is to check if the CustTable record the user typed in does attend the filter we've specified:


You also need to write find method on CustTable that you call in validate method.


So if the user simply types in for a record that does not attend our filter, Dynamics AX won't let the user save that value.

Happy DAXing....... :)


2 comments:

  1. Good post. One question.
    why do you need a findByCustAccount method on CustTable, when we already have find method.
    And you can divide your lookup code, in two. One method to fetch the query and one to perform the lookup, you can actually validate the user input using the method that fetches the query. No need to write separate logic for validate.

    ReplyDelete
  2. Thank you for another informative web site. The place else may just I get that type of info written in such a perfect manner? I have a challenge that I am just now operating on, and I have been on the look out for such information. get redirected here

    ReplyDelete

Enable UAT database over OneBox DevTest environment using JIT

 Dear Friends, In this Article I will show you how to enable UAT database access for Development machine using just-in-time (JIT). Many time...