<< Click to Display Table of Contents >> Navigation: Zetadocs SDK Guide > Creating a Per Tenant Extension > Emailing Customization |
You can customize the emailing process to fit your requirements. For instance
Modify the Email To/Cc/Bcc to Use the Ship-To Email Example
This example uses the OnAfterGenerateSendResults event to take the existing list of Emails and storing them in the Email Cc field, then if found using the Ship To Email for that Sales Order, finally the user logged into Business Central is being added as a BCc to the emails.
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Zetadocs Customize", 'OnAfterGenerateSendResults', '', true, true)]
local procedure OnAfterGenerateSendResults(var onAfterGenerateSendResultsHandler: Codeunit "Zdd OnAfterGenerateSendResults")
var
shipToList: Record "Ship-to Address";
user: Record User;
record: RecordId;
RecRef: RecordRef;
sellToCustomer: Code[20];
shipToCode: Code[10];
emailList: List of [Text];
originalEmailList: List of [Text];
bCcList: List of [Text];
begin
onAfterGenerateSendResultsHandler.TryGetRecordId(record);
//For Sales Order lets take the email address from the Sell-To field, and add some Cc emails as well
36:
if (RecRef.Get(record)) then begin
//Get the current list of additional email addresses then make them CC's as well
onAfterGenerateSendResultsHandler.GetEmailTo(originalEmailList);
onAfterGenerateSendResultsHandler.SetEmailCc(originalEmailList);
//I want to be copied on every SO sent out so we'll add myself as a BCc
user.Get(UserSecurityId());
bCcList.Add(user."Contact Email");
onAfterGenerateSendResultsHandler.SetEmailBCc(bCcList);
// Set the EmailTo address from the Ship-To Email address assigned against the Sales Order
sellToCustomer := RecRef.Field(2).Value;
shipToCode := RecRef.Field(12).Value;
if (shipToCode > '') then begin
if (shipToList.Get(sellToCustomer, shipToCode)) then begin
//Use the ship to email address instead of the usual sell-to email.
emailList.Add(shipToList."E-Mail");
onAfterGenerateSendResultsHandler.SetEmailTo(emailList);
onAfterGenerateSendResultsHandler.SetContactNameTo(shipToList.Contact);
end;
end;
end;
end;
//Propagate any error to the caller
onAfterGenerateSendResultsHandler.PropagateLastError();
end;
Change the Document Template Example
The example uses the OnAfterGenerateSendResults event to adjust the template depending on the Reminder Level, two new templates have been uploaded via the Zetadocs Templates page and assigned codes ZT00019 and ZT00020, they may well be different on your system.
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Zetadocs Customize", 'OnAfterGenerateSendResults', '', true, true)]
local procedure OnAfterGenerateSendResults(var onAfterGenerateSendResultsHandler: Codeunit "Zdd OnAfterGenerateSendResults")
var
record: RecordId;
RecRef: RecordRef;
reminderLevel: Integer;
begin
onAfterGenerateSendResultsHandler.TryGetRecordId(record);
case record.TableNo() of
//For Issued Reminders, check the reminderLevel and swap the template according the level
297:
begin
if (RecRef.Get(record)) then begin
reminderLevel := RecRef.Field(28).Value;
case (reminderLevel) of
0:
//leave the template as the default template
;
1:
//leave the template as the default template
;
2:
onAfterGenerateSendResultsHandler.SetTemplate('ZT00020');
3:
onAfterGenerateSendResultsHandler.SetTemplate('ZT00019');
end;
end;
end;
end;
//Propagate any error to the caller
onAfterGenerateSendResultsHandler.PropagateLastError();
end;
The following code shows how the use the OnBeforeDelivery Event to add data that will be used in the template. This example will add the Sales Peron's name to the dicitonary of template values so that all reports that use the 'Sales Header' table will be have a more personalized email coming from the appropiate sales person. The relevant dynamic field %%[FieldName] or %%[FieldName, defaultValue] will need to be added to the template file/s, see Zetadocs Templates.
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Zetadocs Customize", 'OnBeforeDelivery', '', true, true)]
procedure OnBeforeDelivery(var onBeforeDeliveryHandler: Codeunit "Zetadocs OnBeforeDelivery")
var
salePerson: Record "Salesperson/Purchaser";
SalesHeader: Record "Sales Header";
recordId: RecordId;
recordIds: List of [RecordId];
isSuccess: Boolean;
begin
//Get each record (per recipient)
isSuccess := onBeforeDeliveryHandler.TryGetRecordIds(recordIds);
if (isSuccess) then begin
//Get the first record as they'll all have the same TableNo
recordId := recordIds.Get(1);
case (recordId.TableNo()) of
36:
begin
//Set the template value
SalesHeader.Get(RecordId);
if (salePerson.Get(SalesHeader."Salesperson Code")) then begin
// 'SalesPersonName must match the %%[SalesPersonName] in the template
onBeforeDeliveryHandler.AddOrUpdateTemplateValue('SalesPersonName', salePerson.Name);
end;
end;
end;
end;
//Propagate any error to the caller
onBeforeDeliveryHandler.PropagateLastError();
end;
}
The following code shows how the use the OnBeforeDelivery Event to rename the report file to be emailed. This example will rename the file to include additional information as the date or the user sending the email.
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Zetadocs Customize", 'OnBeforeDelivery', '', true, true)]
procedure OnBeforeDelivery(var onBeforeDeliveryHandler: Codeunit "Zetadocs OnBeforeDelivery")
var
reports: List of [Text];
reportName: Text;
begin
//Rename reports
onBeforeDeliveryHandler.GetReports(reports);
if (reports.Count > 1) then appendFileName := true;
foreach reportName in reports do begin
if (appendFileName) then begin
counter := counter + 1;
onBeforeDeliveryHandler.RenameReport(reportName, StrSubstNo('%1 %2 %3-%4.pdf', FORMAT(TODAY, 0, 9), reportName.TrimEnd('.pdf'), onBeforeDeliveryHandler.GetSendingUser, counter));
end else begin
onBeforeDeliveryHandler.RenameReport(reportName, StrSubstNo('%1 %2 %3.pdf', FORMAT(TODAY, 0, 9), reportName.TrimEnd('.pdf'), onBeforeDeliveryHandler.GetSendingUser));
end;
end;
//Propagate any error to the caller
onBeforeDeliveryHandler.PropagateLastError();
end;