如何从Business Central中的文档布局中获取客户的自定义报告布局ID?

huangapple go评论45阅读模式
英文:

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 &quot;Sales Order List&quot;
{
actions
{
addafter(&quot;Print Confirmation&quot;)
{
action(&quot;Save Confirmation as PDF&quot;)
{
Caption = &#39;Save Confirmation As PDF&#39;;
ApplicationArea = All;
Image = ExportFile;
Promoted = true;
PromotedCategory = Category8;
PromotedIsBig = true;
ToolTip = &#39;Save the order confirmation as a PDF&#39;;
trigger OnAction()
var
SalesHeader: Record &quot;Sales Header&quot;;
TempBlob: Codeunit &quot;Temp Blob&quot;;
FileManagement: Codeunit &quot;File Management&quot;;
RecRef: RecordRef;
OStream: OutStream;
XmlParameters: Text;
begin
XmlParameters := &#39;&lt;?xml version=&quot;1.0&quot; standalone=&quot;yes&quot;?&gt;&#39; +
&#39;&lt;ReportParameters name=&quot;Sales Order&quot; id=&quot;&#39; + System.Format(Report::&quot;Sales Order&quot;) + &#39;&quot;&gt;&#39; +
&#39;&lt;Options&gt;&#39; +
&#39;&lt;Field name=&quot;NoCopies&quot;&gt;0&lt;/Field&gt;&#39; +
&#39;&lt;Field name=&quot;PrintCompany&quot;&gt;false&lt;/Field&gt;&#39; +
&#39;&lt;Field name=&quot;ArchiveDocument&quot;&gt;false&lt;/Field&gt;&#39; +
&#39;&lt;Field name=&quot;LogInteraction&quot;&gt;false&lt;/Field&gt;&#39; +
&#39;&lt;Field name=&quot;DisplayAssemblyInformation&quot;&gt;false&lt;/Field&gt;&#39; +
&#39;&lt;/Options&gt;&#39; +
&#39;&lt;DataItems&gt;&#39; +
&#39;&lt;DataItem name=&quot;Sales Header&quot;&gt;VERSION(1) SORTING(Field1,Field3) WHERE(Field3=1(&#39; + Rec.&quot;No.&quot; + &#39;))&lt;/DataItem&gt;&#39; +
&#39;&lt;DataItem name=&quot;Sales Line&quot;&gt;VERSION(1) SORTING(Field1,Field3,Field4)&lt;/DataItem&gt;&#39; +
&#39;&lt;DataItem name=&quot;SalesLineComments&quot;&gt;VERSION(1) SORTING(Field1,Field2,Field7,Field3)&lt;/DataItem&gt;&#39; +
&#39;&lt;DataItem name=&quot;Sales Comment Line&quot;&gt;VERSION(1) SORTING(Field1,Field2,Field7,Field3)&lt;/DataItem&gt;&#39; +
&#39;&lt;DataItem name=&quot;CopyLoop&quot;&gt;VERSION(1) SORTING(Field1)&lt;/DataItem&gt;&#39; +
&#39;&lt;DataItem name=&quot;PageLoop&quot;&gt;VERSION(1) SORTING(Field1)&lt;/DataItem&gt;&#39; +
&#39;&lt;DataItem name=&quot;SalesLine&quot;&gt;VERSION(1) SORTING(Field1)&lt;/DataItem&gt;&#39; +
&#39;&lt;DataItem name=&quot;AsmLoop&quot;&gt;VERSION(1) SORTING(Field1)&lt;/DataItem&gt;&#39; +
&#39;&lt;/DataItems&gt;&#39; +
&#39;&lt;/ReportParameters&gt;&#39;;
SalesHeader.Reset();
SalesHeader.SetRange(&quot;Document Type&quot;, Rec.&quot;Document Type&quot;);
SalesHeader.SetRange(&quot;No.&quot;, Rec.&quot;No.&quot;);
SalesHeader.FindFirst();
Clear(OStream);
CurrPage.SetSelectionFilter(SalesHeader);
RecRef.GetTable(SalesHeader);
TempBlob.CreateOutStream(OStream, TextEncoding::UTF8);
Report.SaveAs(Report::&quot;Sales Order&quot;, XmlParameters, ReportFormat::Pdf, OStream, RecRef);
FileManagement.BLOBExport(TempBlob, &#39;Sales Order &#39; + Rec.&quot;No.&quot; + &#39;.pdf&#39;, 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 &quot;Sales Order List&quot;
{
actions
{
addafter(&quot;Print Confirmation&quot;)
{
action(&quot;Save as PDF&quot;)
{
Caption = &#39;Save As PDF&#39;;
ApplicationArea = All;
Image = ExportFile;
Promoted = true;
PromotedCategory = Category8;
PromotedIsBig = true;
ToolTip = &#39;Save the order as a PDF&#39;;
trigger OnAction()
var
SalesHeader: Record &quot;Sales Header&quot;;
CustomReportSelection: Record &quot;Custom Report Selection&quot;;
ReportLayoutSelection: Record &quot;Report Layout Selection&quot;;
TempBlob: Codeunit &quot;Temp Blob&quot;;
FileManagement: Codeunit &quot;File Management&quot;;
RecRef: RecordRef;
OStream: OutStream;
ReportType: Integer;
XmlParameters: Text;
begin
ReportType := Report::&quot;Sales Order&quot;;
CustomReportSelection.Reset();
CustomReportSelection.SetRange(&quot;Report ID&quot;, ReportType);
CustomReportSelection.SetRange(&quot;Source No.&quot;, Rec.&quot;Bill-to Customer No.&quot;);
if not CustomReportSelection.IsEmpty then begin
CustomReportSelection.FindFirst();
ReportLayoutSelection.SetTempLayoutSelected(System.Format(CustomReportSelection.&quot;Custom Report Layout Code&quot;))
end;
SalesHeader.Reset();
SalesHeader.SetRange(&quot;Document Type&quot;, Rec.&quot;Document Type&quot;);
SalesHeader.SetRange(&quot;No.&quot;, Rec.&quot;No.&quot;);
XmlParameters := &#39;&lt;?xml version=&quot;1.0&quot; standalone=&quot;yes&quot;?&gt;&#39; +
&#39;&lt;ReportParameters name=&quot;Sales Order&quot; id=&quot;&#39; + System.Format(ReportType) + &#39;&quot;&gt;&#39; +
&#39;&lt;Options&gt;&#39; +
&#39;&lt;Field name=&quot;NoCopies&quot;&gt;0&lt;/Field&gt;&#39; +
&#39;&lt;Field name=&quot;PrintCompany&quot;&gt;false&lt;/Field&gt;&#39; +
&#39;&lt;Field name=&quot;ArchiveDocument&quot;&gt;false&lt;/Field&gt;&#39; +
&#39;&lt;Field name=&quot;LogInteraction&quot;&gt;false&lt;/Field&gt;&#39; +
&#39;&lt;Field name=&quot;DisplayAssemblyInformation&quot;&gt;false&lt;/Field&gt;&#39; +
&#39;&lt;/Options&gt;&#39; +
&#39;&lt;DataItems&gt;&#39; +
&#39;&lt;DataItem name=&quot;Sales Header&quot;&gt;VERSION(1) SORTING(Field1,Field3) WHERE(Field3=1(&#39; + Rec.&quot;No.&quot; + &#39;),Field4=1(&#39; + Rec.&quot;Bill-to Customer No.&quot; + &#39;))&lt;/DataItem&gt;&#39; +
&#39;&lt;DataItem name=&quot;Sales Line&quot;&gt;VERSION(1) SORTING(Field1,Field3,Field4)&lt;/DataItem&gt;&#39; +
&#39;&lt;DataItem name=&quot;SalesLineComments&quot;&gt;VERSION(1) SORTING(Field1,Field2,Field7,Field3)&lt;/DataItem&gt;&#39; +
&#39;&lt;DataItem name=&quot;Sales Comment Line&quot;&gt;VERSION(1) SORTING(Field1,Field2,Field7,Field3)&lt;/DataItem&gt;&#39; +
&#39;&lt;DataItem name=&quot;CopyLoop&quot;&gt;VERSION(1) SORTING(Field1)&lt;/DataItem&gt;&#39; +
&#39;&lt;DataItem name=&quot;PageLoop&quot;&gt;VERSION(1) SORTING(Field1)&lt;/DataItem&gt;&#39; +
&#39;&lt;DataItem name=&quot;SalesLine&quot;&gt;VERSION(1) SORTING(Field1)&lt;/DataItem&gt;&#39; +
&#39;&lt;DataItem name=&quot;AsmLoop&quot;&gt;VERSION(1) SORTING(Field1)&lt;/DataItem&gt;&#39; +
&#39;&lt;/DataItems&gt;&#39; +
&#39;&lt;/ReportParameters&gt;&#39;;
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) + &#39; &#39; + Rec.&quot;No.&quot; + &#39;.pdf&#39;, true);
ReportLayoutSelection.SetTempLayoutSelected(&#39;&#39;);
end;
}
}
}
}

huangapple
  • 本文由 发表于 2023年7月18日 04:41:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707940.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定