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, ...
Answered

How to to get all query records and pack them

(0) ShareShare
ReportReport
Posted on by 465

Hi,

i'm exporting an entity by code, right now i'm only getting one record in the exported file, i think i need to do queryRun.Next() but how will i do that and pass query.pack in the export method having all values?

#DMF
        SharedServiceUnitFileID fileId;
        DMFDefinitionGroupName definitionGroupName = "MyUniqueDefinitionGroupName";

        try
        {
            EntityName           entityName = DMFEntity::findFirstByTableId(tableNum(SalesInvoiceHeaderV2Entity)).EntityName;
            str f = "#FieldGroupName_AllFields";
            // Start:Optional if you want to filter data while exporting
            Query                query = new Query(DMFUtil::getDefaultQueryForEntity(entityName));
            QueryBuildDataSource qbds = query.dataSourceTable(tableNum(SalesInvoiceHeaderV2Entity));


                DMFEntityExporter exporter = new DMFEntityExporter();
                fileId = exporter.exportToFile(entityName,
                                    definitionGroupName,
                                    '', //Optional: ExecutionID
                                    "XML-Attribute", //Optional::SourceName
                                    f, //Optional field selection
            query.pack(), //Optional: Filtered Query
                                    curExt() //Optional: DataAReaId
                                    );
            

            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");
            }
        }
        catch
        {
            error("error occurred while exporting");
        }

I have the same question (0)
  • Sergei Minozhenko Profile Picture
    23,093 on at

    Hi D365_FO,

    Is MyUniqueDefinitionGroupName data project you have already created? Or it's a unique name you generate every run?

  • D365FO user Profile Picture
    465 on at

    for the first time it got created in the export projects.. but now everytime i run it keeps using it.

    i first tried using this approach:      

    DMFDataPackager dataPackage = new DMFDataPackager();

    dataPackage.exportDataPackage("ExportJob"); // which is a project i already created in ax

    but i get an error that the fileId should not be empty, i don't know how to fill it, but the methods makes it empty somehow

    so i switched to the code i showed in the first post (i would prefer to use the other other approach). But as i said i'm only getting 1 record in the file generated.

  • Verified answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    Hi D365FO_user,

    I guess you need to create a unique name for definitionGroupName for every run otherwise you will export data related to run when data project was created (Filtered Query is used only when a new data project is created). Try to delete "MyUniqueDefinitionGroupName" data project (or use another unique name that does not exist in the system) and run your function again.

  • D365FO user Profile Picture
    465 on at

    ohh it works now, but i think in this way alot of projects will be created, that's why it seems better to use the DMFDataPackager. can u check this link : community.dynamics.com/.../error-while-exporting-data-project-by-code

  • Sergei Minozhenko Profile Picture
    23,093 on at

    Hi D365FO_user,

    You are correct and this approach will create a lot of one-time data projects. But you can delete them right after you get the file. You need to think also about how export will behave in a multiuser instance. Let's assume 2 users decide export invoices in the same company at the same time, with one data project you are risking to mix up this data, and instead of 2 different files, you can get 2 similar files with the same content. With one time project you will have better control on export content.

  • D365FO user Profile Picture
    465 on at

    you are right, it's better to create new project each time in case one user needs to do certain filtering

    cause if multiple users want to do certain filtering it doesn't make sense to open the same project and change the criteria.

    And since i'm going to do a batch job, then i'll put parameters for filtering that i can pass to the code inside.

    but this code seems to not work for composite data entities. see below, it only accepts normal entities so what to do in such case?

    pastedimage1591180692245v1.png

  • D365FO user Profile Picture
    465 on at

    any idea?

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Could you share your code as text next time, now I can't copy-paste it in order to share my improvement ideas.

    The first error you can fix by using dataEntityViewStr function.

    EntityName entityName = dataEntityViewStr(SalesOrderV2Entity);

    About the second error, you need to provide a valid data source. Composite entity doesn't have any data sources, it's only a collection of other entities. You need to provide a valid data source of a normal entity.

    For example tableNum(SalesTable).

  • Suggested answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    Hi D365FO_user,

    For composite entities, the export process is a bit more complicated

    1. You need to create template data project and add your composite entity there

    2. Then you need to change a bit first part of your original code: find entity name line and you need to add an additional parameter for getDefaultQuery method - the name of template data project.

    EntityName      entityName;
    DMFEntity       dmfEntity;
    
    select firstonly dmfEntity
        order by EntityName asc
        where targetEntity == dataEntityViewStr(MyInvoiceCompositeEntity);
    
    entityName = dmfEntity.EntityName;
    
    Query                query = new Query(DMFUtil::getDefaultQueryForEntity(entityName, "MyTempalteDataProject"));
    QueryBuildDataSource qbds = query.dataSourceTable(tableNum(SalesInvoiceHeaderV2Entity));
    ...
    
    //Apply needed ranges 
    
    ...
    DMFEntityExporter exporter = new DMFEntityExporter();
    fileId = exporter.exportToFile(...

  • D365FO user Profile Picture
    465 on at

    Hi Nikolas,

    i tried ur way for entity name but after the line where i define the query, i got this error: "Object Query could not be created"

                EntityName           entityName = dataentityviewstr(SalesOrderV2Entity);
                Query                query = new Query(DMFUtil::getDefaultQueryForEntity(entityName));

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
Martin Dráb Profile Picture

Martin Dráb 611 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 285 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans