In a recent development project we had to add query ranges and grouping on the week, month or year number dynamically. Initial thoughts were to use expressions in query ranges combined with date functions like wkOfYr. However after a few attempts it was clear that this was not the best solution to our problem. That is when we decided to use a view with computed columns.
We created a view and added the original table as a datasource. The idea was to add the week of year, month number and year number as computed columns and then use them in the query as normal fields.
The following are the steps you need to do to add a computed column for the week number:
First create a new integer computed column for the week number. Right-click on fields under the view and select new > integer computed column. Let’s call it MyComputedColumn for our example.
Then create a method on your view which will calculate the value for the computed column. The method must be set to private static of type string. For AX 2012 you should set the method to run on server. The method below is self-explanatory, it gets the SQL field name from the view and returns an SQL select statement to convert the date to week number using the SQL function DATEPART.
Remember that a computed column works by adding SQL query syntax to the view. Therefore you need to use SQL functions like DATEPART(WEEK,%1) and not D365 FO/ AX functions like WkOfYr in the returned select statement. You can easily find SQL syntax functions on the web such as this one at w3schools.
This post explains how to implement the same data entity export functionality provided in the Data Management workspace in a form or a menu item.
Below is form dialog that I created to research how to create data entities and run the export functionality directly from X++ code and using standard DMF classes.
Creating an export data project
The first code sample is how I created the export data project. The definition group name is anything that you want. For the source name (data format EXCEL, CSV etc) you can use the standard lookup method DMFQuickImportExportFormHelper.buildSourceNameControlLookup(). For the entity name there is another standard lookup DMFUtil::lookupEntity(this).
/// Create DMF Processing group to export 'Customer Groups'
/// <param name = "_groupName">Definition group name</param>
/// <param name = "_sourceName">Export file type e.g. CSV</param>
/// <param name = "_entityName">Entity Name</param>
public static void createNewDefinitionGroup(DMFDefinitionGroupName _groupName, DMFSourceName _sourceName, DMFEntityName _entityName)
DMFEntity dmfEntity = DMFEntity::find(_entityName);
DMFDataSource dmfDataSource = DMFDataSource::find(_sourceName);
if (dmfEntity && dmfDataSource)
DMFDefinitionGroupName dmfDefinitionGroupName = _groupName;
Description description = _groupName;
//Create data project
"", //Sample file
"@SYS6047", //"All fields"
NoYes::Yes, //Skip staging
NoYes::Yes, //Export to File. Set Yes to create a definition group of operation type Export
"", //Sheet lookup
NoYes::No, //Truncate Entity Data
Exporting the data definition group
To export the definition group we use the DMFPackageExporter class after creating a new DMF Definition Group Execution.
DMFDefinitionGroup definitionGroup = DMFDefinitionGroup::find("My demo export");
DMFExecutionId dmfExecutionId = DMFUtil::setupNewExecution(definitionGroup.DefinitionGroupName);
Description description = strFmt('Execution - %1 for Definition Group - %2', dmfExecutionId, definitionGroup.DefinitionGroupName);
//Create new dmf execution group for the new execution
if (DMFDefinitionGroupExecution::insertOrDisplay(definitionGroup, dmfExecutionId, description, false))
dmfDefinitionGroupExecution = DMFDefinitionGroupExecution::find(definitionGroup.DefinitionGroupName, dmfDefinitionGroupEntity.Entity, dmfExecutionId);
DMFExportImportDataContract dmfDataContract = new DMFExportImportDataContract();
DMFPackageExporter packageExporter = new DMFPackageExporter();
//Download file for the newly created dmf execution group
To send the file to the user from the DMF Definition Group Execution table we use DMFEntityExportDetails to get the download URL.
Another useful class is the DMFFormBrowser. This opens the entity query dialog and lets the user add filters. You can also code your own ranges on the query and then package it to attached to the DMF Group Execution. You would use this code before calling the exportToPackageV2 method shown previously.
queryData = DMFUtil::getDefaultQueryForEntityV3(dmfDefinitionGroupEntity.Entity, dmfDefinitionGroupEntity.DefinitionGroup);
//Show query dialog
queryData = DMFFormBrowser::showQuery(queryData);
//Update Query filter on DMF Definition Group
if(queryData != connull())
dmfDefinitionGroupExecution.QueryData = queryData;
//Save updated query for next time
dmfDefinitionGroupEntity.QueryData = queryData;
This blog post is a code dump of a file upload dialog for D365 FO. There are various around, but I could not find a complete example so I decided to share my own. This is a single class extending the RunBase class, which has a file upload control and then parses the CSV file returned from the IO stream. No data entities are used although you can modify the code and use them.
Privacy & Cookies Policy
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.