2

We have a requirement to attach custom report to email as an attachment. As per our requirement we created a process screen for this. Issue we are facing is, when we use below line of code we are getting “OBJECT REFERENCE ERROR”, can you please have a look at below sample code.

  public class ProcessReports : PXGraph<ProcessReports>
  {
    [InjectDependency]
    protected IReportLoaderService ReportLoader { get; private set; }

    [InjectDependency]
    protected IReportRenderer ReportRenderer { get; private set; }

    public PXCancel<DACName> Cancel;

    public PXProcessing<DACName, Where<DACName.DACName, Equal<OPR>>> QueueList;
    public ProcessReports()
    {
        QueueList.SetProcessDelegate(delegate (List<DACName> list)
        {
            SaveFile(list, true);
        });
    }

    public static void SaveFile(List<DACName> list, bool aIsMassProcess)
    {
        new KWProcessWorkCenterOpenJobReports().save(list, aIsMassProcess);
    }
    public virtual void save(List<DACName> list, bool aIsMassProcess)
    {
        //here at this point we are getting error
        Report _report = ReportLoader.LoadReport("AA501108", null);
    }
}
John
  • 763
  • 3
  • 11
  • As an aside to your question it was noticed that your report is utilizing the "AA" prefix. Prefix's within Acumatica can be reserved by different companies for exclusive use with an ISV solution or bundle of products. If you accidently utilize a reserved prefix and it collides on a client with said product you will need to change the report number in the future. – Joshua Van Hoesen Feb 15 '23 at 16:25

1 Answers1

1

Looking at your code my inference is the object with the IReportLoaderService and IReportRenderer interface decleration never properly gets initialized with PXGraph.CreateInstance<...> before calling save method.

new KWProcessWorkCenterOpenJobReports().save(list, aIsMassProcess);

The following code snippet will generate a report from the given paramaters provided and then save the file within the Acumatica system, though it can be further emailed out as mentioned in your original prompt.

using PX.Data;
using PX.Objects.AR;
using PX.Reports;
using PX.Reports.Controls;
using PX.Reports.Data;
using PX.SM;
using System.Collections.Generic;
using MailMessage = PX.Reports.Mail.Message;

namespace StackOverflow
{
    public class EmailReportProc : PXGraph<EmailReportProc>
    {
        #region Constructor
        public EmailReportProc()
        {
            Documents.SetProcessDelegate(generateAcumaticaReports);
        }
        #endregion

        #region Properties

        [InjectDependency]
        protected IReportLoaderService ReportLoader { get; set; }

        [InjectDependency]
        protected IReportDataBinder ReportDataBinder { get; set; }

        #endregion

        #region Views

        public PXCancel<ARInvoice> Cancel;

        public PXProcessing<ARInvoice, Where<ARInvoice.status, Equal<ARDocStatus.pendingEmail>>> Documents;

        #endregion

        #region Methods
        private static void generateAcumaticaReports(List<ARInvoice> Documents)
        {
            //Initialize new graph instance for use within static processing method.
            EmailReportProc graph = PXGraph.CreateInstance<EmailReportProc>();

            foreach (ARInvoice document in Documents)
            {
                //Paramaters for report
                Dictionary<string, string> parameters = new Dictionary<string, string>
                {
                    { "ARInvoice.DocType", document.DocType },
                    { "ARInvoice.RefNbr", document.RefNbr }
                };

                //Load report - ReportID must be valid sitemap entry in system
                Report report = graph.ReportLoader.LoadReport("AR622000", null);

                //Initialize the parameters for the report
                graph.ReportLoader.InitDefaultReportParameters(report, parameters);

                ReportNode reportNode = graph.ReportDataBinder.ProcessReportDataBinding(report);

                //Generate PDF Report and creates Acumatica file.
                FileInfo fileInfo = new FileInfo(reportNode.ExportFileName + ".pdf", null, MailMessage.GenerateReport(reportNode, RenderType.FilterPdf)[0]);

                UploadFileMaintenance uploadFileMaintenance = PXGraph.CreateInstance<UploadFileMaintenance>();

                //Save Generated file into system
                _ = uploadFileMaintenance.SaveFile(fileInfo);
            }
        }
        #endregion
    }
}
Joshua Van Hoesen
  • 1,684
  • 15
  • 30