Inkey Solution Logo
banner

Blogs

Creating Custom Email Subject in Dynamics 365 Business Central using AL Code

, October 13, 2023 1713 Views

Microsoft Dynamics 365 Business Central is a powerful Enterprise Resource Planning (ERP) solution that offers a wide range of customization options to meet the unique needs of your business. One such customization is the ability to dynamically create a custom email subject in Dynamics 365 Business Central using AL code for different document types, when sending emails from Business Central. This customization can be useful when you want to make your email subjects more descriptive and informative, making it easier for recipients to understand the context of the email.

Requirement

We recently encountered a requirement to customize the email subject when sending quotes and invoices to customers. Instead of using the Out-of-the-Box (OOTB) subject lines provided by Business Central

Prerequisites

  • Access to a Dynamics 365 Business Central instance with administrator privileges.
  • Visual Studio Code with AL Language extension installed.

Solution (Implementation Steps)

In Dynamics 365 Business Central, email subjects are generated by “Codeunit 260 – Document-Mailing” using the “GetEmailSubject” function. To customize the email subject, we will utilize an event, “OnBeforeGetEmailSubject”.

Step 1: Create a New AL Codeunit

  • In the Visual Studio Code, create a new AL codeunit.
  • Inside the codeunit, you can subscribe to the “OnBeforeGetEmailSubject” event to customize the email subject. You can do this by using the “[EventSubscriber]” attribute.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
codeunit 70100 INK_Email_Subject
{
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Document-Mailing", 'OnBeforeGetEmailSubject', '', false, false)]
local procedure MyProcedure(var EmailSubject: Text[250]; PostedDocNo: Code[20]; ReportUsage: Integer; var IsHandled: Boolean)
var
ReportUsageEnum: Enum "Report Selection Usage";
begin
// Convert ReportUsage from Integer to Enum
ReportUsageEnum := "Report Selection Usage".FromInteger(ReportUsage);
// Check if ReportUsage is "S.Quote"
if ReportUsageEnum = ReportUsageEnum::"S.Quote" then begin
// Customize the email subject for "S.Quote"
EmailSubject := CompanyProperty.DisplayName() + ' - Quote Number - ' + PostedDocNo;
IsHandled := true;
end
else
if ReportUsageEnum = ReportUsageEnum::"S.Invoice" then begin
// Customize the email subject for "S.Invoice"
EmailSubject := CompanyProperty.DisplayName() + ' - Invoice Number - ' + PostedDocNo;
IsHandled := true;
end;
end;
var
myInt: Integer;
}
codeunit 70100 INK_Email_Subject { [EventSubscriber(ObjectType::Codeunit, Codeunit::"Document-Mailing", 'OnBeforeGetEmailSubject', '', false, false)] local procedure MyProcedure(var EmailSubject: Text[250]; PostedDocNo: Code[20]; ReportUsage: Integer; var IsHandled: Boolean) var ReportUsageEnum: Enum "Report Selection Usage"; begin // Convert ReportUsage from Integer to Enum ReportUsageEnum := "Report Selection Usage".FromInteger(ReportUsage); // Check if ReportUsage is "S.Quote" if ReportUsageEnum = ReportUsageEnum::"S.Quote" then begin // Customize the email subject for "S.Quote" EmailSubject := CompanyProperty.DisplayName() + ' - Quote Number - ' + PostedDocNo; IsHandled := true; end else if ReportUsageEnum = ReportUsageEnum::"S.Invoice" then begin // Customize the email subject for "S.Invoice" EmailSubject := CompanyProperty.DisplayName() + ' - Invoice Number - ' + PostedDocNo; IsHandled := true; end; end; var myInt: Integer; }
codeunit 70100 INK_Email_Subject
{
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Document-Mailing", 'OnBeforeGetEmailSubject', '', false, false)]
    local procedure MyProcedure(var EmailSubject: Text[250]; PostedDocNo: Code[20]; ReportUsage: Integer; var IsHandled: Boolean)
    var
        ReportUsageEnum: Enum "Report Selection Usage";
    begin
        // Convert ReportUsage from Integer to Enum
        ReportUsageEnum := "Report Selection Usage".FromInteger(ReportUsage);
        // Check if ReportUsage is "S.Quote"
        if ReportUsageEnum = ReportUsageEnum::"S.Quote" then begin
            // Customize the email subject for "S.Quote"
            EmailSubject := CompanyProperty.DisplayName() + ' - Quote Number - ' + PostedDocNo;
            IsHandled := true;
        end
        else
            if ReportUsageEnum = ReportUsageEnum::"S.Invoice" then begin
                // Customize the email subject for "S.Invoice"
                EmailSubject := CompanyProperty.DisplayName() + ' - Invoice Number - ' + PostedDocNo;
                IsHandled := true;
            end;
    end;
    var
        myInt: Integer;
}

Step 2: Publish and Install the Extension

 After creating the codeunit, you’ll need to publish and install the extension in your Dynamics 365 Business Central instance.

Step 3: Test the Customization

  • With the extension installed, go to your Dynamics 365 Business Central instance and create or open a quote or invoice.
  • When you send an email for a quote or invoice, your custom email subject should be used instead of the out-of-the-box subject.
  • Test the customization thoroughly to ensure it works as expected and meets your business requirements

Output

Email Subject in Dynamics 365 Business Central using AL Code

Conclusion

Customizing email subjects in Microsoft Dynamics 365 Business Central using AL code can greatly improve your communication efficiency and help recipients better understand the context of your emails. Before deploying custom code in a production environment, thorough testing in a sandbox environment is essential to ensure that it functions as expected and does not disrupt your business processes. With this customization, you can enhance the communication by providing more informative and personalized email subjects for different document types.


ATM Inspection PowerApp to ease ATM inspection and report generation process.
https://www.inkeysolutions.com/microsoft-power-platform/power-app/atm-inspection

Insert data into Many-to-Many relationship in Dynamics CRM very easily & quickly, using the Drag and drop listbox.
http://www.inkeysolutions.com/what-we-do/dynamicscrmaddons/drag-and-drop-listbox

Comply your Lead, Contact, and User entities of D365 CRM with GDPR compliance using the GDPR add-on.
https://www.inkeysolutions.com/microsoft-dynamics-365/dynamicscrmaddons/gdpr

Create a personal / system view in Dynamics CRM with all the fields on the form/s which you select for a particular entity using the View Creator.
http://www.inkeysolutions.com/what-we-do/dynamicscrmaddons/view-creator

mm

Inkey

INKEY is your solution partner.
Our focus is to deliver you in-time intelligent innovative solutions ("key") for the problems in hand. Maintaining a quality standard right from the inception of a project is our top most priority.

Our team of talented professionals will execute your projects with dedication and excellence. We take ownership and accountability for the effort that goes into meeting our client’s needs.

Years of experience and proven success of delivering innovative custom solutions.

More posts by

Leave a Reply

Your email address will not be published. Required fields are marked *

The maximum upload file size: 2 MB. You can upload: image, audio, video, document, spreadsheet, interactive, text, archive, code, other. Drop file here

Would you like to digitize your business and put it on the cloud?
Do you need clear, concise reports for your organization?