Exemple de Report over Codeunit
La codeunit 5138176 "SIM_DPS Sample Report over CU" montre comment le type d'action Report over Codeunit sélectionne un enregistrement par une logique personnalisée et renvoie un rapport Business Central au format PDF.
Elle utilise SIM_DPS OL, résout un enregistrement, applique les paramètres de la page de demande, génère le rapport dans SIM_DPS OL."File Blob" et permet à Business Portals de le renvoyer au frontend.
Quand utiliser Report over Codeunit
Une action Report normale exécute un rapport pour l'enregistrement du dataset. Utilisez Report over Codeunit lorsque la logique AL doit déterminer quel enregistrement transmettre au rapport.
| Fonction | Source de l'enregistrement |
|---|---|
PrintOrderConfirmation |
Lit un ID d'enregistrement dans le champ SALESORDER. |
PrintOrderConfirmationFromTable |
Utilise SIM_DPS OL."Dataset Table Record". |
Découverte et distribution
Sans code d'action, OnRun utilise GETFUNCTIONS et renvoie les deux fonctions.
Pendant l'exécution, OnRun charge l'action et son Param sélectionne la procédure.
Résoudre l'enregistrement
Depuis un champ de saisie
PrintOrderConfirmation lit SALESORDER et l'évalue comme RecordId. La valeur doit être une RecordId formatée, et non un simple numéro tel que SO-1001. Une valeur absente ou incorrecte provoque une erreur dans Evaluate.
Depuis la table de dataset
PrintOrderConfirmationFromTable transmet Dataset Table Record directement à PrintReport.
Créer le PDF
PrintReport ouvre l'ID comme RecordRef, le filtre par clé avec SIM_DPS Order Function Library.FilterRecRefByKey, charge SIM_DPS Action, lit Report Settings, efface File Blob et appelle Report.SaveAs avec Report ID au format PDF.
Business Portals convertit ensuite File Blob en Base64 et le renvoie en téléchargement. L'action contrôle le nom du fichier et ses espaces réservés.
Configuration
- Ouvrez les actions de la table concernée.
- Créez une action et définissez Type sur Report over Codeunit.
- Définissez ID sur
5138176ou sur la copie personnalisée. - Sélectionnez
PrintOrderConfirmationouPrintOrderConfirmationFromTabledans Param. - Sélectionnez le rapport dans Report Caption ou définissez Report ID.
- Utilisez éventuellement Set Report Settings.
- Configurez le nom du fichier et les espaces réservés.
- Pour
PrintOrderConfirmation, configurezSALESORDERavec uneRecordIdvalide.
Le rapport doit prendre en charge la table résolue et l'exécution sans surveillance via Report.SaveAs.
Considérations d'implémentation
- Conservez
TableNo = "SIM_DPS OL"et ajoutez les fonctions àGETFUNCTIONSetcase. - Business Portals transmet le contexte de l'action via
Rec. - Validez les entrées,
Action Code,Report IDet l'enregistrement. - L'exemple répète trois fois le même appel
Report.SaveAset quitte sans erreur dédiée. Remplacez ceci par une gestion des erreurs et une journalisation adaptées. - Comme
File Blobest effacé avant le rendu, un échec ne conserve aucun fichier précédent. - Testez les paramètres après toute modification de rapport ou de version.
- Vérifiez les autorisations et évitez la logique interactive.
Code d'exemple complet
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;
}