Ejemplo de Report over Codeunit
La codeunit 5138176 "SIM_DPS Sample Report over CU" muestra cómo el tipo de acción Report over Codeunit puede seleccionar un registro mediante lógica personalizada y devolver un informe de Business Central como PDF.
Utiliza SIM_DPS OL, resuelve un registro, aplica la configuración de la página de solicitud, renderiza el informe en SIM_DPS OL."File Blob" y permite que Business Portals lo devuelva al frontend.
Cuándo utilizar Report over Codeunit
Una acción Report normal ejecuta un informe para el registro del conjunto de datos. Utilice Report over Codeunit cuando la lógica AL deba decidir qué registro se pasa al informe.
| Función | Origen del registro |
|---|---|
PrintOrderConfirmation |
Lee una ID de registro del campo SALESORDER. |
PrintOrderConfirmationFromTable |
Utiliza SIM_DPS OL."Dataset Table Record". |
Descubrimiento y distribución
Sin código de acción, OnRun utiliza GETFUNCTIONS y devuelve ambos nombres.
Durante la ejecución, OnRun carga la acción y su Param selecciona el procedimiento.
Resolver el registro
Desde un campo de entrada
PrintOrderConfirmation lee SALESORDER y lo evalúa como RecordId. Debe ser una RecordId formateada, no solo un número como SO-1001. Un valor ausente o no válido provoca un error en Evaluate.
Desde la tabla del conjunto de datos
PrintOrderConfirmationFromTable pasa Dataset Table Record directamente a PrintReport.
Crear el PDF
PrintReport abre la ID como RecordRef, la filtra por clave mediante SIM_DPS Order Function Library.FilterRecRefByKey, carga SIM_DPS Action, lee Report Settings, limpia File Blob y llama a Report.SaveAs con Report ID y formato PDF.
Después, Business Portals convierte File Blob a Base64 y lo devuelve como descarga. La acción controla el nombre y sus marcadores.
Configuración
- Abra las acciones de la tabla correspondiente.
- Cree una acción y establezca Type en Report over Codeunit.
- Establezca ID en
5138176o en la copia personalizada. - Seleccione
PrintOrderConfirmationoPrintOrderConfirmationFromTableen Param. - Seleccione el informe en Report Caption o establezca Report ID.
- Utilice opcionalmente Set Report Settings.
- Configure el nombre del archivo y los marcadores.
- Para
PrintOrderConfirmation, configureSALESORDERcon unaRecordIdválida.
El informe debe admitir la tabla resuelta y la ejecución desatendida mediante Report.SaveAs.
Consideraciones de implementación
- Mantenga
TableNo = "SIM_DPS OL"y añada funciones aGETFUNCTIONSycase. - Business Portals transmite el contexto de acción mediante
Rec. - Valide las entradas,
Action Code,Report IDy el registro. - El ejemplo reintenta la misma llamada
Report.SaveAstres veces y sale sin error específico. Sustitúyalo por control de errores y registro. - Al limpiar
File Blobantes de renderizar, un fallo no conserva el archivo anterior. - Pruebe los ajustes guardados después de cambios de informe o versión.
- Compruebe permisos y evite lógica interactiva.
Código de ejemplo completo
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;
}