<< Click to Display Table of Contents >> Navigation: Zetadocs SDK Guide > Creating a Per Tenant Extension > Collection Management |
Adding the original invoices to reminders Example
The following example illustrates how to use the OnBeforeDelivery event to attach an extra file to the customer, in this instance, depending on the reminder level the stationary is chosen to be applied to the documents. A list of Sales Invoices associated to the reminder is generated and finally a search executed to get the documents and attach them to the email.
The OnAfterGenerateSendResults event is used to set the document template to use for the Reminders, in the example for Issued Reminders at levels 2 and 3 a different document template is used (with harsher wording), these templates (ZT00019 and ZT00020) have been uploaded in Business Central using the Zetadocs Templates page.
[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;
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Zetadocs Customize", 'OnBeforeDelivery', '', true, true)]
procedure OnBeforeDelivery(var onBeforeDeliveryHandler: Codeunit "Zetadocs OnBeforeDelivery")
var
RecRef: RecordRef;
recordId: RecordId;
recordIds: List of [RecordId];
reminderLevel: Integer;
reminderStationery: Text;
searchResult: Text;
issuedReminderList: List of [Text];
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
297:
begin
//Search for the related Documents for the reminder(s)
isSuccess := TryGetIssuedReminderDocuments(onBeforeDeliveryHandler, recordIds, issuedReminderList);
//Decide which stationery to apply
if (RecRef.Get(recordId)) then begin
reminderLevel := RecRef.Field(28).Value;
case (reminderLevel) of
0:
reminderStationery := 'ZD-Copy Stationery.pdf';
1:
reminderStationery := 'ZD-Overdue Stationery.pdf';
2, 3:
reminderStationery := 'ZD-Final Demand Stationery.pdf';
end;
//Add the related invoices to the Additional Attachments
foreach searchResult in issuedReminderList do begin
onBeforeDeliveryHandler.AddOrUpdateAdditionalAttachments(searchResult, 'ZD-Copy Stationery.pdf');
end;
end;
end;
end;
end;
//Propagate any error to the caller
onBeforeDeliveryHandler.PropagateLastError();
end;
local procedure TryGetIssuedReminderDocuments(onBeforeDeliveryHandler: Codeunit "Zetadocs OnBeforeDelivery"; recordIds: List of [RecordId]; var searchResults: List of [Text]) isSuccess: Boolean
var
CustomerLedgerEntry: Record "Cust. Ledger Entry";
IssuedReminderLine: Record "Issued Reminder Line";
SalesInvoiceHeader: Record "Sales Invoice Header";
recordId: RecordID;
RecRef: RecordRef;
TmpRecRef: RecordRef;
InvoiceRecordIds: List of [RecordId];
tempSearchResults: List of [Dictionary of [Text, Text]];
ResultDictionary: Dictionary of [text, text];
begin
//Build up a list of recordlinks to search with
foreach recordId in recordIds do begin
//Set filters to only get Invoices for the Issued Reminder
if (RecRef.Get(recordId)) then begin
IssuedReminderLine.SetFilter(IssuedReminderLine."Reminder No.", Format(RecRef.Field(1)));
IssuedReminderLine.SetFilter(IssuedReminderLine."Document Type", 'Invoice');
IssuedReminderLine.SetFilter(IssuedReminderLine.Type, 'Customer Ledger Entry');
end;
if (IssuedReminderLine.Find('-')) then begin
repeat
//Apply filters to get the Customer Ledger Entries from the related Issued Reminders Lines
CustomerLedgerEntry.SetFilter(CustomerLedgerEntry."Entry No.", Format(IssuedReminderLine."Entry No."));
CustomerLedgerEntry.SetFilter(CustomerLedgerEntry."Source Code", 'SALES');
if (CustomerLedgerEntry.Find('-')) then begin
repeat
//Apply filters to get the Sales Invoice header from the related Customer Ledger Entry
SalesInvoiceHeader.SetFilter(SalesInvoiceHeader."No.", Format(CustomerLedgerEntry."Document No."));
if (SalesInvoiceHeader.Find('-')) then begin
repeat
//Get the Record Reference to obtain the RecordID
TmpRecRef.GetTable(SalesInvoiceHeader);
InvoiceRecordIds.Add(TmpRecRef.RecordId);
//end of SalesInvoiceHeader loop
until SalesInvoiceHeader.Next() = 0;
end;
//end of CustomerLedgerEntry loop
until CustomerLedgerEntry.Next() = 0
end;
//end of IssuedReminderLine loop
until IssuedReminderLine.Next() = 0
end;
SalesInvoiceHeader.Reset();
CustomerLedgerEntry.Reset();
IssuedReminderLine.Reset();
end;
//Search for related documents
isSuccess := onBeforeDeliveryHandler.TrySearch(InvoiceRecordIds, tempSearchResults);
if (isSuccess) then begin
foreach ResultDictionary in tempSearchResults do begin
//Set the custom filters here, for example if only documents with the word invoice in the name are wanted, the code is as follow:
//Other available values in the dictionary are Modified, FileName, DocumentReference, DocumentType and DocumentLink.
if (ResultDictionary.Get('FileName').Contains('Invoice')) then searchResults.Add(ResultDictionary.Get('Url'));
end;
end;
end;
Adding all Related Documents Example
The code snippet below is a simpler search to just attach any document in the factbox to the email with its own stationery:
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Zetadocs Customize", 'OnBeforeDelivery', '', true, true)]
procedure OnBeforeDelivery(var onBeforeDeliveryHandler: Codeunit "Zetadocs OnBeforeDelivery")
var
recordId: RecordId;
recordIds: List of [RecordId];
isSuccess: Boolean;
tempSearchResults: List of [Dictionary of [Text, Text]];
resultDictionary: Dictionary of [text, text];
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);
//Search for related documents
isSuccess := onBeforeDeliveryHandler.TrySearch(recordIds, tempSearchResults);
if (isSuccess) then begin
foreach resultDictionary in tempSearchResults do begin
onBeforeDeliveryHandler.AddOrUpdateAdditionalAttachments(resultDictionary.Get('Url'), 'ZD-Stationery.pdf', resultDictionary.Get('DocumentLink'));
end;
end;
end;
//Propagate any error to the caller
onBeforeDeliveryHandler.PropagateLastError();
end;