Blog

Blog

How to Change Subject, Body and Attachment File Name while Sending E-Mail ?

Posted on 12-Nov-2024 by Shrey Chauhan Associate Technical Consultant

People in businesses send information and documents, such as sales and purchase orders and invoices, by email every day. Administrators can connect one or more email accounts to Business Central, letting you send documents without having to open an email app. You can compose each message individually with basic formatting tools, such as fonts, styles, colors, and so on, and add attachments of up to 100 MB. Additionally, report layouts enable administrators to include only the key information from documents. Learn more at Send Documents by Email.

Email capabilities in Business Central are for outbound messages only. You can't receive replies, that is, there's no "Inbox" page.

You can send almost all types of documents as PDF attachments. Alternatively, you can set up a report layout that includes information from the document in the email text, along with text that makes the email more friendly, for example, a standard greeting. For more information, see Managing Report and Document Layouts.

When you send invoices, you can make it easier for customers to make payments through a payment service, such as PayPal, by automatically adding information and a link to the service in the email. For more information, see Enable Customer Payments Through Payment Services.

To enable emails from within Business Central, start the Set Up Email assisted setup guide. For more information, see Set Up Email.

Customizing Email Subject, Body, and Attachment Name

To change the subject, body, and attachment file name dynamically, you’ll need to create a Codeunit. Codeunits allow you to customize Business Central processes by adding code for specific events. Here’s a breakdown of the steps involved.

Step 1: Create a Codeunit

A Codeunit is essential for applying custom code to update email properties. Start by creating one for your email customization. Use shortcut Alt + Shift + E to view available events.

Here how its look


 

Step 2: Adding Events to Codeunit for Email Customization

After accessing the event drop-down menu, look for the Document-Mailing event. This is where you can add custom code to alter the attachment name, subject, and body.

Here’s the image for Changing the Attachment Name :-


Just click on the Codeunit “Document-Mailing

Customizing Attachment Names

Within the Codeunit, you’ll write specific code to rename attachments in emails. This can be done in two ways:


Now Let’s write the Code to update the Attachment Name

Hardcoded Naming (Red Box): The attachment name is set to a fixed string. Useful for consistent naming conventions.

Here’s the Output of Red Box code :-

This is the Sales Order I have created in business central and the Action is to Post and Send

Here’s the Mail its Creating

Dynamic Naming (Green Box): Populate the attachment name based on dynamic data like document type, customer name, or order number.

Here’s the Output of Green Box code :-

This is the Sales Order I have created in business central and the Action is to Post and Send

Here’s the Mail its Creating

Step 3: Customizing the Email Subject

Changing the email subject is similarly customizable:

Here’s the Event to Change the E-mail Subject and E-mail Document Name

Hardcoded Subject: Set a static subject line.

Here’s the Mail its Creating

Dynamic Subject Based on Document Data: Use specific values from the document, like invoice number or customer name.


Here’s the Code to change the Subject using values :

Here’s the Mail its Creating

Step 4: Updating the Email Body

To add a custom email body, use the following approach to insert values and provide more context to the recipient.

Here’s the Code to change the Body using values :

Here’s the Mail its Creating

Step 5: Automating the Email Process

Business Central also allows users to send emails without opening a dialog box, which can be useful for large batch operations. By integrating this custom code, you can bypass the manual sending step entirely.

Custom Code to Send Mail Directly without opening any dialog box

Conclusion

Customizing the email subject, body, and attachment name in Business Central provides a tailored and professional touch to your outgoing communications. By creating and configuring Codeunits, you can adapt emails to reflect specific document details, enhancing the client’s experience and ensuring that your emails stand out. This process streamlines document sharing and helps maintain a consistent and personalized communication strategy.


Post a Comment

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

Captcha
can't read? refresh

WhatsApp Now