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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

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

How to get an electronic report in an memory stream

(1) ShareShare
ReportReport
Posted on by 115
Hi experts, 
 
I am trying to get an electronic document in as bytes or memory stream to store it in an container in one of my custom tables. I was able to get the memory stream with the following code.
 
With the formatMappingRun.run() my ER document gets downloaded to the local and I was able to open and review the document. Post the run method, I got my report bytes with the help of the class ERFileDestinationMemory and stored that in a container. After this, I was able to see the ASCII value in the container but when I download the report from the saved container, a document gets saved with 5 times lesser in size than the document generated through formatMappingRun.run() method and it says an error that /We can't open this file - Something went wrong/ when I attempt to open it. 
 
I tried searching for similar logic in Microsoft classes but was not able to find. 
 
Can anyone guide me whether I went wrong somewhere in my code. Also, even if someone knows any functionality in F&O where Microsoft saves the ER report on generating would help as well. Thank you in advance. 
 
System.IO.MemoryStream         origStream, newStream;
        System.Byte[]                  reportBytes, newBytes;
        ERIFormatMappingRun            formatMappingRun;
        name                           fileName; 
        ERFileDestinationMemory        fileDestinationMemory;
        ERModelDefinitionInputParametersAction parameters;
        
        CustomTable  customTableBuffer, customTableUpdatebuffer;
        container                      blobContainer; 
        formatMappingRun      = ERObjectsFactory::createFormatMappingRunByFormatMappingId(customTableBuffer.ERTemplateId, /Er report/);
        fileDestinationMemory = new ERFileDestinationMemory();
        parameters            = this.constructParameter();
        formatMappingRun.withFileDestination(fileDestinationMemory);
        fileName = formatMappingRun.withParameter(parameters).run();
        reportBytes = fileDestinationMemory.GetByteArray(); 
        origStream = new System.IO.MemoryStream(reportBytes); 
        newBytes   = origStream.ToArray(); 
        newStream  = new System.IO.MemoryStream(newBytes); 
         
        customTableUpdatebuffer= customTableUpdatebuffer::findByRecId(customTableBuffer.RecId, true);
    
        customTableUpdatebuffer.AgreementFile           =  Binary::constructFromMemoryStream(newStream).getContainer();
        customTableUpdatebuffer.update();
I have the same question (0)
  • Martin Dráb Profile Picture
    238,745 Most Valuable Professional on at
    You didn't tell us what data format does the report generates, but it's unlikely that it's an ASCII string. Therefore interpreting the bytes as ASCII text sounds like a bug to me.
  • Arcadi  Profile Picture
    796 on at
    I'm also interested on this, I've been trying to find a way but I wasn't able to. What I found is a workaround: 
     
    In ER destinations select archive
    Now after running the ER you have an erRunJobTable, join it with  docuref and then 
     
    streamReader = new System.IO.StreamReader(DocumentManagement::getAttachmentStream(docuRef));
     
    But that's not ideal because you have to be sure that the generated ER file has a unique name. 
     
  • John Bright Profile Picture
    145 on at
    Hi Martin, 
        It is not an ASCII string my bad. Generally the report generates in pdf format and downloads to the local machine. But I am trying to get the memory stream and store those bytes in a container. Even though I was able to get the memory stream and store it, the PDF just doesn't opens. 
     
    Hi Arcadi, 
      Thanks for the suggestion. But this doesn't solve my problem since the requirement does not need an unique name whenever a document is generated. I tried developing a custom destination class and I received the memory stream from the destination class, even gave the same outcome. 
     
    Apologies for delayed response. 

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 545 Super User 2026 Season 1

#2
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 408

#3
Adis Profile Picture

Adis 267 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans