Aller au contenu

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.

AL
'GETFUNCTIONS':
    Rec."Function List" := 'PrintOrderConfirmation,PrintOrderConfirmationFromTable';

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

  1. Ouvrez les actions de la table concernée.
  2. Créez une action et définissez Type sur Report over Codeunit.
  3. Définissez ID sur 5138176 ou sur la copie personnalisée.
  4. Sélectionnez PrintOrderConfirmation ou PrintOrderConfirmationFromTable dans Param.
  5. Sélectionnez le rapport dans Report Caption ou définissez Report ID.
  6. Utilisez éventuellement Set Report Settings.
  7. Configurez le nom du fichier et les espaces réservés.
  8. Pour PrintOrderConfirmation, configurez SALESORDER avec une RecordId valide.

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 à GETFUNCTIONS et case.
  • Business Portals transmet le contexte de l'action via Rec.
  • Validez les entrées, Action Code, Report ID et l'enregistrement.
  • L'exemple répète trois fois le même appel Report.SaveAs et quitte sans erreur dédiée. Remplacez ceci par une gestion des erreurs et une journalisation adaptées.
  • Comme File Blob est 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

AL
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;
}