Bojensen Blogs

Run, Save and Email Purchase order report through X++ code

Hi Friends,

In this post I will demonstrate how to do below through X++ code.

We will take Purchase order report as an example

1. Run a report
2. Save as PDF at local drive
3. Email this report
4. Delete from local drive after email.
To perform above task , create a new class and add below methods
To Run and Save Purchase order report through X++ code
public str runAndSaveSSRSReport(PurchTable _purchTable)
{
    SrsReportRunController          ssrsController = new SrsReportRunController();
    PurchPurchaseOrderContract      Contract = new PurchPurchaseOrderContract();
    SRSPrintDestinationSettings     printerSettings;
    VendPurchOrderJour              VendPurchOrderJour;
    str                             ReportPath;


    select firstOnly VendPurchOrderJour
    order by VendPurchOrderJour.createdDateTime DESC
    where VendPurchOrderJour.PurchId == _PurchTable.PurchId;

    ReportPath = "C:\\" + _PurchTable.PurchId +".pdf";

    ssrsController.parmReportName(ssrsReportStr(PurchPurchaseOrder, Report));

    ssrsController.parmExecutionMode(SysOperationExecutionMode::Synchronous);

    ssrsController.parmShowDialog(false);

    Contract.parmRecordId(VendPurchOrderJour.RecId);

    ssrsController.parmReportContract().parmRdpContract(Contract);


    //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(@ReportPath);

    //run & save the report
    ssrsController.runReport();
    return ReportPath;// return the file location where pdf saved. we use this path to email and delete after email.
}

To Email this report

public void POConfirmationEmail(PurchTable   _PurchTable)
{
    PurchTable                  PurchTable;
    Map                         parameterMap = new Map(Types::String, Types::String);
    Email                       requester;
    SysEmailId                  ApprovalEmailTemplate;
    SysEmailId                  ReopenEmailTemplate;
    int                         itemCount = 1;
    str                         ItemId, ItemQty, ItemDeliveryDate, ItemPrice;
    str                         companyDetails;
    FilenameOpen                attachmentFilename;

    companyDetails = curext();
    ParameterMap.insert('CompanyDetails',companyDetails);
    PurchTable = _PurchTable;
    attachmentFilename = this.runAndSaveSSRSReport(PurchTable);
    ParameterMap.insert('VendorName', VendTable::find(PurchTable.OrderAccount).name());
    ParameterMap.insert('PurchaseID', PurchTable.PurchId);
    requester   = LogisticsElectronicAddress::findRecId(DirPartyTable::findRec(VendTable::find(PurchTable.OrderAccount).Party).PrimaryContactEmail).Locator;

    if(!requester)
    {
        throw error("No Email address is available");
    }
    else
    {
        SysEmailTable::sendMail("PoEmail", companyinfo::languageId(), requester, parameterMap, attachmentFilename); //PoEmail is emial template with two parameter (VendName and PurchId, you can make changes in template as per user requirement. 

        this.deleteReportFile(attachmentFilename);// To delete this file after email.
    }
}

 

Delete report file from your local drive
public void deleteReportFile(str _ReportPath)
{
    str         ReportPath = _ReportPath;

    if(!ReportPath)
        warning("No file in local to remove");
    else
        WinAPI::deleteFile(@ReportPath);
}

Now all you need is to call this class during confirmation of PO. Put your Feedback/question/Queries in comments.

Enjoy……
-Harry

Comments are closed.