Automating HTML Email Notifications in Microsoft Dynamics 365 Business Central - CloudFronts

Automating HTML Email Notifications in Microsoft Dynamics 365 Business Central

Introduction

In this blog, we will explore how to create HTML-formatted email notifications in Microsoft Dynamics 365 Business Central using AL code. We will guide you through a practical example that sends an HTML email notification when a Posted Purchase Invoice is inserted.

Pre-requisite

– Microsoft Dynamics 365 Business Central (On-premises or Cloud)

Objective

Our goal is to automatically send an HTML email containing purchase order details whenever a new Purchase Invoice Header is created.

Step-by-Step Implementation

Before diving into the code, you need to set up the email functionality in Business Central to ensure the system can send emails.

Step 1: Set Up Email in Business Central

Open Business Central:

– Sign in to your Business Central account.

– Search for “Set Up Email” in the top-right search bar.

Configure Email:

– Choose SMTP as the email type and click “Next.”

– Fill in the necessary details, such as the email account and authentication details, then click “Next” to finish the setup.

– Set the email account as the default if you have multiple email addresses.

Step 2: Create Necessary Fields in Table and Page Extensions

Add a Field in Table Extension:

– Create a boolean field named “GRN Notification” in the User Setup table extension. This field will ensure that the email is sent only to the users who require it.

tableextension 51328 UserSetupExt extends “User Setup”

{

    fields

    {

field(55005; “GRN Notification”; Boolean)

        {

            DataClassification = CustomerContent;

        }

    }

}

Add a Field in Page Extension:

– Add the “GRN Notification” field to the User Setup page extension to allow users to enable or disable notifications.

pageextension50102 extends “User Setup”

{

    layout

    {

addafter(“Register Time”)

          {

field(“GRN Notification”; Rec.”GRN Notification”)

                    {

                        ApplicationArea = All;

                    }

}

    }

}

Step 3: Create a Table Extension for the Purchase Invoice Header

This is where we extend the Purch. Inv. Header table to trigger a procedure that sends the email when a new record is inserted.

tableextension 50101 PurchaseInvoiceHeader extends “Purch. Inv. Header”
{
    trigger OnInsert()
    begin
        GRNPostingtoPO(Rec);
    end;

Step 4: Define the GRNPostingtoPO Procedure

This procedure handles the core logic of the email notification:

    procedure GRNPostingtoPO(PurchaseInvoiceHeader: Record “Purch. Inv. Header”)

    var

        UserSetup: Record “User Setup”;

        EmailMessage: Codeunit “Email Message”;

        Email: Codeunit “Email”;

        PurchaseLine: Record “Purchase Line”;

        PurchaseHeader: Record “Purchase Header”;

        HtmlBody: Text;

    begin

        // Find the corresponding Purchase Header using the “Order No.”

        PurchaseHeader.SetRange(“No.”, PurchaseInvoiceHeader.”Order No.”);

       // If the Purchase Header exists, retrieve related Purchase Lines.

        if PurchaseHeader.FindFirst() then begin

            PurchaseLine.SetRange(“Document No.”, PurchaseHeader.”No.”);

            if PurchaseLine.FindSet() then begin

                // Build the HTML email body with purchase order details.

                HtmlBody := ‘Hello Team,’ +

                            ‘<p>Please find the attached purchase order details.</p>’ + ‘<BR>’ +

                            ‘<p>Purchase Order has been created successfully.</p>’ +

                            ‘<h3>Purchase Order No. ‘ + PurchaseInvoiceHeader.”No.” + ‘</h3>’ +

                            ‘<table border=”1″ style=”border-collapse: collapse; width: 100%;”>’ +

                            ‘<tr>’ +

                            ‘<th style=”padding: 8px; text-align: left; background-color: #f2f2f2;”>IDS No.</th>’ +

                            ‘<th style=”padding: 8px; text-align: left; background-color: #f2f2f2;”>ITEM No.</th>’ +

                            ‘<th style=”padding: 8px; text-align: left; background-color: #f2f2f2;”>Item Description</th>’ +

                            ‘<th style=”padding: 8px; text-align: left; background-color: #f2f2f2;”>Quantity</th>’ +

                            ‘</tr>’;

                // Loop through the Purchase Lines to add them to the HTML body.

                repeat

                    HtmlBody += ‘<tr>’ +

                                ‘<td style=”padding: 8px;”>’ + PurchaseLine.”Document No.” + ‘</td>’ +

                                ‘<td style=”padding: 8px;”>’ + PurchaseLine.”No.” + ‘</td>’ +

                                ‘<td style=”padding: 8px;”>’ + PurchaseLine.Description + ‘</td>’ +

                                ‘<td style=”padding: 8px;”>’ + Format(PurchaseLine.Quantity) + ‘</td>’ +

                                ‘</tr>’;

                until PurchaseLine.Next() = 0;

                // Close the HTML table and body.

                HtmlBody += ‘</table>’ +

                            ‘<p>This is an Auto-generated mail, if any concerns related to purchase please contact the ERP Team.</p>’ +

                            ‘</body></html>’;

                // Send the email to users who have GRN Notification enabled.

                UserSetup.SetRange(“GRN Notification”, true);

                if UserSetup.FindSet() then begin

                    repeat

                        EmailMessage.Create(

                            UserSetup.”E-Mail”,

                            ‘Purchase Order Posted’,

                            HtmlBody,

                            true

                        );

                        Email.Send(EmailMessage, Enum::”Email Scenario”::Default);

                    until UserSetup.Next() = 0;

                end;

            end;

        end;

    end;

Output:

Conclusion

By following these steps, you can create HTML-formatted email notifications in Microsoft Dynamics 365 Business Central. This method ensures that users receive detailed and well-structured notifications, which enhances communication and workflow efficiency within your organization.

We hope you found this article useful, and if you would like to discuss anything, you can reach out to us at [email protected]


Share Story :

Secured By miniOrange