web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Is it possible to do a GroupBy in a lookup method on a form control?

(0) ShareShare
ReportReport
Posted on by 2,007

I am trying to create a lookup in a form to allow users to pick the tableId of a table using UtilIdElements as the source for the lookup. The problem with this is that a record for the table will appear on each layer it exists on, so if a table exists on both sys and cus (which is one of our scenarios) the table entry appears twice in the lookup, when we only want it to show up once. To fix this, I attempted to add group by fields to the lookup and changed the orderMode to OrderMode:GroupBy. When I try to do the lookup after adding the group by, I get this error:

Temporary tables can have either Group By or Order By fields, but not both.

This error doesn't make sense to me, since I am using query objects to generate the lookup, but I'm thinking perhaps that a SQL temporary table is generated during the lookup, thus the error. However, at no point in this code do I explicitly declare any sort fields or change the orderMode to OrderMode:OrderBy, so I am unsure why i am getting this error.

Here is the code for my lookup. The control is of type Int64 to facilitate getting the tableId value. Does anyone have an idea of why this is happening and how I can possibly fix it? 

public void lookup()
{
    Query                       query = new Query();
    QueryBuildDataSource        qbds;
    QueryBuildRange             typeQBR;
    QueryBuildRange             tableQBR;
    SysTableLookup              sysTableLookup = SysTableLookup::newParameters(tableNum(UtilIdElements), this);

    sysTableLookup.addLookupfield(fieldNum(UtilIdElements, Id));
    sysTableLookup.addLookupfield(fieldNum(UtilIdElements, Name));

    qbds = query.addDataSource(tableNum(UtilIdElements));

    qbds.orderMode(OrderMode::GroupBy);
    qbds.addGroupByField(fieldNum(UtilIdElements, Id));
    qbds.addGroupByField(fieldNum(UtilIdElements, Name));


    typeQBR = qbds.addRange(fieldNum(UtilIdElements, recordType));
    typeQBR.value(SysQuery::value(UtilElementType::Table));

    tableQBR = qbds.addRange(fieldNum(UtilIdElements, Id));
    tableQBR.value(con2Str(element.conTableList()));

    sysTableLookup.parmQuery(query);
    sysTableLookup.performFormLookup();
    // super();
}

Thanks.

Brandt

I have the same question (0)
  • Brandt Fundak Profile Picture
    2,007 on at
    RE: Is it possible to do a GroupBy in a lookup method on a form control?

    Martin,

    Sorry i got dragged into something else and have not been able to follow up on this. Your suggestion worked. I had an issue with getting the system enum for table type to work with my QueryBuildRange object, but was able to use the enum2int function to resolve that issue. Here's the amended code for the lookup for anyone that is interested (element.contableList() is a method that ranges on a subset of table ids using a container)

    public void lookup()
    {
        Query                       query = new Query();
        QueryBuildDataSource        qbds;
        QueryBuildRange             typeQBR;
        QueryBuildRange             tableQBR;
        SysTableLookup              sysTableLookup = SysTableLookup::newParameters(tableNum(SysModelElement), this);
    
        sysTableLookup.addLookupfield(fieldNum(SysModelElement, AXId));
        sysTableLookup.addLookupfield(fieldNum(SysModelElement, Name));
    
        qbds = query.addDataSource(tableNum(SysModelElement));
    
        typeQBR = qbds.addRange(fieldNum(SysModelElement, ElementType));
        typeQBR.value(SysQuery::value(enum2int(UtilElementType::Table)));
    
        tableQBR = qbds.addRange(fieldNum(SysModelElement, AXId));
        tableQBR.value(con2Str(element.conTableList()));
    
        sysTableLookup.parmQuery(query);
        sysTableLookup.performFormLookup();
        // super();
    }

    Thank you for your advice.

    Brandt

  • Verified answer
    Martin Dráb Profile Picture
    235,961 Most Valuable Professional on at
    RE: Is it possible to do a GroupBy in a lookup method on a form control?

    Will it make any difference if you use SysModelElement instead of UtilIdElements?

  • Brandt Fundak Profile Picture
    2,007 on at
    RE: Is it possible to do a GroupBy in a lookup method on a form control?

    I get the exact same behavior whether the orderMode call occurs before or after setting the group by fields or not calling it at all. (I assume that setting group by fields may create an implicit orderMode call.

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at
    RE: Is it possible to do a GroupBy in a lookup method on a form control?

    What if you remove line 14 (qbds.orderMode(OrderMode::GroupBy))?

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Sohaib Cheema Profile Picture

Sohaib Cheema 823 User Group Leader

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 673 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 491 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans