2016-11-21 74 views
0

我需要修改採購訂單,因此用戶可以選擇和保存PO爲PDF在目標文件夾導出報表爲PDF AX 2012 CU10

![Purchase Order Form 我創建了一個新的按鈕,讓用戶選擇數據和點擊後按鈕,系統會創建一個PDF文件並將其保存到目標文件夾。

這是我的代碼

public static void main(Args _args) 
{ 
PurchTable      purchTable,purchTable2, row; 

PurchPurchaseOrderController purchPurchaseOrderController ; 
PurchPurchaseOrderContract  purchPurchaseOrderContract; 
SrsReportRunImpl    srsReportRun; 
VendPurchOrderJour    vendPurchOrderJour; 
FormDataSource     purchTable_ds; 

str        PDFName; 
int        tot = 0; 

Args       args ; 
ReportName      reportName = "PurchPurchaseOrder.ReportPRI"; 
; 

    if(_args.record().TableId == tableNum(PurchTable)) 
{ 
    purchTable2 = _args.record(); 
    purchTable_ds = purchTable2.dataSource();  
} 

if (purchTable_ds.anyMarked()) 
{ 
    row = purchTable_ds.getFirst(1, false); 
    info("a1"); 
    while(row) 
    {   
     tot++; 

     info(row.PurchId); 

     args = new Args(); 

     args.record(row); 

     select firstFast purchTable where purchTable.RecId == row.RecId ; 

     select firstFast vendPurchOrderJour where vendPurchOrderJour.PurchId == purchTable.PurchId ; 

     PDFName = strFmt("C:\\SharePDF\\%1.pdf",strReplace(purchTable.PurchId,'/','_')); 

     purchPurchaseOrderController = new PurchPurchaseOrderController(); 
     purchPurchaseOrderController.parmReportName(ReportName); 

     purchPurchaseOrderContract = purchPurchaseOrderController.parmReportContract().parmRdpContract(); 

     purchPurchaseOrderContract.parmRecordId(vendPurchOrderJour.RecId); 

     purchPurchaseOrderController.parmArgs(args); 

     srsReportRun = purchPurchaseOrderController.parmReportRun() as SrsReportRunImpl; 

     purchPurchaseOrderController.parmReportRun(srsReportRun); 

     purchPurchaseOrderController.parmReportContract().parmReportExecutionInfo(new SrsPrintMgmtExecutionInfo()); 

     purchPurchaseOrderController.parmReportContract().parmPrintSettings().printMediumType(SRSPrintMediumType::File); 
     purchPurchaseOrderController.parmReportContract().parmPrintSettings().overwriteFile(true); 
     purchPurchaseOrderController.parmReportContract().parmPrintSettings().fileFormat(SRSReportFileFormat::PDF); 
     purchPurchaseOrderController.parmReportContract().parmPrintSettings().fileName(PDFName); 

     purchPurchaseOrderController.runReport(); 


     row = purchTable_ds.getNext(); 
    } 
} 

info(strFmt("%1",tot)); 

} 

此代碼運行,將創建一個PDF文件,但只在最後PO。

任何建議如何解決這個問題?謝謝

+0

,如果你在這行代碼debugg:'行= purchTable_ds.getFirst(1,假);'獲得第一PO或得到一個不正確的值? –

+0

它得到最後的採購訂單。 – julius

+0

嘗試使用[MultiSelectionHelper](https://msdn.microsoft.com/en-us/library/multiselectionhelper.aspx)類。 [通過MultiSelectionHelper類獲取精選記錄](http://mafsarkhan.blogspot.de/2013/05/get-select-records-by.html)是許多關於如何使用它的教程之一。 –

回答

0

這是已經得到解決。感謝所有。

這是我的完整代碼

public static void main(Args _args) 
{ 
PurchTable      purchTable,purchTable2, row; 
PurchPurchaseOrderContract  purchPurchaseOrderContract; 

VendPurchOrderJour    vendPurchOrderJour; 
FormDataSource     purchTable_ds; 
SRSPrintDestinationSettings  settings; 
SrsReportRunController   controller ; 

str        folderPath; 
dialog       d; 

DialogField      dialogFilename; 

str        PDFName, FilePath; 
int        tot = 0; 

Args       args; 
ReportName      reportName = "PurchPurchaseOrder.ReportPRI"; 

PurchTable      _PurchTable; 
MultiSelectionHelper   _helper = MultiSelectionHelper::construct(); 
; 


d = new dialog(); 
d.caption("select a folder"); 
dialogFilename = d.addField(extendedTypeStr(FilePath));//add a field where you select your file in a specific path 
d.run(); 

if(d.closedOk()) 
{ 
    folderPath = dialogFileName.value();//return path file value 

    if(folderPath == '') 
    { 
     folderPath = 'C:\\SharePdf'; 
    } 


    if(_args.record().TableId == tableNum(PurchTable)) 
    { 
     purchTable2 = _args.record(); 
     purchTable_ds = purchTable2.dataSource(); 
    } 


if (purchTable_ds.anyMarked()) 
{ 
    row = purchTable_ds.getFirst(1, false); 

    while(row) 
    { 
     tot++; 

     args = new Args(); 

     select firstFast purchTable where purchTable.RecId == row.RecId ; 

     select firstFast vendPurchOrderJour where vendPurchOrderJour.PurchId == purchTable.PurchId ; 

     PDFName = strFmt("%2\\%1.pdf",strReplace(purchTable.PurchId,'/','_'), folderPath); 

     controller = new SrsReportRunController(); 
     purchPurchaseOrderContract = new PurchPurchaseOrderContract(); 

     controller.parmReportName(ReportName); 
     controller.parmExecutionMode(SysOperationExecutionMode::Synchronous); 
     controller.parmShowDialog(false); 

     purchPurchaseOrderContract.parmRecordId(vendPurchOrderJour.RecId); 

     args.record(row); 

     controller.parmReportContract().parmRdpContract(purchPurchaseOrderContract); 
     controller.parmArgs(args); 

     settings = controller.parmReportContract().parmPrintSettings(); 
     settings.printMediumType(SRSPrintMediumType::File); 
     settings.fileFormat(SRSReportFileFormat::PDF); 
     settings.overwriteFile(true); 
     settings.fileName(PDFName); 

     controller.startOperation(); 

     row = purchTable_ds.getNext(); 
    } 

} 

info(strFmt("%1 Total",tot)); 

} 

} 
0

似乎你的錯誤在這一行row = purchTable_ds.getFirst(1, false);你需要一個MultiSelectionHelper類。

嘗試使用此代碼:

public static void main(Args _args) 
{ 
    PurchTable      purchTable,purchTable2, row; 

    PurchPurchaseOrderController purchPurchaseOrderController ; 
    PurchPurchaseOrderContract  purchPurchaseOrderContract; 
    SrsReportRunImpl    srsReportRun; 
    VendPurchOrderJour    vendPurchOrderJour; 
    FormDataSource     purchTable_ds; 

    str        PDFName; 
    int        tot = 0; 

    Args       args ; 
    ReportName      reportName = "PurchPurchaseOrder.ReportPRI"; 

    //New 
    PurchTable   _PurchTable; 
    MultiSelectionHelper _helper = MultiSelectionHelper::construct(); 
    //New end 
    ; 

    //New 
    _helper.parmDatasource(purchTable_ds); 
    _PurchTable = _helper.getFirst(); 

    while(_PurchTable.RecId != 0){ 
      Purchtable = _PurchTable; 
    //New end    
      tot++; 

      //info(row.PurchId); 

      //args = new Args(); 

      //args.record(row); 

      //select firstFast purchTable where purchTable.RecId == row.RecId ; 

      select firstFast vendPurchOrderJour where vendPurchOrderJour.PurchId == purchTable.PurchId ; 

      PDFName = strFmt("C:\\SharePDF\\%1.pdf",strReplace(purchTable.PurchId,'/','_')); 

      purchPurchaseOrderController = new PurchPurchaseOrderController(); 
      purchPurchaseOrderController.parmReportName(ReportName); 

      purchPurchaseOrderContract = purchPurchaseOrderController.parmReportContract().parmRdpContract(); 

      purchPurchaseOrderContract.parmRecordId(vendPurchOrderJour.RecId); 

      purchPurchaseOrderController.parmArgs(args); 

      srsReportRun = purchPurchaseOrderController.parmReportRun() as SrsReportRunImpl; 

      purchPurchaseOrderController.parmReportRun(srsReportRun); 

      purchPurchaseOrderController.parmReportContract().parmReportExecutionInfo(new SrsPrintMgmtExecutionInfo()); 

      purchPurchaseOrderController.parmReportContract().parmPrintSettings().printMediumType(SRSPrintMediumType::File); 
      purchPurchaseOrderController.parmReportContract().parmPrintSettings().overwriteFile(true); 
      purchPurchaseOrderController.parmReportContract().parmPrintSettings().fileFormat(SRSReportFileFormat::PDF); 
      purchPurchaseOrderController.parmReportContract().parmPrintSettings().fileName(PDFName); 

      purchPurchaseOrderController.runReport(); 

      //New 
      _PurchTable = _helper.getNext(); 
      //New end 
     } 


    info(strFmt("%1",tot)); 

} 
+0

感謝您的回答,但問題依然存在。你的代碼仍然只能生成1個pdf。我認爲問題出現在'purchPurchaseOrderController.runReport();'因爲'info(strFmt(「%1」,tot));'不顯示 – julius

+0

好,但是這條線正常嗎? '_PurchTable = _helper.getFirst();'''while(...)'正常工作? –

+0

,工作正常。如果我評論''//purchPurchaseOrderController.runReport();'它顯示所有采購訂單編號,並獲得總採購訂單,但不會創建PDF – julius