<< Click to Display Table of Contents >> Navigation: Zetadocs SDK Guide > Creating a Per Tenant Extension > Sending Email on Failure/Success |
The Per Tenant Example below demonstrates how you can use the OnAfterZetadocsDelivery methods provided to construct and send an email with the details of documents that have failed the Delivery/Archiving process.
Make sure that the GetDeliveryResults method is called, then iterated through in a for each loop, you will then have access to information on each document in the Zetadocs Delivery Batch created by the customer.
Note: The example below uses the Business Central SMTP Email settings, these can be setup by searching for "SMTP Mail Settings" in the Tell Me What You Want search, by default these will not be configured.
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Zetadocs Customize", 'OnAfterZetadocsDelivery', '', true, true)]
local procedure OnAfterZetadocsDelivery(var OnAfterZetadocsDeliveryHandler: Codeunit "Zdd OnAfterZetadocsDelivery")
var
user: Record User;
TheRecordId: RecordId;
DeliveryResultToken: JsonToken;
DeliveryResultObject: JsonObject;
DeliveryJsonArray: JsonArray;
IsDeliverySuccess: Boolean;
IsArchiveSuccess: Boolean;
InError: Boolean;
ErrorDescription: Text;
RecordIdText: Text;
EmailTo: Text;
SendingUser: Text;
FailureEmailBody: Label 'Failed to send %2. To %1. <b>Error</b>: %3. Delivery Successful: %4. Archiving Successful: %5. <br><br>', Comment = '%2 is the document, %1 is to whom it has been sent, %3 is the error returned, %4 is the delivery status, %5 is the archiving status';
SuccessEmailBody: Label 'Successfully sent %2. To %1. <br><br>', Comment = '%2 is the document, %1 is to whom it has been sent, %3 is the delivery status.';
EmailBody: Text;
begin
if (not OnAfterZetadocsDeliveryHandler.IsZetadocsDeliverySuccessful()) then begin
// Format the email body how you wish.
EmailBody := StrSubstNo('Delivery Report for %1, delivery was attempted on %2.<br>',
OnAfterZetadocsDeliveryHandler.GetReportName(),
OnAfterZetadocsDeliveryHandler.GetReportDateTime());
EmailBody := StrSubstNo('%1<br><br>%2. Details below: <br><br>', EmailBody, OnAfterZetadocsDeliveryHandler.GetDeliveryErrorMessage());
//Get Delivery Json Array
OnAfterZetadocsDeliveryHandler.GetDeliveryResults(DeliveryJsonArray);
foreach DeliveryResultToken in DeliveryJsonArray do begin
//Convert Token to Object, use this in the methods below
DeliveryResultObject := DeliveryResultToken.AsObject();
//Get Email To
EmailTo := OnAfterZetadocsDeliveryHandler.GetEmailTo(DeliveryResultObject);
//Get Record Id
OnAfterZetadocsDeliveryHandler.GetRecordId(DeliveryResultObject, TheRecordID);
//Get if Delivery was successful
IsDeliverySuccess := OnAfterZetadocsDeliveryHandler.IsDeliverySuccessful(DeliveryResultObject);
//Get Error Description
ErrorDescription := OnAfterZetadocsDeliveryHandler.GetDeliveryErrorDescription(DeliveryResultObject);
//Get if Delivery Process is in Error
InError := OnAfterZetadocsDeliveryHandler.IsDeliveryInError(DeliveryResultObject);
//Get if the Archiving was successful
IsArchiveSuccess := OnAfterZetadocsDeliveryHandler.IsArchiveSuccessful(DeliveryResultObject);
if (InError) then begin
//Populate the email body with some extra information like the document type/number and email recipients.
EmailBody := EmailBody + StrSubstNo(DelChr(FailureEmailBody, '>', '.'),
EmailTo,
TheRecordID,
ErrorDescription,
IsDeliverySuccess,
IsArchiveSuccess);
end else begin
EmailBody := EmailBody + StrSubstNo(SuccessEmailBody,
EmailTo,
RecordIdText);
end;
end;
//If you want to update tables uncomment the code below
// case TheRecordID.TableNo() of
// //Sales Orders
// 36:
// begin
// //Update some values in the SalesOrder table
// salesOrder.Get(TheRecordId);
// //Makes changes to fields here...
// //End of changes
// salesOrder.Modify;
// Commit;
// end;
// end;
//Send the email to the user who ran the report.
SendingUser := OnAfterZetadocsDeliveryHandler.GetSendingUser();
if (user.Get(UserSecurityId())) then begin
if (not (user."Contact Email" = '')) then SendMail(user."Contact Email", EmailBody, 'Zetadocs Delivery Error Report');
end;
end;
end;
local procedure SendErrorMail(emailAddress: Text; emailBody: Text)
var
EmailItem: Record "Email Item" temporary;
begin
EmailItem."Send to" := emailAddress;
EmailItem.Subject := 'Zetadocs Delivery Error Report';
EmailItem.SetBodyText(emailBody);
EmailItem.Send(true);
end;
The Per Tenant Example below demonstrates how you can use the OnAfterCapture methods provided to construct and send an email with the details of documents that have failed the Capture/Archiving process.
Note: The example below uses the Business Central SMTP Email settings, these can be setup by searching for "SMTP Mail Settings" in the Tell Me What You Want search, by default these will not be configured.
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Zetadocs Customize", 'OnAfterCapture', '', true, true)]
local procedure OnAfterCapture(var onAfterCaptureHandler: Codeunit "Zdd OnAfterCapture")
var
user: Record User;
documents: List of [Text];
documentName: Text;
isDocumentProcessedSuccessfully: Boolean;
errorMessage: Text;
FailureEmailBody: Label 'Failed to capture document %1. <b>Error</b>: %2. <br><br>', Comment = '%1 is the document name, %2 is the error message related to the failure to process the document.';
EmailBody: Text;
begin
onAfterCaptureHandler.GetDocumentIds(documents);
foreach documentName in documents do begin
onAfterCaptureHandler.GetSuccessStatus(documentName, isDocumentProcessedSuccessfully, errorMessage);
if (not isDocumentProcessedSuccessfully) then begin
EmailBody := StrSubstNo(FailureEmailBody, documentName, errorMessage);
end;
end;
if (EmailBody <> '') then
if (user.Get(UserSecurityId())) then begin
if (not (user."Contact Email" = '')) then SendMail(user."Contact Email", EmailBody, 'Capture Report');
end;
end;
For Business Central 17.1 and onwards, the email sending functionality should be upgraded to the following code, assuming the Microsoft email setup is being run properly.
local procedure SendMail(emailAddress: Text; emailBody: Text; emailSubject: Text)
var
EmailItem: Record "Email Item" temporary;
begin
EmailItem."Send to" := emailAddress;
EmailItem.Subject := emailSubject;
EmailItem.SetBodyText(emailBody);
EmailItem.Send(true);
end;