How to send emails with attachments in Business Cental AL
Many times we find ourselves with the need to send a document from Business Central with attached files, from Sales Orders, Purchases or some other document. Below I will share a solution which works for the aforementioned.
The following example is based on generating a Sales Confirmation and sending an email with two attachments.
The first thing we will do is attach two files from the Sales Order.
Then, from the new action created "Send Confirmation With Attachments", we press to send the email with the two attachments.
When you execute the action, an email will arrive with the sales confirmation and the two attached data.
From the AL code, we can customize the email sending with images or a more appropriate email body for the company.
Below I link the source code.
https://github.com/juancamilo0519/SendAttachWithBusinessCentral
//How to Do in Business Central By Juan Camilo Camargo A.
pageextension 50101 SalesOrderExt extends "Sales Order"
{
actions
{
addlast("O&rder")
{
action("Send Confirmation With Attachments")
{
ApplicationArea = All;
Image = SendAsPDF;
Caption = 'Send Confirmation With Attachments';
trigger OnAction()
var
OutStream: OutStream;
InvoicePdf: InStream;
InvoiceXML: InStream;
Attach: InStream;
DocumentServices: Codeunit "Document Service Management";
RecDocAttached: Record "Document Attachment";
TenantMedia: Record "Tenant Media";
Mail: Text;
Email: Codeunit Email;
TheMessage: Codeunit "Email Message";
base64: Codeunit "Base64 Convert";
begin
IF CONFIRM('Do you want to send the sales confirmation?') THEN BEGIN
TheMessage.Create('juancamilocamargoariza@gmail.com', 'Sales Order Confirmation', 'This is the Body');
RecDocAttached.Reset();
RecDocAttached.SetRange("Table ID", 36);
RecDocAttached.SetRange("No.", rec."No.");
if RecDocAttached.FindSet() then
repeat begin
if TenantMedia.get(RecDocAttached."Document Reference ID".MediaId) then begin
TenantMedia.CalcFields(Content);
if TenantMedia.Content.HasValue then begin
Clear(Attach);
TenantMedia.Content.CreateInStream(Attach);
TheMessage.AddAttachment(RecDocAttached."File Name" + '.' + RecDocAttached."File Extension", RecDocAttached."File Extension", base64.ToBase64(Attach));
end;
end;
end until RecDocAttached.Next() = 0;
Email.Send(TheMessage);
Message('Invoice has been sent');
end;
end;
}
}
}
}
Comentarios
Publicar un comentario