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 :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested Answer

Customer Statement Mail

(3) ShareShare
ReportReport
Posted on by 10
hello All ,
 
i am trying to create batch to create customer statement email, but the i am able to send with no customer statement content. However, the content does exist by manually with the same parameters. The code is shown as below.
 
_args.record(custTable);
 
 str ext = SRSPrintDestinationSettings::findFileNameType(SRSReportFileFormat::PDF, SRSImageFileFormat::BMP);
 fileName = custTable.AccountNum + ext;
 controller = new CustAccountStatementExtController();
 controller.parmReportName(reportName);
 contract = controller.parmReportContract().parmRdpContract();
 contract.parmFromDate(mkdate(1,1, 2000));
 contract.parmToDate(DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone()));
 contract.parmOnlyOpen(true);
 controller.parmArgs(_args);
 srsReportRun = controller.parmReportRun() as SrsReportRunImpl;
 controller.parmReportRun(srsReportRun);
 controller.parmReportContract().parmPrintSettings().printMediumType(SRSPrintMediumType::File);
 controller.parmReportContract().parmPrintSettings().overwriteFile(false);
 controller.parmReportContract().parmPrintSettings().fileFormat(SRSReportFileFormat::PDF);
 controller.parmReportContract().parmPrintSettings().fileName(fileName);
 controller.parmShowDialog(false);
 controller.startOperation();
 
 
 SRSReportRunService srsReportRunService = new SrsReportRunService();
 srsReportRunService.getReportDataContract(controller.parmReportContract().parmReportName());
 srsReportRunService.preRunReport(controller.parmReportContract());
 Map reportParametersMap = srsReportRunService.createParamMapFromContract(controller.parmReportContract());
 Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[] parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);
 SRSProxy srsProxy = SRSProxy::constructWithConfiguration(controller.parmReportContract().parmReportServerConfig());
 
 System.Byte[] reportBytes = srsproxy.renderReportToByteArray(controller.parmreportcontract().parmreportpath(),
 parameterValueArray,
 controller.parmReportContract().parmPrintSettings().fileFormat(),
 controller.parmReportContract().parmPrintSettings().deviceinfo());
 System.Net.Mime.ContentType contType = new System.Net.Mime.ContentType();
 contType.MediaType = 'application/pdf';
 if (reportBytes)
 {
 // Converting byte array to memory stream
 System.IO.MemoryStream stream = new System.IO.MemoryStream(reportBytes);
 System.Byte[] bytes = stream.ToArray();
 System.IO.MemoryStream newStream = new System.IO.MemoryStream(bytes);
 // Upload file to temp storage and direct the browser to the file URL
 //File::SendFileToUser(stream, controller.parmReportContract().parmPrintSettings().parmFileName());
 stream.Position = 0;
 str fileContentType = System.Web.MimeMapping::GetMimeMapping(fileName);
 contType.Name = fileName;
 // Attach the file to a record using stream object
 //addedRecord = DocumentManagement::attachFile(custTable.TableId,custTable.RecId,custTable.DataAreaId, 'File', stream, fileName, fileContentType,'PDF file attached');
 //sender = SysEmailParameters.SMTPUserName;
 //pwd = SysEmailParameters::password();
 sender = sysEmailTable.SenderAddr;
 
 email = custTable.email();
 ccEmail = '';
 ccEmail = CustParameters::find().CustomerSOACC;
 
 if (email)
 {
 var messageBuilder = new SysMailerMessageBuilder();
 if(ccEmail != '')
 {
 messageBuilder.addTo(email)
 .setSubject(strFmt("%1 - %2", SysEmailMessageTable::find(CustParameters::find().CustomerSOAEmailITemp,"en-us").Subject, custTable.name()))
 .setBody(SysEmailMessageTable::find(sysEmailTable.EmailId,'en-us').Mail)
 .addCC(ccEmail);
 }
 else
 {
 messageBuilder.addTo(email)
 .setSubject(strFmt("%1 - %2", SysEmailMessageTable::find(CustParameters::find().CustomerSOAEmailITemp,"en-us").Subject, custTable.name()))
 .setBody(SysEmailMessageTable::find(sysEmailTable.EmailId,'en-us').Mail);
 //.addCC(ccEmail);
 }
 
 
 if (sender)
 {
 messageBuilder.setFrom(sender,sysEmailTable.SenderName);
 }
 
 if (newStream != null)
 {
 messageBuilder.addAttachment(newStream,fileName,contType.MediaType);
 }
 SysMailerFactory::sendNonInteractive(messageBuilder.getMessage());
 //info("Email for Customer Account statement was sent Successfully..");
Categories:
I have the same question (0)
  • Suggested answer
    Saif Ali Sabri Profile Picture
    2,351 Super User 2025 Season 2 on at

    The issue arises because the report generation isn't completed before attempting to attach it. Use synchronous execution and retrieve the report output directly from the controller. Here's the corrected code snippet:

    xpp
    Copy
    // Replace startOperation() with run() to ensure synchronous execution
    controller.run();  // Instead of controller.startOperation();
    
    // Get the generated report output from the controller
    SrsReportRunImpl reportRun = controller.parmReportRun();
    System.IO.Stream stream = reportRun.getFirstOutput() as System.IO.Stream;
    
    // Reset stream position to 0 before attaching
    stream.Position = 0;
    
    // Attach the stream to the email
    if (stream != null && stream.Length > 0)
    {
        messageBuilder.addAttachment(stream, fileName, contType.MediaType);
    }

    Key Fixes:

    1. Synchronous Execution: Use controller.run() instead of startOperation() to ensure the report finishes generating.

    2. Direct Output Retrieval: Fetch the report stream from the controller’s reportRun instead of re-rendering via SRSProxy.

    3. Stream Position Reset: Ensure the stream’s position is 0 to avoid empty attachments.

    This ensures the PDF is generated and attached correctly in batch mode.

  • CU20031241-0 Profile Picture
    10 on at
     
    In d365 f&o we don't have the option to get the first output
     
    my pdf is again empty.
  • Suggested answer
    Saif Ali Sabri Profile Picture
    2,351 Super User 2025 Season 2 on at
    The error in your screenshot indicates that getFirstOutput() is not available in D365 F&O. Instead, you need to retrieve the report output using SRSProxy. Since your PDF is empty, follow these steps to fix the issue:

    Fixes:
    1. Ensure the Report is Fully Processed Before Retrieving Bytes
    Instead of using getFirstOutput(), use SRSProxy to get the report output:
    x++
    CopyEdit
    SRSProxy srsProxy = SRSProxy::constructWithConfiguration(controller.parmReportContract().parmReportServerConfig());

    System.Byte[] reportBytes = srsProxy.renderReportToByteArray(
        controller.parmReportContract().parmReportPath(),
        SrsReportRunUtil::getParameterValueArray(
            SrsReportRunService::createParamMapFromContract(controller.parmReportContract())),
        controller.parmReportContract().parmPrintSettings().fileFormat(),
        controller.parmReportContract().parmPrintSettings().deviceinfo());

    if (!reportBytes || reportBytes.Length == 0)
    {
        error("Report generation failed. The PDF is empty.");
    }
    📌 Why? This ensures the report is rendered properly before attempting to send it via email.

    2. Debug Report Rendering in Batch Mode
    Run this code manually and in batch to check if the report generates properly:
    x++
    CopyEdit
    info(strFmt("Report Path: %1", controller.parmReportContract().parmReportPath()));
    info(strFmt("Report Bytes Length: %1", reportBytes ? reportBytes.Length : 0));
    If reportBytes.Length == 0, then the report is not generating correctly in batch mode.

    3. Ensure the Batch User Has Report Execution Permissions
    • Go to System Administration > Security > Users
    • Check if the batch user has Report Viewer role
    • Make sure the batch user has access to CustAccountStatementExtController

    Summary of Fixes
    ✅ Use SRSProxy.renderReportToByteArray() instead of getFirstOutput()
    ✅ Add debugging to check if the report is generated in batch mode
    ✅ Ensure the batch job user has proper report execution permissions

    Try these steps and let me know if the issue persists! 🚀

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 > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 80

#3
Martin Dráb Profile Picture

Martin Dráb 64 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans