Saturday, September 9, 2017

How to run a Report through X++ code and save to PDF format

static void autoSave_PDF_SSRSSalesInoive(Args _args)
{
    Dialog                                    dialog;
    DialogField                          fromDate, toDate, invoiceId, folderPath;
    CustInvoiceJour                  custInvoiceJourLocal;
    FilePath                              filePath, slash = @"\";
    Filename                            fileName;
    Args                                   args = new Args();
    boolean                             folderexists,fileExists;
    str                                      idstr;
    int                 poss;
    SrsReportRunController              ssrsController = new SrsReportRunController();
    InvoiceContract                           salesConfirmContract = new InvoiceContract();
    SRSPrintDestinationSettings      printerSettings;  
    ;
    dialog = new Dialog("Sales Invoices save to PDF");
    fromDate = dialog.addField(extendedTypeStr(TransDate), "Select From Date");
    toDate = dialog.addField(extendedTypeStr(TransDate), "Select To Date");
    if(dialog.run())
    {
        if(curUserId() == "userid")
            filePath = @"C:\Users\userid\Desktop\Invoices\" + strFmt("%1%2%3",fromDate.value(), " To ", toDate.value()) + slash;
        else
            filePath = @"C:\JB\" + strFmt("%1%2%3",fromDate.value(), " To ", toDate.value()) + slash;
        while(poss < strLen(filePath))
        {
            poss = strFind(filePath, "/", poss, strLen(filePath));
            filePath = strPoke(filePath, "-", poss);
            if(!poss)
                break;
        }
        folderexists = WinAPI::folderExists(filePath);
        if(!folderexists)
            WinAPI::createDirectoryPath(filePath);
         while select custInvoiceJourLocal where custInvoiceJourLocal.InvoiceDate >=     fromDate.value() && custInvoiceJourLocal.InvoiceDate <= toDate.value()
              {
            fileName = filePath + strFmt("%1%2%3%4%5",custInvoiceJourLocal.PrintMgmtSiteId + "_",custInvoiceJourLocal.InvoiceId, "_" , custInvoiceJourLocal.InvoiceDate, ".pdf");
            while(poss < strLen(fileName))
            {
                poss = strFind(fileName, "/", poss, strLen(fileName));
                fileName = strPoke(fileName, "-", poss);
                if(!poss)
                    break;
            }
            fileExists = System.IO.File::Exists(fileName);
            if(!fileExists)
            {
                ssrsController.parmReportName(ssrsReportStr(SalesInvoice, Report));
                   //define how we want to execute the report (right now or batch style)
                ssrsController.parmExecutionMode(SysOperationExecutionMode::Synchronous);
                //hide the report dialog
                ssrsController.parmShowDialog(false);

                //we need to populate the required parms for the current sales order controller
                salesConfirmContract.parmInvoiceId(custInvoiceJourLocal.InvoiceId);
                salesConfirmContract.parmInvoiceDate(custInvoiceJourLocal.InvoiceDate);
                salesConfirmContract.parmSalesId(custInvoiceJourLocal.SalesId);
 
                //link the contract to the controller so we know how to run the dp
                ssrsController.parmReportContract().parmRdpContract(salesConfirmContract);

                //link the printer settings to the controller
                printerSettings = ssrsController.parmReportContract().parmPrintSettings();
                //print to pdf and always overwrite if the file exists
                printerSettings.printMediumType(SRSPrintMediumType::File);
                printerSettings.fileFormat(SRSReportFileFormat::PDF);
                printerSettings.overwriteFile(true);
                printerSettings.fileName(fileName);

                //run & save the report
                ssrsController.startOperation();
               info(strFmt("File Saved at %1", fileName));
            }
        }
    }
}

No comments:

Post a Comment

update_recordset with joins

 update_recordset with joins update_recordSet storeTransfer         setting      TransactionId = transfertable.TransferId     join transfert...