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

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
    237,976 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
    794 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
    141 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

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