Hi, Readers.
I received an interesting question this morning.
When we send sales invoice or service invoice by Email, customer’s email address is the default “Email-to” email address which is shown on the popup dialogue. However, I don’t want to use customer’s email address. I created a new field in customer card called “invoice email”. I want to change the default Email address to “invoice email”. How can I achieve it in the code?
So in this post, I would like to briefly talk about how to change default customer/vendor email address when sending emails.
First let’s look at the situation mentioned in the question.
For example, when sending Confirmation in Sales Orders, the email address of the Email on the Customer Card is automatically obtained.
The same goes for Posted Sales Invoice.
Of course, for the same customer, the person receiving the order and the person receiving the invoice may not be the same. In this case do we have to customize it?
No, in fact, BC comes with another powerful feature, Document Layouts for Customers and Vendors.
Document layouts use report layouts to define the look and feel of documents that you send to customers and vendors. Business Central provides standard layouts, but you can also tailor custom layouts for each of your business partners.
Document layouts can also save you time when you send documents to customer or vendor contacts by email. For each layout that you assign to the customer or contact, you can by specify one or more contact email addresses. For example, you can send an invoice to the customer’s purchasing and warehouse contacts. Adding contact email addresses is easy. On the Document Layouts page, the Select Email from Contacts action let’s you choose from a list of the contact email addresses that you’ve registered for the customer or vendor. You can also add email addresses manually. If you enter multiple addresses, separate them with a semi-colon, and don’t add spaces between the addresses.
https://learn.microsoft.com/en-us/dynamics365/business-central/ui-define-customer-vendor-document-layouts
Let’s do a simple test.
Choose Document Layouts in Customer Card.
Select Usage and Report ID for the Customer and then add Send To Email.
Go back to Posted Sales Invoice, and when you click Send Email again, you will find that Send To has been updated. (Since only the new email address is set for Invoice usage, it has no effect on other Document types.)
Test Video:
Pretty simple, isn’t it?😁
PS: The Document Layouts feature is also available for Vendors.
Let’s get back to the question at the beginning, is this possible if we really have to customize?
I just looked into it and it’s possible. But I still recommend using the standard features.
Let’s see how to do it.
First, add a new field, Invoice Email, to the customer table as required.
Then add the control logic code. I specified in the code that it only takes effect when the Usage is Invoice and the newly added field, Invoice Email is not empty.
Test Video: This can achieve a similar effect to the Document Layouts function.
Source Code: Please note that this may not be the only method of customization and is for reference only
codeunit 50100 ZYEmailHandler
{
[EventSubscriber(ObjectType::Table, Database::"Report Selections", 'OnBeforeGetEmailAddress', '', false, false)]
local procedure OnBeforeGetEmailAddress(ReportUsage: Option; RecordVariant: Variant; var TempBodyReportSelections: Record "Report Selections"; var EmailAddress: Text[250]; var IsHandled: Boolean; CustNo: Code[20]);
var
Cust: Record Customer;
begin
if ReportUsage = Enum::"Report Selection Usage"::"S.Invoice".AsInteger() then
if Cust.Get(CustNo) then
if Cust."Invoice Email" <> '' then begin
EmailAddress := Cust."Invoice Email";
IsHandled := true;
end;
end;
}
tableextension 50112 ZYCustExt extends Customer
{
fields
{
field(50100; "Invoice Email"; Text[80])
{
Caption = 'Invoice Email';
ExtendedDatatype = EMail;
DataClassification = CustomerContent;
}
}
}
pageextension 50112 ZYCustCardExt extends "Customer Card"
{
layout
{
addafter("E-Mail")
{
field("Invoice Email"; Rec."Invoice Email")
{
ApplicationArea = All;
}
}
}
}
END
Hope this will help.
Thanks for reading.
ZHU
コメント