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

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

Entity export through X++

(2) ShareShare
ReportReport
Posted on by 30
Hi Martin, As suggested I am creating a new thread.
 
class UCL_DataProject
{
    public static void main(Args  _args)
    {
        
                #DMF
                SharedServiceUnitFileID fileId;
                DMFDefinitionGroupName definitionGroupName = "CustomerAgingDataStorage";
 
                try
                {
                    EntityName  entityName = DMFEntity::findFirstByTableId(tableNum(CustomerAgingDataStorageEntity)).EntityName;
 
                    QueryBuildRange         qbr;
                    QueryBuildDataSource    qbds;
                    
                    // Start:Optional if you want to filter data while exporting

                    Query   query = new Query(DMFUtil::getDefaultQueryForEntityV3(entityName));
                    qbds    = query.dataSourceTable(tableNum(CustAgingDataHeaderStorage));
                    qbr = qbds.addRange(fieldNum(CustAgingDataHeaderStorage, BatchName));
                    qbr.value(queryValue("30.09-2024-10-29T16:37:38"));  
                  
                    // End
 
                    DMFEntityExporter exporter = new DMFEntityExporter();
                    fileId = exporter.exportToFile(entityName,
                    definitionGroupName,'',"Excel","@ApplicationFoundation:DMFExportOperation",query.pack(),curext()
                    );
 
                    if (fileId != '')
                    {
                        str downloadUrl = DMFDataPopulation::getAzureBlobReadUrl(str2Guid(fileId));
 
                        System.Uri uri = new System.Uri(downloadUrl);
                        str fileExt;
 
                        if (uri != null)
                        {
                            fileExt = System.IO.Path::GetExtension(uri.LocalPath);
                        }
 
                        Filename filename = strFmt('MyFirstExport%1',fileExt);
                        System.IO.Stream stream = File::UseFileFromURL(downloadUrl);
                        File::SendFileToUser(stream, filename);
                    }
                    else
                    {
                        throw error("DMF execution failed and details were written to the execution log");
                    }
                    info("Export had been done successfully");
                }
                catch
                {
                    error("error occurred while exporting");
                }
    }

}
1. I like to filter the data before exporting to excel based on the batch name, But the in query for testing I hardcore the "Batch name" and it is not working. And it's exported all data.
 
2.The EXCEL file is downloaded to the local machine in the Download folder perfectly.***
 
3, But the customer is looking for the file as a data project after downloading in the local machine in download folder as below:

In the below format customer looking for the excel file:
 
 
As suggested by you, I have checked the configuration for the Entity. The source data format was there earlier Excel. So, I have changed to Package and also change in the below code:
 
 
Code
 
DMFEntityExporter exporter = new DMFEntityExporter();
fileId = exporter.exportToFile(entityName,
definitionGroupName,'',"Package","@ApplicationFoundation:DMFExportOperation",query.pack(),curext()
);
 
and executed the code. but it return's TXT file instead of data project as below:
 
 
In my code, there are two problems as of now:
 
1. Query is NOT filtered based on the Batch name as mentioned above.
2. After exporting, data package is NOT generated as expected in download folder. 
 
Please let me know what modifications are required.
 
Thanks!
 
 
 
 
Categories:
I have the same question (0)
  • Martin Dráb Profile Picture
    237,985 Most Valuable Professional on at
    For reference (to help anyone trying to make sense of this thread), this is a continuation of @rp@n's threads Parameter need to pass on data entity in d365fo and I like to export Vendor master data through DMF data project using X++.
     
    First of all, let me make your code easier to read by fixing indentation, use local variable declarations and so on.
    class UCL_DataProject
    {
        public static void main(Args args)
        {
            #DMF
            DMFDefinitionGroupName definitionGroupName = "CustomerAgingDataStorage";
    
            try
            {
                EntityName entityName = DMFEntity::findFirstByTableId(tableNum(CustomerAgingDataStorageEntity)).EntityName;
    
                // Start:Optional if you want to filter data while exporting
                Query   query = new Query(DMFUtil::getDefaultQueryForEntityV3(entityName));
                QueryBuildDataSource qbds    = query.dataSourceTable(tableNum(CustAgingDataHeaderStorage));
                QueryBuildRange qbr = qbds.addRange(fieldNum(CustAgingDataHeaderStorage, BatchName));
                qbr.value(queryValue("30.09-2024-10-29T16:37:38"));  
                // End
    
                SharedServiceUnitFileID fileId = new DMFEntityExporter().exportToFile(
                    entityName,
                    definitionGroupName,
                    '',
                    "Excel",
                    "@ApplicationFoundation:DMFExportOperation",
                    query.pack(),
                    curext());
    
                if (fileId != '')
                {
                    str downloadUrl = DMFDataPopulation::getAzureBlobReadUrl(str2Guid(fileId));
                    System.Uri uri = new System.Uri(downloadUrl);
                    str fileExt = System.IO.Path::GetExtension(uri.LocalPath);
    
                    Filename filename = strFmt('MyFirstExport%1',fileExt);
                    System.IO.Stream stream = File::UseFileFromURL(downloadUrl);
                    File::SendFileToUser(stream, filename);
                }
                else
                {
                    throw error("DMF execution failed and details were written to the execution log");
                }
            }
        }
    }
    I don't see any problem in your query. I suggest debugging the export.
     
    What is the content of your file? That is has the .txt extension doesn't automatically mean that it's really a text file.
     
  • @rp@n Profile Picture
    30 on at
    Thanks Martin for the response.
     
    Let's start with first issue.
     
    The query is NOT getting filtered. It exported all data. I am just looking for particular data only.
     
     
                        Query   query = new Query(DMFUtil::getDefaultQueryForEntityV3(entityName));
                        qbds    = query.dataSourceTable(tableNum(CustomerAgingDataStorageEntity));
                        qbr = qbds.addRange(fieldNum(CustAgingDataHeaderStorage, BatchName));
                        qbr.value(queryValue("30.09-2024-10-29T16:37:38"));
    Kindly advise pls.
     
    Thanks!
  • Martin Dráb Profile Picture
    237,985 Most Valuable Professional on at
    Yes, I understand that.
     
    By the way, please get used to remove the extra indendation from your code snippets (you can see an example in your last reply). You created thousand of threads in past years, and got a lot of feedback, therefore you should know well how to use this site.
  • Martin Dráb Profile Picture
    237,985 Most Valuable Professional on at
    Ah, I see a bug in your latest code. You used a wrong table in fieldNum(): the data source is CustomerAgingDataStorageEntity, not CustAgingDataHeaderStorage.
     
    You should learn how to look at query generated by your code; you'd likely see a wrong field there. If you completely skip debugging and you check the final result only, you don't know which part of the solution failed.
  • @rp@n Profile Picture
    30 on at
    Thanks Martin, need to check the query .
     
    And the second issue, Data project not getting generated after exporting.
     
    Is any code modification is required for this.
     
    Kindly advise pls.

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 420 Most Valuable Professional

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 241 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans