Reference group and GROUP BY

This blog post explains a problem that you can run into when using reference group controls with grouped data – and a solution for this problem.

I have a table which stores references to workers. The field has HcmWorkerRecId data type, i.e. it stores record IDs from HcmWorker table. When I drop this field to a grid on a form, the system uses Reference Group control, which shows human-readable data instead of RecId numbers. By default, it shows worker names.

There are multiple records for the same worker, therefore if I want to show data summarized data for each worker, I add grouping to the query:

TableWithRef_ds.queryBuildDataSource().addGroupByField(fieldNum(TableWithRef, Worker));

But the result isn’t correct – the reference group doesn’t show anything.

The grouping works correctly; I can see the right record IDs if I display them thought an Int64 control instead of a reference group.

The problem is that we group only by the record ID, but not by Name. And because Name is neither used in GROUP BY nor it has an aggregation function applied, its value is undefined and there is nothing to show.

Let’s fix it. Go to the data source in AOT and add reference data sources. Because Name field isn’t directly in HcmWorker table, we’ll need one more data source – DirPerson. Like this:

You can add them by right-clicking the Reference Data Sources node, choosing New Reference Data Source and then setting properties Join Relation and Name.

Then we can easily add an extra field to group by, this time from DirPerson table:

DirPerson_ds.queryBuildDataSource().addGroupByField(fieldNum(DirPerson, Name));

Voilà, names are back!

5 Comments

  1. Hello Martin,

    is there also a solution for Dynamics AX2012R2.

    I Need the Same example for this Version. But if i try to call the Statement for the GroupBy, the reference Datasource is not defined at the Moment.

    Thanks
    Michael

    • Where are calling addGroupByField()? After you add the referenced datasource to the form in AOT (as shown in the blog post), it should be initialized at the same time as all other datasources. It’s outer-joined to the parent DS and data for both is obtained at once.

      • Hello Martin,

        i calling the addGroupByField() in the init of the parent datasource of the referenced datasource.
        The referenced datasource is added in AOT. If i call “DirPerson_ds.queryBuildDataSource().addGroupByField(fieldNum(DirPerson, Name));” an error “QueryBuildDataSource Objekt ist nicht initialisiert.”

        Im really confused.

        • I found the issue. i had to groupby in the executeQuery of the main table.
          Now it works. Thanks

Comments are closed.