Sending Email using X++ Code (AX7 /D365 for Operations)

Friends,

I was just researching on the emailing capabilities in D365 and found that SysINetMail, SysMailer and some of the smmOutlook classes are deprecated because these classes used predominantly client-side technologies that are no longer available

Below is the quick code snippet that can be used in D365 to send emails. Please note, this is just a untested sample code, with out any setups configured. please ensure all setups are done and improvise the code accordingly.

SysMailerFactory class internally used SysImailer* classes to send emails.

class SRSendEmail_D365

{       

    public static void main(Args _args)

    {  

        SysMailerMessageBuilder messageBuilder = new SysMailerMessageBuilder();

        Email   toEmail;

        Email   fromEmail;

         

        try

        {

            FromEmail = "sreddy@xyzcompany.com";

            toEmail   = "tester@xyzcompany.com";

 

            messageBuilder.setBody("Hello from D365", false);

            messageBuilder.setSubject("Email Test from D365");

            messageBuilder.addTo(toEmail);

            // Note: not calling setFrom() defaults to the current user for SMTP client, whereas

            // when sent to Outlook any setFrom() value will be ignored since profiles on the client are used

            messageBuilder.setFrom(fromEmail);

 

            // Open the generated email in the configured client

            // Sends a message interactively, allowing the user to view and modify the message before

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

            // Try using sendNonInteractive method too

        }

        catch (Exception::Error)

        {

            throw error("@SYS33567");

        }

    }

 

}

Happy Dax6ng,

image

Advertisements

Write to Excel (Create an Excel) using X++ Code (AX 7/D365 for Operations)

Hi all,

Below is the quick code snippet to export the data to excel/create an excel in AX 7/D365 for operations

Please note, in D365 we can achieve this through OfficeOpenXml namespace

Improvise it based on your requirement

using System.IO;

using OfficeOpenXml;

using OfficeOpenXml.Style;

using OfficeOpenXml.Table;

class SRWriteToExcel

{

    public static void main(Args _args)

    {

        CustTable custTable;

        MemoryStream memoryStream = new MemoryStream();

 

        using (var package = new ExcelPackage(memoryStream))

        {

            var currentRow = 1;

 

            var worksheets = package.get_Workbook().get_Worksheets();

            var CustTableWorksheet = worksheets.Add("Export");

            var cells = CustTableWorksheet.get_Cells();

            OfficeOpenXml.ExcelRange cell = cells.get_Item(currentRow, 1);

            System.String value = "Account Number";

            cell.set_Value(value);

            cell = null;

            value = "Currency";

            cell = cells.get_Item(currentRow, 2);

            cell.set_Value(value);

 

            while select CustTable

            {

                currentRow ++;

                cell = null;

 

                cell = cells.get_Item(currentRow, 1);

                cell.set_Value(CustTable.AccountNum);

                cell = null;

 

                cell = cells.get_Item(currentRow, 2);

                cell.set_Value(CustTable.Currency);

            }

            package.Save();

            file::SendFileToUser(memoryStream, ‘Test’);

           

        }

       

    }

 

}

Happy Dax6ng,

image

Read from Excel using X++ Code (AX7/ D365 for Operations)

Hi Friends,

Been long. Hope all is well. Work kept me busy.

Below is the code snippet which will be handy to read the data from excel using code in D365/ax 7

Please note that, in D365 SysExcel* classes have been deprecated.

Use OfficeOpenXML namespace to achieve this. Improvise the below snippet based on your need.

using System.IO;

using OfficeOpenXml;

using OfficeOpenXml.ExcelPackage;

using OfficeOpenXml.ExcelRange;

 

class SRReadFromExcel_D365

{       

    public static void main(Args _args)

    { 

        System.IO.Stream            stream;

        ExcelSpreadsheetName        sheeet;

        FileUploadBuild             fileUpload;

        DialogGroup                 dlgUploadGroup;

        FileUploadBuild             fileUploadBuild;

        FormBuildControl            formBuildControl;

        Dialog                      dialog = new Dialog("Import the data from Excel");

 

        dlgUploadGroup          = dialog.addGroup("@SYS54759");

        formBuildControl        = dialog.formBuildDesign().control(dlgUploadGroup.name());

        fileUploadBuild         = formBuildControl.addControlEx(classstr(FileUpload), ‘Upload’);

        fileUploadBuild.style(FileUploadStyle::MinimalWithFilename);

        fileUploadBuild.fileTypesAccepted(‘.xlsx’);

 

        if (dialog.run() && dialog.closedOk())

        {

            FileUpload fileUploadControl     = dialog.formRun().control(dialog.formRun().controlId(‘Upload’));

            FileUploadTemporaryStorageResult fileUploadResult = fileUploadControl.getFileUploadResult();

 

            if (fileUploadResult != null && fileUploadResult.getUploadStatus())

            {

                stream = fileUploadResult.openResult();

                using (ExcelPackage Package = new ExcelPackage(stream))

                {

                    int                         rowCount, i;

                    Package.Load(stream);

                    ExcelWorksheet  worksheet   = package.get_Workbook().get_Worksheets().get_Item(1);

                    OfficeOpenXml.ExcelRange    range       = worksheet.Cells;

                    rowCount                  = worksheet.Dimension.End.Row – worksheet.Dimension.Start.Row + 1;

 

                    for (i = 2; i<= rowCount; i++)

                    {

                        info(range.get_Item(i, 1).value);

                        info(range.get_Item(i, 2).value);

                    }

                }

            }

            else

            {

                error("Error here");

            }

 

        }

    }

 

}

Happy Dax6ng

image