Skip to main content

Notifications

How to send SalesPackingSlip report over the email to Customer in D365FO using X++ code

Hi All,
In this blog we are going to learn about how to generate SalesPacking slip report and send it over the customers primary email address.



X++ Code.

Step 1 :Create new class.

internal final class TestPackingSlipJournalExportService
{

    SRSCatalogItemName          reportDesignLocal;
    str                         documentTitle;
    System.IO.MemoryStream      reportMemoryStream;
    SRSPrintMediumType          printMedium;
    LanguageId                  languageId;
    System.Byte[]               reportBytes;

public static TestPackingSlipJournalExportService construct(SRSPrintMediumType _printMedium)
{

        TestPackingSlipJournalExportService    service = new TestPackingSlipJournalExportService();

        service.parmPrintMedium(_printMedium);

        return service;

}

public SRSPrintMediumType parmPrintMedium(SRSPrintMediumType _printMedium = printMedium){

        printMedium = _printMedium;
        return printMedium;
 }

   /// <summary>
    /// Export packing slip journal.
    /// </summary>
    /// <param name = "_custPackingSlipJourRecId"></param>
    /// <param name = "_fileName">File name</param>
    /// <returns>Report in byte</returns>

public System.Byte[] exportPackingSlipJournal(RecId _custPackingSlipJourRecId, str _fileName)
{

        SalesPackingSlipContract    salesPackingSlipContract;
        SalesPackingSlipController  formLetterController        = SalesPackingSlipController::construct();        CustPackingSlipJour         custPackingSlipJour  = CustPackingSlipJour::findRecId(_custPackingSlipJourRecId);

        Common                      printMgmtReferencedTable;
        FormLetterReport            formLetterReport;
        PrintMgmtPrintSettingDetail printSettingDetail;
        SalesTable                  salesTable = custPackingSlipJour.salesTable();

        PrintMgmtReportFormatName   printMgmtReportFormatName   = PrintMgmtDocType::construct(PrintMgmtDocumentType::SalesOrderPackingSlip).getDefaultReportFormat();

        reportDesignLocal = printMgmtReportFormatName;

     if (!reportDesignLocal)
    {
            reportDesignLocal = ssrsReportStr(SalesPackingSlip, Report)
     }

      formLetterReport = FormLetterReport::construct(PrintMgmtDocumentType::SalesOrderPackingSlip);

        // Determine where to start looking for Print Mgmt settings
        printMgmtReferencedTable = salesTable;

        languageId = salesTable.LanguageId ? salesTable.LanguageId : custPackingSlipJour.LanguageId;

        formLetterReport.loadPrintSettings(custPackingSlipJour, printMgmtReferencedTable, custPackingSlipJour.LanguageId);

         if (formLetterReport.moveNextPrintSetting())
        {

            printSettingDetail = formLetterReport.getCurrentPrintSetting();

            formLetterReport.parmReportRun().loadSettingDetail(printSettingDetail);

        }

        formLetterReport.parmPrintType(PrintCopyOriginal::Original);
       salesPackingSlipContract = new SalesPackingSlipContract();


        salesPackingSlipContract.parmRecordId(custPackingSlipJour.RecId);
        salesPackingSlipContract.parmTableId(custPackingSlipJour.TableId);

        salesPackingSlipContract.parmDocumentTitle("@ExtendedItallianLocalization:DeliveryNote");

        this.generateReport(salesPackingSlipContract, _fileName);

        return this.getReportBytes();

    }

    private void generateReport(Object _rdpContract, str _fileName)
    {

        SrsReportRunController  srsReportRunController = new SrsReportRunController();
        srsReportRunController.parmReportName(reportDesignLocal);
        srsReportRunController.parmExecutionMode(SysOperationExecutionMode::Synchronous);
        srsReportRunController.parmShowDialog(false);
        srsReportRunController.parmReportContract().parmRdpContract(_rdpContract);

        if (languageId)
        {            srsReportRunController.parmReportContract().parmRdlContract().parmLanguageId(languageId);            srsReportRunController.parmReportContract().parmRdlContract().parmLabelLanguageId(languageId);       }

        srsReportRunController.parmReportContract().parmReportExecutionInfo(new SRSReportExecutionInfo());
       srsReportRunController.parmReportContract().parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration());

        SRSPrintDestinationSettings printerSettings = srsReportRunController.parmReportContract().parmPrintSettings();

        printerSettings.printMediumType(printMedium);
        printerSettings.fileFormat(SRSReportFileFormat::PDF);
        printerSettings.parmFileName(_fileName);
        printerSettings.overwriteFile(true);
        SRSReportRunService srsReportRunService = new SrsReportRunService();

       srsReportRunService.getReportDataContract(srsReportRunController.parmReportContract().parmReportName());

     srsReportRunService.preRunReport(srsReportRunController.parmReportContract());

        Map reportParametersMap = srsReportRunService.createParamMapFromContract(srsReportRunController.parmReportContract());

        Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[]  parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);

        SRSProxy srsProxy = SRSProxy::constructWithConfiguration(srsReportRunController.parmReportContract().parmReportServerConfig());

           // Actual rendering to byte array
        reportBytes = srsproxy.renderReportToByteArray(srsReportRunController.parmreportcontract().parmreportpath(),                                        parameterValueArray,
                                        printerSettings.fileFormat(),
                                        printerSettings.deviceinfo());
    }

    public System.Byte[] getReportBytes()
    {
        return reportBytes;
    }

}


Step 2: Call the above created class and get the report bytes.

Step 3: Covert the report byte into stream and pass it as attachment to email.

You can get below 3 parameters like below.

1)CustPackingSlipJour = CustPackingSlipJour::findRecId(_custPackingSlipJourRecId);
2)Email = CustPackingSlipJour.salesTable().customerEmail();
3)FileName = "Test.pdf"

public static void sendPackingSlipByEmail(CustPackingSlipJour _custPackingSlipJour, Email _ToEmail, Filename _fileName)
{
       TestPackingSlipJournalExportService service = TestPackingSlipJournalExportService::construct(SRSPrintMediumType::File);

      System.Byte[] reportBytes = service.exportPackingSlipJournal(_custPackingSlipJour.RecId,_fileName);

        if (reportBytes)
        {

            SysMailerMessageBuilder messageBuilder = new SysMailerMessageBuilder();

            try
            {
                System.IO.Stream stream =   new System.IO.MemoryStream(reportBytes);

                stream.Position = 0;
                messageBuilder.setBody("Packing Slip id - " + _custPackingSlipJour.PackingSlipId);
                messageBuilder.setSubject("Packing slip journal confirmation");
                messageBuilder.addTo(_ToEmail);
                messageBuilder.addAttachment(stream, _filename);
                messageBuilder.setFrom('no-reply@Test.com');

               SysMailerFactory::sendNonInteractive(messageBuilder.getMessage());

            }
            catch
            {
                exceptionTextFallThrough();
            }

        }

    }

Step 4: Result in email




This was originally posted here.

Comments

*This post is locked for comments