January 20, 2015

Error:Failed to create a session; confirm that the user has the proper privileges to log on to Microsoft Dynamics

Error Message: Failed to create a session; confirm that the user has the proper privileges to log on to Microsoft Dynamics.

image

Solution: Delete AUC files and restart the services.





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