Search This Blog

January 19, 2015

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

Hi Friends,

Sometime customer we face this requirement to send SSRS reports directly on emails of customer/vendor like Purchase order or Sales order report. In this post, I will demonstrate how to do these kinds of stuff in very easy steps, 

To understand each and every logic, we will perform this whole process in three steps. Let’s take an example of Purchase order report. 

1. Run a report and Save as PDF at local drive
2. Email this report
3. Delete from local drive after email.

It would be suggested to have all code into a single class than writing the code here and there on forms or any other standard classes. 

1.      Run a report and Save as PDF at local drive: 
Create a new class and add a new method for run a report and save it at your local/shared path. 

   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));

    //link the printer settings to the controller
    printerSettings = ssrsController.parmReportContract().parmPrintSettings();
    //print to pdf and always overwrite if the file exists

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

2.      Email this report: 
Now when we have report saved on local drive we can pick this file to attach in email. Add a new method in your class as below,

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();
    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;

        throw error("No Email address is available");
        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.

3.      Delete report file from your local drive:
Now after email we have to delete this from memory, lets add one more method in your class to delete this file,

public void deleteReportFile(str _ReportPath)
    str         ReportPath = _ReportPath;

        warning("No file in local to remove");

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



No comments:

Post a Comment