Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Sort order on custom lookup not working

(0) ShareShare
ReportReport
Posted on by

Hello,

I'm working on a custom lookup in AX 2012 R2 and I'm seeing the strangest behavior in the order by clause that is generated by SQL.  I'm wondering if anyone can point me to where I'm going wrong.  The lookup is presenting a list of branches from native a AX table OMOperatingUnit.  I want my lookup to be sorted by the branch number (OMOperatingUnitNumber field) and also include the branch name.  With only the OMOperatingUnit table included in the lookup's data sources, this part is working just fine: 

public static Common lookupBranchBudgetByUser(FormReferenceControl _formReferenceControl)
{
Query query = new Query();
QueryBuildDataSource qbdsOMOperatingUnitNumber, qbdsBranchBudgetBranchUserFilterView;

// create the lookup and add the lookup fields
SysReferenceTableLookup sysTableLookup = SysReferenceTableLookup::newParameters(tableNum(OMOperatingUnit), _formReferenceControl, true);
sysTableLookup.addLookupfield(fieldNum(OMOperatingUnit, OMOperatingUnitNumber));
sysTableLookup.addLookupfield(fieldNum(OMOperatingUnit, Name));
sysTableLookup.addSelectionField(fieldNum(OMOperatingUnit, RecId));

// add the OMOperatingUnit data source
qbdsOMOperatingUnitNumber = query.AddDataSource(tableNum(OMOperatingUnit));

// order lookup by branch number
qbdsOMOperatingUnitNumber.addOrderByField(fieldNum(OMOperatingUnit, OMOperatingUnitNumber));

// group by branch so we only present the distinct list of branches in the list
qbdsOMOperatingUnitNumber.addGroupByField(fieldNum(OMOperatingUnit, OMOperatingUnitNumber));
qbdsOMOperatingUnitNumber.addGroupByField(fieldNum(OMOperatingUnit, Name));
qbdsOMOperatingUnitNumber.addGroupByField(fieldNum(OMOperatingUnit, RecId));

sysTableLookup.parmQuery(query);
return sysTableLookup.performFormLookup();

}

The above section works just fine for presenting the lookup and sorting them by branch.  However, for this use case, only certain users should have access to budget certain branches.  I have a set of tables which specify which user has access to which branch and I've created a view for these tables so I can filter the branch list on my view.  The filtering is working great but it somehow throws my ordering off.  Here's the code that I've added prior to the return to do the filtering: 

// join to the branch budget user filter view to filter only the branches that the user has access to budget

qbdsBranchBudgetBranchUserFilterView = qbdsOMOperatingUnitNumber.addDataSource(tableNum(BranchBudgetBranchUserFilterView));
qbdsBranchBudgetBranchUserFilterView.addLink(fieldNum(OMOperatingUnit, RecId), fieldNum(BranchBudgetBranchUserFilterView, OMOperatingUnitRecId));

// filter current user
qbdsBranchBudgetBranchUserFilterView.addRange(fieldNum(BranchBudgetBranchUserFilterView, User)).value(curUserId());

// filter valid attribute assignment for the current date
qbdsBranchBudgetBranchUserFilterView.addRange(fieldNum(BranchBudgetBranchUserFilterView, BranchAttributeRelTableValidFrom)).value(queryRange(null, today(), true));
qbdsBranchBudgetBranchUserFilterView.addRange(fieldNum(BranchBudgetBranchUserFilterView, BranchAttributeRelTableValidTo)).value(queryRange(today(), null, true));

Once I add this join to filter out the branches that a particular user shouldn't see in the lookup, if i click the lookup I see the list of branches are now sorted by RecId instead of the OMOperatingUnitNumber.  I ran a trace and checked the query in the trace parser and found this query: 

