BhavyaSri
BhavyaSri

Reputation: 150

How do we attach report PDF/Excel file when i click on custom action on Sales order screen using Acumatica code

We have a requirement is to create new action on sales order screen and when we click on that custom action we are opening email activity screen to send an email in that email we want to attach a report PDF/Excel file without clicking report action button separately, but it should get happen when we click on custom action button through code.

Is this possible to attach the report file when we click on custom action button instead of report action.

Thanks in advance.

Upvotes: 1

Views: 500

Answers (1)

Marco Villaseñor
Marco Villaseñor

Reputation: 498

This is possible using PXReportTools and PX.SM.FileInfo in your custom action. As Dmitrii Naumov pointed out in the comments, the method posted by Joshua Van Hoesen does this, you just need to set the report you need and set the parameters it expects.

Here is the required code for anyone that finds this question:

public class ARInvoiceEntryExtension : PXGraphExtension<ARInvoiceEntry>
{
    public PXAction<ARInvoice> attachReport;
    [PXUIField(DisplayName = "Attach Report")]
    [PXButton]
    public virtual IEnumerable AttachReport(PXAdapter adapter)
    {
        //Report Paramenters
        Dictionary<String, String> parameters = new Dictionary<String, String>();
        parameters["ARInvoice.DocType"] = Base.Document.Current.DocType;
        parameters["ARInvoice.RefNbr"] = Base.Document.Current.RefNbr;

        //Report Processing
        PX.Reports.Controls.Report _report = PXReportTools.LoadReport("AR641000", null);
        PXReportTools.InitReportParameters(_report, parameters,
                SettingsProvider.Instance.Default);
        ReportNode reportNode = ReportProcessor.ProcessReport(_report);

        //Generation PDF
        byte[] data = PX.Reports.Mail.Message.GenerateReport(reportNode,
        ReportProcessor.FilterPdf).First();

        PX.SM.FileInfo file = new PX.SM.FileInfo(reportNode.ExportFileName + ".pdf", null, data);

        UploadFileMaintenance graph = new UploadFileMaintenance();
        graph.SaveFile(file);
        PXNoteAttribute.AttachFile(Base.Document.Cache, Base.Document.Current, file);

        return adapter.Get();
    }
}

Upvotes: 3

Related Questions