0

I want to attach a report pdf to files section of an invoice n Acumatica. I tried the below code snippet. It works for a single invoice only. But when we process multiple invoices from Process Invoices screen, file is attaching only for first invoice. For other invoices, file is not attaching. I believe this issue is because of wrong cache passing in this line. PXNoteAttribute.AttachFile(Base.Caches[typeof(ARInvoice)], invoice, file);

Any idea about fixing this? Below is my full code.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using PX.Common;
using PX.Data;
using PX.Objects.AP;
using PX.Objects.AR;
using PX.Objects.CA;
using PX.Objects.CM;
using PX.Objects.CR;
using PX.Objects.CS;
using PX.Objects.DR;
using PX.Objects.EP;
using PX.Objects.GL;
using PX.Objects.IN;
using PX.Objects.PM;
using PX.Objects.PO;
using PX.Objects.TX;
using POLine = PX.Objects.PO.POLine;
using POOrder = PX.Objects.PO.POOrder;
using System.Threading.Tasks;
using CRLocation = PX.Objects.CR.Standalone.Location;
using PX.Objects.AR.CCPaymentProcessing;
using PX.Objects.AR.CCPaymentProcessing.Common;
using PX.Objects.AR.CCPaymentProcessing.Helpers;
using PX.Objects.AR.CCPaymentProcessing.Interfaces;
using ARRegisterAlias = PX.Objects.AR.Standalone.ARRegisterAlias;
using PX.Objects.AR.MigrationMode;
using PX.Objects.Common;
using PX.Objects.Common.Discount;
using PX.Objects.Common.Extensions;
using PX.Objects.IN.Overrides.INDocumentRelease;
using PX.CS.Contracts.Interfaces;
using PX.Data.DependencyInjection;
using PX.Data.WorkflowAPI;
using PX.Objects.Extensions.PaymentTransaction;
using PX.Objects.SO.GraphExtensions.CarrierRates;
using PX.Objects.SO.GraphExtensions.SOOrderEntryExt;
using PX.Objects.SO.Attributes;
using PX.Objects.Common.Attributes;
using PX.Objects.Common.Bql;
using OrderActions = PX.Objects.SO.SOOrderEntryActionsAttribute;
using PX.Objects.SO.DAC.Projections;
using PX.Data.BQL.Fluent;
using PX.Objects;
using PX.Objects.SO;
using PX.Data.Reports;
using PX.SM;
using PX.Reports.Controls;
using PX.Reports.Data;
using System.Configuration;
using PX.Reports;

using CommonServiceLocator;

namespace PX.Objects.AR
{
    public class ARInvoiceEntry_Extension : PXGraphExtension<ARInvoiceEntry>
    {
        #region Event Handlers

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

        [InjectDependency]
        protected internal PX.Reports.IReportDataBinder ReportDataBinder { get; private set; }

        public delegate IEnumerable ReleaseDelegate(PXAdapter adapter);
        [PXOverride]
        public IEnumerable Release(PXAdapter adapter, ReleaseDelegate baseMethod)
        {
            foreach (ARInvoice invoice in adapter.Get<ARInvoice>())
            {
                //Report Paramenters
                Dictionary<String, String> parameters = new Dictionary<String, String>();
                parameters["ARInvoice.DocType"] = invoice.DocType;
                parameters["ARInvoice.RefNbr"] = invoice.RefNbr;
                PXReportSettings settings = new PXReportSettings("AR641000");

                //Report Processing
                PX.Reports.Controls.Report report =
                ReportLoader.CheckIfNull(nameof(ReportLoader)).LoadReport("AR641000", null);
                ReportLoader.InitReportParameters(report, parameters, settings, false);
                PX.Reports.Data.ReportNode reportNode =
                ReportDataBinder.CheckIfNull(nameof(ReportDataBinder)).ProcessReportDataBinding(report);

                //Generation PDF
                byte[] data = PX.Reports.Mail.Message.GenerateReport(reportNode,
                              RenderType.FilterPdf).First();
                PX.SM.FileInfo file = new PX.SM.FileInfo(reportNode.ExportFileName + "- " + invoice.RefNbr + ".pdf", null, data);

                var uploadFileMaintenance = PXGraph.CreateInstance<UploadFileMaintenance>();
                uploadFileMaintenance.SaveFile(file);
                PXNoteAttribute.AttachFile(Base.Caches[typeof(ARInvoice)], invoice, file);

            }

            return baseMethod(adapter);
        }
        #endregion
    }
}
John C
  • 1
  • 1

1 Answers1

0

Please see this code snippet, it attached the files to all released invoice in my instance.

            var uploadFileMaintenance = PXGraph.CreateInstance<UploadFileMaintenance>();
            uploadFileMaintenance.SaveFile(file);
            
            Base.Document.Current = invoice;
            PXNoteAttribute.AttachFile(Base.Document.Cache, invoice, file);
            Base.Save.Press();
Zoltan Febert
  • 471
  • 5
  • 9