Voorbeeld van Report over Codeunit
Codeunit 5138176 "SIM_DPS Sample Report over CU" laat zien hoe actietype Report over Codeunit via aangepaste logica een record selecteert en een Business Central-rapport als PDF retourneert.
De codeunit gebruikt SIM_DPS OL, bepaalt een record, past opgeslagen aanvraagpagina-instellingen toe, rendert naar SIM_DPS OL."File Blob" en laat Business Portals het bestand aan de frontend retourneren.
Wanneer Report over Codeunit gebruiken
Een normale Report-actie voert een rapport uit voor het datasetrecord. Gebruik Report over Codeunit wanneer AL-logica moet bepalen welk record aan het rapport wordt doorgegeven.
| Functie | Recordbron |
|---|---|
PrintOrderConfirmation |
Leest een record-ID uit invoerveld SALESORDER. |
PrintOrderConfirmationFromTable |
Gebruikt SIM_DPS OL."Dataset Table Record". |
Functiedetectie en dispatch
Zonder actiecode gebruikt OnRun GETFUNCTIONS en retourneert beide functies.
Tijdens uitvoering laadt OnRun de actie en selecteert Param de procedure.
Het rapportrecord bepalen
Vanuit een invoerveld
PrintOrderConfirmation leest SALESORDER en evalueert dit als RecordId. De waarde moet een geformatteerde RecordId zijn, niet alleen een ordernummer zoals SO-1001. Een ontbrekende of ongeldige waarde veroorzaakt een fout in Evaluate.
Vanuit de datasettabel
PrintOrderConfirmationFromTable geeft Dataset Table Record rechtstreeks aan PrintReport door.
De PDF maken
PrintReport opent de ID als RecordRef, filtert deze op sleutel via SIM_DPS Order Function Library.FilterRecRefByKey, laadt SIM_DPS Action, leest Report Settings, wist File Blob en roept Report.SaveAs aan met Report ID en PDF-formaat.
Business Portals zet daarna File Blob om naar Base64 en retourneert de download. De actie beheert bestandsnaam en tijdelijke aanduidingen.
Configuratie
- Open de acties voor de vereiste datasettabel.
- Maak een actie en stel Type in op Report over Codeunit.
- Stel ID in op
5138176of op de aangepaste kopie. - Selecteer
PrintOrderConfirmationofPrintOrderConfirmationFromTablein Param. - Selecteer het rapport in Report Caption of stel Report ID in.
- Gebruik optioneel Set Report Settings.
- Configureer bestandsnaam en tijdelijke aanduidingen.
- Configureer voor
PrintOrderConfirmationeen geldigeRecordIdinSALESORDER.
Het rapport moet de bepaalde tabel ondersteunen en geschikt zijn voor onbeheerde Report.SaveAs-uitvoering.
Implementatieoverwegingen
- Behoud
TableNo = "SIM_DPS OL"en voeg functies toe aanGETFUNCTIONSencase. - Business Portals geeft de actiecontext door via
Rec. - Valideer invoer,
Action Code,Report IDen het record. - Het voorbeeld probeert dezelfde
Report.SaveAs-aanroep driemaal en stopt zonder specifieke fout. Vervang dit door passende foutafhandeling en logging. - Omdat
File Blobvóór rendering wordt gewist, blijft na een fout geen vorig bestand over. - Test opgeslagen instellingen na rapport- of versiewijzigingen.
- Controleer machtigingen en vermijd interactieve rapportlogica.
Volledige voorbeeldcode
codeunit 5138176 "SIM_DPS Sample Report over CU"
{
TableNo = "SIM_DPS OL";
trigger OnRun()
var
LocalRecordSIMDPSDTAction: Record "SIM_DPS DT Action";
LocalRecordSIMDPSAction: Record "SIM_DPS Action";
LocalParamText: Text;
begin
LocalParamText := 'GETFUNCTIONS';
if Rec."Action Code" <> '' then begin
if not LocalRecordSIMDPSDTAction.Get(Rec."Dataset Code", Rec."Dataset Table Code", Rec."Dataset Table Type", Rec."Action Code") then exit;
if not LocalRecordSIMDPSAction.Get(LocalRecordSIMDPSDTAction."Action Code") then exit;
LocalParamText := LocalRecordSIMDPSAction.Param;
end;
case LocalParamText of
'PrintOrderConfirmation':
PrintOrderConfirmation(Rec);
'PrintOrderConfirmationFromTable':
PrintOrderConfirmationFromTable(Rec);
'GETFUNCTIONS':
Rec."Function List" := 'PrintOrderConfirmation,PrintOrderConfirmationFromTable';
end;
end;
var
GlobalCodeunitSIMDPSOrderFunctionLibrary: Codeunit "SIM_DPS Order Function Library";
GlobalRecIdLabelErr: Label 'You have not selected a valid record.';
procedure PrintOrderConfirmation(var ParamRecordSIMDPSOL: Record "SIM_DPS OL")
var
LocalRecordId: RecordId;
begin
Evaluate(LocalRecordId, Format(GlobalCodeunitSIMDPSOrderFunctionLibrary.GetDPSOrderLineInputFieldByCode(ParamRecordSIMDPSOL, 'SALESORDER')));
PrintReport(ParamRecordSIMDPSOL, LocalRecordId);
end;
procedure PrintOrderConfirmationFromTable(var ParamRecordSIMDPSOL: Record "SIM_DPS OL")
var
LocalRecordId: RecordId;
begin
LocalRecordId := ParamRecordSIMDPSOL."Dataset Table Record";
PrintReport(ParamRecordSIMDPSOL, LocalRecordId);
end;
procedure PrintReport(var ParamRecordSIMDPSOL: Record "SIM_DPS OL"; ParamRecordId: RecordId)
var
LocalRecordSIMDPSAction: Record "SIM_DPS Action";
LocalRecordRef: RecordRef;
Local2RecordRef: RecordRef;
LocalVariant: Variant;
LocalReportRequestPageText: Text;
LocalInStream: InStream;
LocalOutStream: OutStream;
begin
if LocalRecordRef.Get(ParamRecordId) then begin
GlobalCodeunitSIMDPSOrderFunctionLibrary.FilterRecRefByKey(LocalRecordRef, Local2RecordRef);
LocalVariant := Local2RecordRef;
if LocalRecordSIMDPSAction.Get(ParamRecordSIMDPSOL."Action Code") then begin
LocalReportRequestPageText := '';
if LocalRecordSIMDPSAction."Report Settings".HasValue() then begin
LocalRecordSIMDPSAction.CalcFields("Report Settings");
LocalRecordSIMDPSAction."Report Settings".CreateInStream(LocalInStream);
LocalInStream.Read(LocalReportRequestPageText);
end;
Clear(LocalOutStream);
Clear(ParamRecordSIMDPSOL."File Blob");
ParamRecordSIMDPSOL."File Blob".CreateOutStream(LocalOutStream);
if not Report.SaveAs(LocalRecordSIMDPSAction."Report ID", LocalReportRequestPageText, ReportFormat::Pdf, LocalOutStream, LocalVariant) then
if not Report.SaveAs(LocalRecordSIMDPSAction."Report ID", LocalReportRequestPageText, ReportFormat::Pdf, LocalOutStream, LocalVariant) then
if not Report.SaveAs(LocalRecordSIMDPSAction."Report ID", LocalReportRequestPageText, ReportFormat::Pdf, LocalOutStream, LocalVariant) then
exit;
end;
end else
Error(GlobalRecIdLabelErr);
end;
}