英文:
How Do I Get a Customer's Custom Report Layout ID from their Document Layouts in Business Central?
问题
我正在编写一个Business Central扩展,它在销售订单列表页面上添加了一个按钮,用于保存销售订单确认的PDF副本,文件名格式为"Sales Order [订单号].pdf"。
一些客户可能在他们的“客户卡>客户>文档布局”中定义了自定义报告布局,这应该导致使用不同的布局,而不是在报告布局选择中指定的那个。
然而,我找到的保存报告的方法需要一个报告ID(Report.SaveAs,Report.Execute),我不确定如何从客户文档布局中检索自定义报告布局的ID。据我所知,没有文档布局的表格。
以下是我目前拥有的代码,除了自定义报告布局的问题外,它可以按预期工作。
pageextension 50100 SalesOrderExt extends "销售订单列表"
{
actions
{
addafter("打印确认")
{
action("保存确认为PDF")
{
Caption = '保存确认为PDF';
ApplicationArea = All;
Image = ExportFile;
Promoted = true;
PromotedCategory = Category8;
PromotedIsBig = true;
ToolTip = '将订单确认保存为PDF';
trigger OnAction()
var
SalesHeader: Record "销售标头";
TempBlob: Codeunit "临时 Blob";
FileManagement: Codeunit "文件管理";
RecRef: RecordRef;
OStream: OutStream;
XmlParameters: Text;
begin
XmlParameters := '<?xml version="1.0" standalone="yes"?>' +
'<ReportParameters name="销售订单" id="' + System.Format(Report::"销售订单") + '">' +
'<Options>' +
'<Field name="NoCopies">0</Field>' +
'<Field name="PrintCompany">false</Field>' +
'<Field name="ArchiveDocument">false</Field>' +
'<Field name="LogInteraction">false</Field>' +
'<Field name="DisplayAssemblyInformation">false</Field>' +
'</Options>' +
'<DataItems>' +
'<DataItem name="销售标头">VERSION(1) SORTING(Field1,Field3) WHERE(Field3=1(' + Rec."No." + '))</DataItem>' +
'<DataItem name="销售行">VERSION(1) SORTING(Field1,Field3,Field4)</DataItem>' +
'<DataItem name="销售行注释">VERSION(1) SORTING(Field1,Field2,Field7,Field3)</DataItem>' +
'<DataItem name="销售注释行">VERSION(1) SORTING(Field1,Field2,Field7,Field3)</DataItem>' +
'<DataItem name="CopyLoop">VERSION(1) SORTING(Field1)</DataItem>' +
'<DataItem name="PageLoop">VERSION(1) SORTING(Field1)</DataItem>' +
'<DataItem name="销售行">VERSION(1) SORTING(Field1)</DataItem>' +
'<DataItem name="AsmLoop">VERSION(1) SORTING(Field1)</DataItem>' +
'</DataItems>' +
'</ReportParameters>';
SalesHeader.Reset();
SalesHeader.SetRange("文档类型", Rec."文档类型");
SalesHeader.SetRange("编号", Rec."编号");
SalesHeader.FindFirst();
Clear(OStream);
CurrPage.SetSelectionFilter(SalesHeader);
RecRef.GetTable(SalesHeader);
TempBlob.CreateOutStream(OStream, TextEncoding::UTF8);
Report.SaveAs(Report::"销售订单", XmlParameters, ReportFormat::Pdf, OStream, RecRef);
FileManagement.BLOBExport(TempBlob, '销售订单 ' + Rec."编号" + '.pdf', true);
end;
}
}
}
}
英文:
I am writing a Business Central extension that adds a button to the Sales Order List page, which saves a PDF copy of a sales order confirmation with the file name format "Sales Order [Order No.].pdf".
Some customers may have custom report layouts defined in their Customer Card > Customer > Document Layouts, which should result in a different layout being used than the one specified in Report Layout Selection.
However, the methods I have found to save reports require a report ID (Report.SaveAs, Report.Execute), and I am not sure how to retrieve the custom report layout IDs from the customer document layouts. There isn't a table for document layouts as far as I know.
Below is the code I have so far, which works as intended barring the custom report layout issue.
pageextension 50100 SalesOrderExt extends "Sales Order List"
{
actions
{
addafter("Print Confirmation")
{
action("Save Confirmation as PDF")
{
Caption = 'Save Confirmation As PDF';
ApplicationArea = All;
Image = ExportFile;
Promoted = true;
PromotedCategory = Category8;
PromotedIsBig = true;
ToolTip = 'Save the order confirmation as a PDF';
trigger OnAction()
var
SalesHeader: Record "Sales Header";
TempBlob: Codeunit "Temp Blob";
FileManagement: Codeunit "File Management";
RecRef: RecordRef;
OStream: OutStream;
XmlParameters: Text;
begin
XmlParameters := '<?xml version="1.0" standalone="yes"?>' +
'<ReportParameters name="Sales Order" id="' + System.Format(Report::"Sales Order") + '">' +
'<Options>' +
'<Field name="NoCopies">0</Field>' +
'<Field name="PrintCompany">false</Field>' +
'<Field name="ArchiveDocument">false</Field>' +
'<Field name="LogInteraction">false</Field>' +
'<Field name="DisplayAssemblyInformation">false</Field>' +
'</Options>' +
'<DataItems>' +
'<DataItem name="Sales Header">VERSION(1) SORTING(Field1,Field3) WHERE(Field3=1(' + Rec."No." + '))</DataItem>' +
'<DataItem name="Sales Line">VERSION(1) SORTING(Field1,Field3,Field4)</DataItem>' +
'<DataItem name="SalesLineComments">VERSION(1) SORTING(Field1,Field2,Field7,Field3)</DataItem>' +
'<DataItem name="Sales Comment Line">VERSION(1) SORTING(Field1,Field2,Field7,Field3)</DataItem>' +
'<DataItem name="CopyLoop">VERSION(1) SORTING(Field1)</DataItem>' +
'<DataItem name="PageLoop">VERSION(1) SORTING(Field1)</DataItem>' +
'<DataItem name="SalesLine">VERSION(1) SORTING(Field1)</DataItem>' +
'<DataItem name="AsmLoop">VERSION(1) SORTING(Field1)</DataItem>' +
'</DataItems>' +
'</ReportParameters>';
SalesHeader.Reset();
SalesHeader.SetRange("Document Type", Rec."Document Type");
SalesHeader.SetRange("No.", Rec."No.");
SalesHeader.FindFirst();
Clear(OStream);
CurrPage.SetSelectionFilter(SalesHeader);
RecRef.GetTable(SalesHeader);
TempBlob.CreateOutStream(OStream, TextEncoding::UTF8);
Report.SaveAs(Report::"Sales Order", XmlParameters, ReportFormat::Pdf, OStream, RecRef);
FileManagement.BLOBExport(TempBlob, 'Sales Order ' + Rec."No." + '.pdf', true);
end;
}
}
}
}
答案1
得分: 0
以下是已翻译的代码部分:
原来引用位于自定义报告选择表中。在 Business Central 的用户界面中隐藏了“Source No.”字段。
以下是有效的代码:
pageextension 50100 SalesOrderExt extends "Sales Order List"
{
actions
{
addafter("Print Confirmation")
{
action("Save as PDF")
{
Caption = '保存为 PDF';
ApplicationArea = All;
Image = ExportFile;
Promoted = true;
PromotedCategory = Category8;
PromotedIsBig = true;
ToolTip = '将订单保存为 PDF';
trigger OnAction()
var
SalesHeader: Record "Sales Header";
CustomReportSelection: Record "Custom Report Selection";
ReportLayoutSelection: Record "Report Layout Selection";
TempBlob: Codeunit "Temp Blob";
FileManagement: Codeunit "File Management";
RecRef: RecordRef;
OStream: OutStream;
ReportType: Integer;
XmlParameters: Text;
begin
ReportType := Report::"Sales Order";
CustomReportSelection.Reset();
CustomReportSelection.SetRange("Report ID", ReportType);
CustomReportSelection.SetRange("Source No.", Rec."Bill-to Customer No.");
if not CustomReportSelection.IsEmpty then begin
CustomReportSelection.FindFirst();
ReportLayoutSelection.SetTempLayoutSelected(System.Format(CustomReportSelection."Custom Report Layout Code"))
end;
SalesHeader.Reset();
SalesHeader.SetRange("Document Type", Rec."Document Type");
SalesHeader.SetRange("No.", Rec."No.");
XmlParameters := '<?xml version="1.0" standalone="yes"?>' +
'<ReportParameters name="Sales Order" id="' + System.Format(ReportType) + '">' +
'<Options>' +
'<Field name="NoCopies">0</Field>' +
'<Field name="PrintCompany">false</Field>' +
'<Field name="ArchiveDocument">false</Field>' +
'<Field name="LogInteraction">false</Field>' +
'<Field name="DisplayAssemblyInformation">false</Field>' +
'</Options>' +
'<DataItems>' +
'<DataItem name="Sales Header">VERSION(1) SORTING(Field1,Field3) WHERE(Field3=1(' + Rec."No." + '),Field4=1(' + Rec."Bill-to Customer No." + '))</DataItem>' +
'<DataItem name="Sales Line">VERSION(1) SORTING(Field1,Field3,Field4)</DataItem>' +
'<DataItem name="SalesLineComments">VERSION(1) SORTING(Field1,Field2,Field7,Field3)</DataItem>' +
'<DataItem name="Sales Comment Line">VERSION(1) SORTING(Field1,Field2,Field7,Field3)</DataItem>' +
'<DataItem name="CopyLoop">VERSION(1) SORTING(Field1)</DataItem>' +
'<DataItem name="PageLoop">VERSION(1) SORTING(Field1)</DataItem>' +
'<DataItem name="SalesLine">VERSION(1) SORTING(Field1)</DataItem>' +
'<DataItem name="AsmLoop">VERSION(1) SORTING(Field1)</DataItem>' +
'</DataItems>' +
'</ReportParameters>';
Clear(OStream);
CurrPage.SetSelectionFilter(SalesHeader);
RecRef.GetTable(SalesHeader);
TempBlob.CreateOutStream(OStream, TextEncoding::UTF8);
Report.SaveAs(ReportType, XmlParameters, ReportFormat::Pdf, OStream, RecRef);
FileManagement.BLOBExport(TempBlob, CopyStr(CurrPage.ObjectId(true), 6) + ' ' + Rec."No." + '.pdf', true);
ReportLayoutSelection.SetTempLayoutSelected('');
end;
}
}
}
}
英文:
It turns out the references are located in the Custom Report Selection table. The "Source No." field that is hidden from the UI in Business Central.
Here is the working code below:
pageextension 50100 SalesOrderExt extends "Sales Order List"
{
actions
{
addafter("Print Confirmation")
{
action("Save as PDF")
{
Caption = 'Save As PDF';
ApplicationArea = All;
Image = ExportFile;
Promoted = true;
PromotedCategory = Category8;
PromotedIsBig = true;
ToolTip = 'Save the order as a PDF';
trigger OnAction()
var
SalesHeader: Record "Sales Header";
CustomReportSelection: Record "Custom Report Selection";
ReportLayoutSelection: Record "Report Layout Selection";
TempBlob: Codeunit "Temp Blob";
FileManagement: Codeunit "File Management";
RecRef: RecordRef;
OStream: OutStream;
ReportType: Integer;
XmlParameters: Text;
begin
ReportType := Report::"Sales Order";
CustomReportSelection.Reset();
CustomReportSelection.SetRange("Report ID", ReportType);
CustomReportSelection.SetRange("Source No.", Rec."Bill-to Customer No.");
if not CustomReportSelection.IsEmpty then begin
CustomReportSelection.FindFirst();
ReportLayoutSelection.SetTempLayoutSelected(System.Format(CustomReportSelection."Custom Report Layout Code"))
end;
SalesHeader.Reset();
SalesHeader.SetRange("Document Type", Rec."Document Type");
SalesHeader.SetRange("No.", Rec."No.");
XmlParameters := '<?xml version="1.0" standalone="yes"?>' +
'<ReportParameters name="Sales Order" id="' + System.Format(ReportType) + '">' +
'<Options>' +
'<Field name="NoCopies">0</Field>' +
'<Field name="PrintCompany">false</Field>' +
'<Field name="ArchiveDocument">false</Field>' +
'<Field name="LogInteraction">false</Field>' +
'<Field name="DisplayAssemblyInformation">false</Field>' +
'</Options>' +
'<DataItems>' +
'<DataItem name="Sales Header">VERSION(1) SORTING(Field1,Field3) WHERE(Field3=1(' + Rec."No." + '),Field4=1(' + Rec."Bill-to Customer No." + '))</DataItem>' +
'<DataItem name="Sales Line">VERSION(1) SORTING(Field1,Field3,Field4)</DataItem>' +
'<DataItem name="SalesLineComments">VERSION(1) SORTING(Field1,Field2,Field7,Field3)</DataItem>' +
'<DataItem name="Sales Comment Line">VERSION(1) SORTING(Field1,Field2,Field7,Field3)</DataItem>' +
'<DataItem name="CopyLoop">VERSION(1) SORTING(Field1)</DataItem>' +
'<DataItem name="PageLoop">VERSION(1) SORTING(Field1)</DataItem>' +
'<DataItem name="SalesLine">VERSION(1) SORTING(Field1)</DataItem>' +
'<DataItem name="AsmLoop">VERSION(1) SORTING(Field1)</DataItem>' +
'</DataItems>' +
'</ReportParameters>';
Clear(OStream);
CurrPage.SetSelectionFilter(SalesHeader);
RecRef.GetTable(SalesHeader);
TempBlob.CreateOutStream(OStream, TextEncoding::UTF8);
Report.SaveAs(ReportType, XmlParameters, ReportFormat::Pdf, OStream, RecRef);
FileManagement.BLOBExport(TempBlob, CopyStr(CurrPage.ObjectId(true), 6) + ' ' + Rec."No." + '.pdf', true);
ReportLayoutSelection.SetTempLayoutSelected('');
end;
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论