1

Assume I have an entity with the following structure:

ID (it would be the GUID of the record, I'll use number in this example for easier read)
Name
Product (lookup to product)
Price List (custom lookup to standard price list) 

I now need to build a lookup view in this fashion:


(Sample data) 
ID    Name    Product    PriceList
-----------------------------------
1      Rec1    P1         PL1
2      Rec1    P1         PL2 
3      Rec1    P1         PL3
4      Rec2    P2         PL1
5      Rec2    P2         PL2

(Desired result in lookup view)
ID    Name
-----------------------------------
1     Rec1              
4     Rec2              

To explain the requirement in plain english, it is:

In the lookup view I only want one row per Name: Product and Price List don't matter.

Here's what I'd do if I could use straight T-SQL:

;WITH cte AS
(
    SELECT ROW_NUMBER() 
    OVER (PARTITION BY Name ORDER BY Name ASC) AS RowNo, ID, Name
    FROM FilteredEntity
)
SELECT ID, Name FROM cte WHERE RowNo = 1

But I can't figure out how to achieve the same result set in a FetchXML query.

Alex
  • 23,004
  • 4
  • 39
  • 73

2 Answers2

1

The only way I can think of to do this would go like this:

  1. In JS on your form, determine the unique names (Rec1 and Rec2 from your example), and store the ids of two records that reference Rec1 and Rec2 (let's call these ids Id1 and Id2)
  2. Generate a fetch query that returns only those two records. Your fetch query would specifically say show me records where id is Id1 or Id2.
  3. Filter the lookup using addCustomFilter and addPreSearch (link).
Polshgiant
  • 3,595
  • 1
  • 22
  • 25
  • I used this on another project so kudos, unfortunately in this particular situation I need to build a Saved Query, so *if it can be done* it has to be a single fetchxml query with no scripting... – Alex May 24 '16 at 09:21
0

I can see two approaches here, neither of which are pure FetchXML queries.

  1. Use distinct, (I'm not 100% on this but worth giving a go). This will then hopefully get you a record for every name, and you just grab the first record of each in code.

<attribute name='name' distinct='true'/>

  1. Perform the filtering client side using Linq.
James Wood
  • 17,286
  • 4
  • 46
  • 89
  • `distinct` unfortunately has no effect because the query has to include the entity ID (otherwise the lookup won't work properly). About the LINQ part, I'll look into some kind of retrieve plugin... – Alex May 23 '16 at 09:53