SELECT T1.NAME, T1.RECID, T1.OMOPERATINGUNITNUMBER, T1.RECID
FROM DIRPARTYTABLE T1
CROSS JOIN BRANCHBUDGETBRANCHUSERFILTERVIEW T2
WHERE (((T1.PARTITION=?)
AND ((T1.OMOPERATINGUNITTYPE=?)
AND (T1.RECID>=?)))
AND (T1.INSTANCERELATIONTYPE IN (2377) ))
AND ((((((T2.PARTITION=?)
AND (T2.PARTITION#2=?))
AND (T2.PARTITION#3=?))
AND (T2.PARTITION#4=?))
AND (T2.PARTITION#5=?))
AND ((((T2.USER_=?)
AND (T2.BRANCHATTRIBUTERELTABLEVALIDFROM<=?))
AND (T2.BRANCHATTRIBUTERELTABLEVALIDTO>=?))
AND (T1.RECID=T2.OMOPERATINGUNITRECID)))
GROUP BY T1.OMOPERATINGUNITNUMBER, T1.NAME, T1.RECID
ORDER BY T1.RECID

Now the strangest thing in this, if I type * into the field to open the lookup, the values are sorted properly and the query i see in the trace parser is this:

SELECT T1.NAME, T1.RECID, T1.OMOPERATINGUNITNUMBER, T1.RECID
FROM DIRPARTYTABLE T1
CROSS JOIN BRANCHBUDGETBRANCHUSERFILTERVIEW T2
WHERE (((T1.PARTITION=?)
AND ((T1.OMOPERATINGUNITNUMBER LIKE ? ESCAPE '\' )
AND (T1.OMOPERATINGUNITTYPE=?)))
AND (T1.INSTANCERELATIONTYPE IN (2377) ))
AND ((((((T2.PARTITION=?)
AND (T2.PARTITION#2=?))
AND (T2.PARTITION#3=?))
AND (T2.PARTITION#4=?))
AND (T2.PARTITION#5=?))
AND ((((T2.USER_=?)
AND (T2.BRANCHATTRIBUTERELTABLEVALIDFROM<=?))
AND (T2.BRANCHATTRIBUTERELTABLEVALIDTO>=?))
AND (T1.RECID=T2.OMOPERATINGUNITRECID)))
GROUP BY T1.OMOPERATINGUNITNUMBER, T1.NAME, T1.RECID
ORDER BY T1.OMOPERATINGUNITNUMBER

 


Does anyone have any idea why joining to my view would cause the lookup to sort by RecId when I click the lookup but it still sorts properly when i key * into the field?  The large majority of users will use their mouse to open the lookup so asking them to key * isn't a great workaround for my users.  Any suggestion is greatly appreciated!

*This post is locked for comments

  • Greg's Mom Profile Picture
    Greg's Mom on at
    RE: Sort order on custom lookup not working

    [quote user="Daniel Weichsel"]

    What happens if you remove the groupBy conditions and instead use an "exists join" for the filter data source (rather than an inner join)?

    [/quote]

    It works perfectly!  That's what happens!  Thanks so much for the perfect suggestion.  I'm disappointed that I hadn't thought of that myself.  Thank you!

  • Verified answer
    Daniel Weichsel Profile Picture
    Daniel Weichsel 1,657 on at
    RE: Sort order on custom lookup not working

    What happens if you remove the groupBy conditions and instead use an "exists join" for the filter data source (rather than an inner join)?

  • Greg's Mom Profile Picture
    Greg's Mom on at
    RE: Sort order on custom lookup not working

    I also wanted to mention that when I capture the query from AX by writing query.toString(), I get the same query whether I click the lookup or use * to open the lookup and the query shows the order by branch (OMOperatingUnitNumber).  It's just the strangest thing, I can't understand why this is changing to order by RecId when i click the lookup.

    SELECT *
    FROM OMOperatingUnit(OMOperatingUnit_1)
    GROUP BY OMOperatingUnit.OMOperatingUnitNumber, OMOperatingUnit.Name, OMOperatingUnit.RecId
    ORDER BY OMOperatingUnit.OMOperatingUnitNumber ASC
    JOIN *
    FROM BranchBudgetBranchUserFilterView(BranchBudgetBranchUserFilterView_1)
    ON OMOperatingUnit.RecId = BranchBudgetBranchUserFilterView.OMOperatingUnitRecId
    AND ((User = N'...'))
    AND ((BranchAttributeRelTableValidFrom<={ts '2018-06-28 00:00:00.000'}))
    AND ((BranchAttributeRelTableValidTo>={ts '2018-06-28 00:00:00.000'}))

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,489 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans