Filtering Lookup by Relation in Dynamics 365 for Operations
Last time I showed how to add filtering capabilities to your lookup using the DataInteractorFactory class. This time I intend to show how to extend filtering capabilities to support lookups where there is more than one key field on the target table.
Let’s say we have a table, SSISplitBillingTable, that contains split billing codes for a customer. The primary key for this table would then be the combination of the SplitBillingCode and AccountNum.
This also implies that each customer may have multiple SplitBillingCodes. Because we want to identify a default code for each customer, we will add a foreign key to CustTable.
And a table relation to SSISplitBillingCode to enable the lookup.
When we click the drop-down to select a Split billing code from the CustTable form, we are presented with too many records. The intention is to allow the user to select only Split billing codes that are associated with the current CustTable record. As you can see, the list contains 2 records for the current customer, C0020, and one for customer AA.
It was at this point that I must admit I was a little confused. I had assumed that if I put both fields in the table relation that my list would be properly filtered, but apparently, I would be forced to write a little code to make this work. I knew I didn’t want to add a lookup method to every form that would contain this field because that requires too many modifications and doesn’t do anything to prevent future problems. Instead, I was going to see if I could modify the DataInteractorFactory class. It was here I learned something new.
My approach was to explore the DataInteractorTarget owned by the DataInteractorFactory class. Among other things, this class identifies the field, control, and table relation involved in the lookup. A little investigation led me to the BoundRelation class, and the realization that my scenario was already supported.
Inside the applyFieldDependenciesToQuery method, the BoundRelation class sets up the query used to perform the lookup. It does this by iterating through the collection of dependent fields, adding each field value as a range on the query. The problem was that my dependent fields collection was empty. The reason the collection was empty could be found in the processDictRelation method. This method iterates over the lines in the table relation until it finds the primary key field. Each field it encounters before it is found is added to the dependent field list.
if (thisField == this.fkFieldBinding().fieldId()) { // This is the link that defines the relation from fk field to pk field. pkField = externField; } else if (pkField == 0) { // This field comes before the bound field in the relation definition. To match kernel behavior, add this field as a filter. SysDictField dictField = new SysDictField(primaryKeyTableId, externField); if (dictField != null) { this.dependentFieldsTarget().addEnd(AbsoluteFieldBinding::construct(dictField.name(), dictField.tableName())); this.dependentFieldsSource().addEnd(thisField); } }
This means that to filter our lookup to only those records with a matching AccountNum, we need only reorder the lines in our table relation.
Now when we use the lookup the results are properly filtered.
Under the terms of this license, you are authorized to share and redistribute the content across various mediums, subject to adherence to the specified conditions: you must provide proper attribution to Stoneridge as the original creator in a manner that does not imply their endorsement of your use, the material is to be utilized solely for non-commercial purposes, and alterations, modifications, or derivative works based on the original material are strictly prohibited.
Responsibility rests with the licensee to ensure that their use of the material does not violate any other rights.