Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

Export data through DMF data project using x++ query range is not working

(0) ShareShare
ReportReport
Posted on by 45

Hi,

Using the following code to generate CSV file by Data entity through x++ code.

I am getting the CSV file with all data, filter query range is not working.

Please help me on this. 

public void exportHeader()
{
#DMF
SharedServiceUnitFileID fileId;
DMFDefinitionGroupName definitionGroupName = #AgHdExp;

try
{
EntityName entityName = DMFEntity::findFirstByTableId(tableNum(SalesAgreementHeaderEntity)).EntityName;

// Start:Optional if you want to filter data while exporting
Query query = new Query(DMFUtil::getDefaultQueryForEntityV3(entityName));
QueryBuildDataSource qbds = query.dataSourceTable(tableNum(SalesAgreementHeaderEntity));

// query range 

SysQuery::findOrCreateRange(qbds, fieldNum(SalesAgreementHeaderEntity,
SALESAGREEMENTID)).value("LS16-000021");

// End

DMFEntityExporter exporter = new DMFEntityExporter();

fileId = exporter.exportToFile(entityName,
definitionGroupName,'',#CSV,#FieldGroupName_AllFields,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(#AgHdExp+'%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");
}
}
catch
{
error("error occurred while exporting");
}
}

Thanks,

Arun

  • CU02011137-0 Profile Picture
    4 on at
    Export data through DMF data project using x++ query range is not working
    Can anybody pls reply on thid
  • CU02011137-0 Profile Picture
    4 on at
    Export data through DMF data project using x++ query range is not working
    Hi Arun, as you mentioned that your issue has been resolved.
     
    I have also faced the similar issue. Through exporttofile, I am getting all the data in exporting but the range is not working.
     
    Kindly send me the latest code which you used and you mentioned that range yiu provide in entity.
     
    Please send me the screenshot pls.
     
    I will follow the same.
     
    Thanks in advance!
     
    Waiting for your reply.
  • Verified answer
    ArunAnandK Profile Picture
    45 on at
    RE: Export data through DMF data project using x++ query range is not working

    Hi Martin,

    Thanks for the response.

    When i ran a query, i was getting proper filtered data. when i sent exportToFile() i was getting full data.

    My problem is fixed, i have added filter criteria in entity range, it works.

    Thanks,

    Arun.

  • Verified answer
    Martin Dráb Profile Picture
    231,975 Most Valuable Professional on at
    RE: Export data through DMF data project using x++ query range is not working

    Your code is difficult to read, because you pasted it in a wrong way and line indentation got lost. Please always use Insert > Code (in the rich-formatting view).

    public void exportHeader()
    {
        #DMF
        SharedServiceUnitFileID fileId;
        DMFDefinitionGroupName definitionGroupName = #AgHdExp;
    
        try
        {
            EntityName entityName = DMFEntity::findFirstByTableId(tableNum(SalesAgreementHeaderEntity)).EntityName;
    
            // Start:Optional if you want to filter data while exporting
            Query query = new Query(DMFUtil::getDefaultQueryForEntityV3(entityName));
            QueryBuildDataSource qbds = query.dataSourceTable(tableNum(SalesAgreementHeaderEntity));
    
            // query range 
            SysQuery::findOrCreateRange(qbds, fieldNum(SalesAgreementHeaderEntity, SalesAgreementId)).value(queryValue("LS16-000021"));
    
            // End
            DMFEntityExporter exporter = new DMFEntityExporter();
    
            fileId = exporter.exportToFile(entityName, definitionGroupName, '', #CSV, #FieldGroupName_AllFields, 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(#AgHdExp '%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");
            }
        }
        catch
        {
            error("error occurred while exporting");
        }
    }

    What if you run the query by yourself, instead of sending it to exportToFile(). Does it give you the right result?

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

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

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,276 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,975 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans