Create an excel file and send it through Email in D365 using X++ code

          This post is about creating an excel file using X++ and attach it as an Email(office 365 SMTP) attachment without saving it in local file location. 


SysMailerMessageBuilder         mailer  = new SysMailerMessageBuilder();
SysMailerSMTP                   smtp    = new SysMailerSMTP();
RowNumber                       row     = 1 ;
RESPDCLogTable                  pDCLogTable1;
System.IO.MemoryStream memoryStream     = new System.IO.MemoryStream();
mailer.setSubject(strFmt("PDC posted batch status %1",sessionDate));
mailer.setFrom(SysUserInfo::find().Email);
mailer.setBody('Please find the PDC Status report.');
mailer.addTo('ToMailAddress');
 
 using (var package = new OfficeOpenXml.ExcelPackage(memoryStream))
 {
     var worksheets = package.get_Workbook().get_Worksheets();
     var worksheet  = worksheets.Add("PDC batch status");
     var cells      = worksheet.get_Cells();
     var cell       = cells.get_Item(1,1);
     cell.set_Value("PDC Register Id");
     cell = cells.get_Item(1,2);
     cell.set_Value("Check number");
     cell = cells.get_Item(1,3);
     cell.set_Value("Agreement");
     cell = cells.get_Item(1,4);
     cell.set_Value("Customer account");
     cell = cells.get_Item(1,5);
     cell.set_Value("Status");
     cell = cells.get_Item(1,6);
     cell.set_Value("Error");
     while select pDCLogTable1
     {
        row++;
 
        cell = cells.get_item(row, 1);
        cell.set_Value(pDCLogTable1.PDCRegisterId);
 
        cell = cells.get_item(row, 2);
        cell.set_Value(pDCLogTable1.CheckNumber);
 
        cell = cells.get_item(row, 3);
        cell.set_Value(pDCLogTable1.AgreementId);
 
        cell = cells.get_item(row, 4);
        cell.set_Value(pDCLogTable1.CustAccount);
 
        if(pDCLogTable1.LogStatus == RESPDCLogStatus::Error)
        {
           cell = cells.get_item(row, 5);
           cell.set_Value("Error");
        }
        else if(pDCLogTable1.LogStatus == RESPDCLogStatus::Posted)
        {
           cell = cells.get_item(row, 5);
           cell.set_Value("Posted");
        }
 
        cell = cells.get_item(row, 6);
        cell.set_Value(pDCLogTable1.ErrorMsg);
 
     }
        package.Save();
 
 }
       memoryStream.Seek(0, System.IO.SeekOrigin::Begin);
       mailer.addAttachment(memoryStream, strFmt("PDCStatus%1.xlsx",sessionDate), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        try
        {
           smtp.sendNonInteractive(mailer.getMessage());
        }
        catch(Exception::CLRError)
        {
           error(CLRInterop::getLastException().toString());
        } 
Output: 

Mail screenshot:


Excel screenshot:




Comments

Popular posts from this blog

SSRS Report using Controller , Contract and RDP classes in D365

Exporting data to Excel through X++ code

COC for Form level method

How to pass the parameter from one form to another in Dynamic365

Multi Select Lookup in SSRS Report in D365