Example of Business Events and Business Central
Last week I have blogged about how you can create Business Events inside Business Central and react on that it in Power Automate:
Private Preview: Business Events the newest integration with Business Central – Discover Microsoft Business Central (bertverbeek.nl)
Mostly it works better by an example. In this case I have created an example of printing the “Purchase -Receipt” after the goods are received. This will generate a base64 string and sent is over to Power Automate to send it by mail. Or you can store it in a file storage or what you want.
The code in BC looks like below:
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Purch.-Post", 'OnAfterFinalizePosting', '', true, true)]
local procedure OnAfterFinalizePosting(
var PurchHeader: Record "Purchase Header"; var PurchRcptHeader: Record "Purch. Rcpt. Header";
var PurchInvHeader: Record "Purch. Inv. Header"; var PurchCrMemoHdr: Record "Purch. Cr. Memo Hdr.";
var ReturnShptHeader: Record "Return Shipment Header"; var GenJnlPostLine: Codeunit "Gen. Jnl.-Post Line";
PreviewMode: Boolean; CommitIsSupressed: Boolean)
var
Url: Text[250];
PurchaseReceiptsApiUrlTok: Label 'v2.0/companies(%1)/purchaseReceipts(%2)', Locked = true;
Outstream: OutStream;
Instream: InStream;
RecRef: RecordRef;
Variant: Variant;
Base64: Codeunit "Base64 Convert";
TempBlob: Codeunit "Temp Blob";
Base64Text: Text;
begin
if PurchRcptHeader."No." <> '' then begin
Url := GetBaseUrl() + StrSubstNo(PurchaseReceiptsApiUrlTok, GetCompanyId(), TrimId(PurchRcptHeader.SystemId));
//Print purchase receipt
PurchRcptHeader.SetRecFilter();
Recref.GetTable(PurchRcptHeader);
Outstream := TempBlob.CreateOutStream();
if Report.SaveAs(Report::"Purchase - Receipt", '', ReportFormat::Pdf, Outstream, RecRef) then begin
Instream := TempBlob.CreateInStream();
Base64Text := Base64.ToBase64(Instream);
MyBusinessEventPurchaseReceivedPosted(PurchRcptHeader.SystemId, Url, Base64Text);
end;
end;
end;
[ExternalBusinessEvent('PurchaseReceiptPosted', 'Purchase receipt posted', 'This business event is triggered when goods from a purchase order are received by the internal warehouse/external logistics company.', EventCategory::"My Purchase Events")]
local procedure MyBusinessEventPurchaseReceivedPosted(PurchaseInvoiceId: Guid; Url: text[250]; Base64Text: text)
begin
end;
local procedure GetBaseUrl(): text
begin
exit(GetUrl(CLIENTTYPE::Api));
end;
local procedure GetCompanyId(): text
var
Company: Record Company;
begin
Company.Get(CompanyName);
exit(TrimId(Company.SystemId));
end;
local procedure TrimId(Id: Guid): Text
begin
exit(DelChr(Format(Id), '<>', '{}'));
end;
Then In Power Automate you can create the following flow:
Once you have done that you get an e-mail with your document:
Update 04-05-2023
Sandy Winarko pointed me that you also can use Virtual tables instead of the standard BC connector. And he is right. So here is the example of the Dataverse workflow:
1 COMMENT