January 19, 2015

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

Sending SSRS Reports via Email in D365FO: Simplified Steps

Sending SSRS Reports via Email in D365FO: Simplified Steps

Introduction

In some cases, customers may require the ability to send SSRS reports directly to their email addresses, such as Purchase Order or Sales Order reports. In this blog post, we will demonstrate how to achieve this functionality using simple and straightforward steps.

Step 1: Running the Report and Saving it Locally as PDF

To begin, create a new class and add a method that runs the report and saves it to a local or shared path. Here is an example code snippet:

    
public str runAndSaveSSRSReport(PurchTable _purchTable)
{
    // Initialize variables
    SrsReportRunController ssrsController = new SrsReportRunController();
    PurchPurchaseOrderContract contract = new PurchPurchaseOrderContract();
    SRSPrintDestinationSettings printerSettings;
    VendPurchOrderJour vendPurchOrderJour;
    str reportPath;

    // Retrieve the latest purchase order for the specified PurchTable
    select firstOnly vendPurchOrderJour
        order by vendPurchOrderJour.createdDateTime DESC
        where vendPurchOrderJour.PurchId == _purchTable.PurchId;

    // Set the report path to save the PDF
    reportPath = "C:\\" + _purchTable.PurchId + ".pdf";

    // Configure the report controller
    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();
    printerSettings.printMediumType(SRSPrintMediumType::File);
    printerSettings.fileFormat(SRSReportFileFormat::PDF);
    printerSettings.overwriteFile(true);
    printerSettings.fileName(@reportPath);

    // Run and save the report
    ssrsController.runReport();

    return reportPath; // Return the file location where the PDF is saved
}
    
  

Step 2: Emailing the Report

Once the report is saved locally, you can proceed with attaching and sending it via email. Add the following method to your class:

    
public void POConfirmationEmail(PurchTable _purchTable)
{
    // Initialize variables
    PurchTable purchTable;
    Map parameterMap = new Map(Types::String, Types::String);
    Email requester;
    SysEmailId ApprovalEmailTemplate;
    SysEmailId ReopenEmailTemplate;
    str companyDetails;
    FilenameOpen attachmentFilename;

    companyDetails = curext();
    parameterMap.insert('CompanyDetails', companyDetails);

    purchTable = _purchTable;
    attachmentFilename = this.runAndSaveSSRSReport(purchTable);

    // Set email parameters
    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
    {
        // Send email using the "PoEmail" template and delete the report file after sending
        SysEmailTable::sendMail("PoEmail", companyinfo::languageId(), requester, parameterMap, attachmentFilename);
        this.deleteReportFile(attachmentFilename);
    }
}
    
  

Step 3: Deleting the Report File

Finally, add the following method to your class to delete the report file from the local drive:

    
public void deleteReportFile(str _reportPath)
{
    str reportPath = _reportPath;

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

Conclusion

By following the above three steps, you can easily send SSRS reports via email in D365FO. Consolidating the code into a single class enhances reusability and simplifies maintenance. Feel free to leave your feedback, questions, or queries in the comments section.

Enjoy the streamlined process!

- Harry

January 09, 2015

How to add new fields/Methods in Listpage from in AX 2012

How to add new fields/Methods in List page from in AX 2012
Here we use the Production Order list page for example
Open Production order list page from below link
Production control/Common/Production orders/All production orders
clip_image002
Now we need to add one new field “Serial Number” in this grid (Assuming there will be only one item in one Production order in every case).
This serial number is available in “Transaction” from of this production order.
Untitled
Now to display new fields you can add a new display method in table and drag-drop this method in a grid as a field.
Add one new method in “ProdTable” table add copy below code
display InventSerialId inventTransInventSerialId()
{
InventTransOrigin InventTransOrigin;
InventTrans InventTrans;
;
select InventTrans join InventTransOrigin
where InventTrans.InventTransOrigin == InventTransOrigin.RecId &&
InventTrans.ItemId == InventTransOrigin.ItemId &&
InventTransOrigin.ReferenceId == this.ProdId &&
InventTransOrigin.ItemId == this.ItemId;
return InventDim::find(InventTrans.inventDimId).inventSerialId;
}
Now set DataSource property of this new field as ProdTable. Save your from and compile for any error.
Now open this from, here is your from
clip_image008

-Harry