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 check if exported file is empty or not ? by code

(0) ShareShare
ReportReport
Posted on by 1,552

So i'm exporting an entity by code (incremental push) using batch process every 5 minutes and each time this file gets exported i send it to a third system.

What is happening is that sometimes i get empty files sent because there is no change that happened during these 5 minutes. How can i prevent sending empty files? how can i check it?

        fileId = exporter.exportToFile(entityName,
                                _definitionGroupName,
                                '', //Optional: ExecutionID
                                "XML-Element", //Optional::SourceName
                                #FieldGroupName_AllFields, //Optional field selection
                                query.pack(), //Optional: Filtered Query
                                curExt() //Optional: DataAReaId
                                );

I have the same question (0)
  • Suggested answer
    mhdshb1 Profile Picture
    1,250 on at

    Hi,

    Are you using the recurring data jobs?

    If so it is simply done by configuring the  Prevent upload when zero records 

    check this link for more details

    Hope this helps,

    Best regards,

    M

  • Sergei Minozhenko Profile Picture
    23,093 on at

    Hi Junior AX, 

    So you are using custom batch job instead of recurring jobs? Is there any reason to do it? Recurring data jobs has built-in features to skip empty runs. 

  • junior AX Profile Picture
    1,552 on at

    Hi Mohammad and Sergie,

    I'm not using recurring jobs.. because after the export i send the file to azure blob by getting the downloaded url. I'm not sure if i can do that in recurring jobs.

      str downloadUrl = DMFDataPopulation::getAzureBlobReadUrl(str2Guid(_fileId));

    So how can i check if the file is empty before sending to blob?

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

    Hi Junior AX,

    There is API for recurring export jobs are available in F&O. Do you think 3rd party system can use it to get messages from F&O if you start using recurring jobs?

    docs.microsoft.com/.../recurring-integrations

  • junior AX Profile Picture
    1,552 on at

    Hi Sergie,

    does that mean i need to build an api for the third system to read from?

    i already developed the solution, is there any way that  i can check if the file is empty by code?

  • Suggested answer
    mhdshb1 Profile Picture
    1,250 on at

    Hi ,

    Yes as Sergei mentioned there is API to export the jobs, The purpose of the Recurring integration is what are you trying to do. So I prefer if you use this approach and no need to reinvent the wheel.

    Use this reference to test the service from third parties

    docs.microsoft.com/.../third-party-service-test

    There are many examples that you can find in the web.

    Regards,

    M

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

    Hi Junior AX,

    Your solution is its own implementation of recurring jobs, and I would also recommend using standard recurring jobs instead of a custom one. In this case, one change you need to develop is an entity (if you can't use standard) and it's much easier to support it and with a custom solution. Also, you can't get support from MS.

    Anyway, if you want to continue with your own solution, there is plenty of examples of how to check if XML is empty in C# in the internet, but you need to download the file from blob storage to stream and read it first.

    There are some related links:

    stackoverflow.com/.../wget-curl-in-c-sharp

    stackoverflow.com/.../how-to-check-if-xml-file-is-empty-or-not-using-c-sharp

  • junior AX Profile Picture
    1,552 on at

    Hi Mohammed and Sergie,

    can i send the file to azure blob using recurring integrations? if yes how?

    and if i want to continue using my own solution, then i want to check if the file is empty before sending to azure blob. There is no need for my file to reach there if it's empty, so how can i do that in x ?

    System.IO.Stream stream = File::UseFileFromURL(_downloadUrl);

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

    Hi Junior AX,

    If you want to use recurring integration, you need to use API for export to download the file as was mentioned before. Depends on your requirement it can be done by the 3rd party system, or you can utilize logic apps (or something similar) to get a file from API and move it further.

    docs.microsoft.com/.../recurring-integrations

    To read XML from the stream you can use XmlDocument and static method newFromStream to create it. Then you need to find the first occurrence for entity element and check if at least one field is not empty and it's better to use mandatory field to check if it's not empty (empty export generates 1 empty row in the XML anyway).

    www.axaptapedia.com/XMLDocument_class

  • junior AX Profile Picture
    1,552 on at

    Hi Sergie,

    So if i use recurring integration and i make it run every 5 minutes. The api will automatically download the file each 5 minutes?

    That's why i did it by code, i just export the entity by code and send it to azure blob where bizTalk can take it from there. I'm not sure if recurring integration will help me in this

    I already have the xmlDocument i guess based on the code below, so need to create it... can you please help me more in writing this?

    fileId = exporter.exportToFile(entityName,
                                    _definitionGroupName,
                                    '', //Optional: ExecutionID
                                    "XML-Element", //Optional::SourceName
                                    #FieldGroupName_AllFields, //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('%1%2',newGuid(),fileExt);
            System.IO.Stream stream = File::UseFileFromURL(downloadUrl);
    
    
            Microsoft.WindowsAzure.Storage.Auth.StorageCredentials  storageCredentials  = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials("", "");
    
            Microsoft.WindowsAzure.Storage.CloudStorageAccount      storageAccount      = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, true);
    
            var                                                     blobcli             = storageAccount.CreateCloudBlobClient();
    
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer  cont                = blobcli.GetContainerReference("");
    
            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob      cloudBlockBlob      = cont.GetBlockBlobReference(fileName);
    
            cloudBlockBlob.UploadFromStream(stream,null,null,null);

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... 456 Super User 2025 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 429 Most Valuable Professional

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 239 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans