Solved

Attach file to files section of an AP Invoice from Release AP Documents screen is not working

  • 10 July 2023
  • 7 replies
  • 127 views

Userlevel 4
Badge +1

Hi All,

 

I have previously asked this question for an AR Invoice in a different approach. But I was unable to resolve it using the replies of that question because of some issues(Release method is not triggering when we process invoices from a process screen).

However, Now I want to attach a report pdf to files section of an AP Invoice through Release AP Documents screen. I tried to override Release action in APPaymentEntry graph. But it is not triggering when releasing invoices through Release AP Documents screen. Then I tried the below code snippet & it is triggering for each AP Invoice. There are no errors in the code. But file is not attaching as expected. Below is my code.

Acumatica Version : 21.213

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using PX.Common;
using PX.Data;
using PX.Data.DependencyInjection;
using PX.Data.WorkflowAPI;
using PX.LicensePolicy;
using PX.Objects.Common;
using PX.Objects.Common.Extensions;
using PX.Objects.Common.Bql;
using PX.Objects.Common.Discount;
using PX.Objects.GL;
using PX.Objects.GL.FinPeriods;
using PX.Objects.CM;
using PX.Objects.CS;
using PX.Objects.CR;
using PX.Objects.CT;
using PX.Objects.PM;
using PX.Objects.TX;
using PX.Objects.IN;
using PX.Objects.IN.Services;
using PX.Objects.CA;
using PX.Objects.BQLConstants;
using PX.Objects.EP;
using PX.Objects.PO;
using PX.Objects.SO;
using PX.Objects.DR;
using PX.Objects.AR;
using PX.TM;
using AP1099Hist = PX.Objects.AP.Overrides.APDocumentRelease.AP1099Hist;
using AP1099Yr = PX.Objects.AP.Overrides.APDocumentRelease.AP1099Yr;
using PX.Objects.GL.Reclassification.UI;
using Branch = PX.Objects.GL.Branch;
using CRLocation = PX.Objects.CR.Standalone.Location;
using PX.Objects.AP.BQL;
using PX.Objects.Common.GraphExtensions.Abstract;
using PX.Objects.Common.GraphExtensions.Abstract.DAC;
using PX.Objects.Common.GraphExtensions.Abstract.Mapping;
using PX.Objects.Extensions.CostAccrual;
using PX.Data.BQL.Fluent;
using PX.Data.BQL;
using PX.Objects;
using PX.Objects.AP;
using PX.Data.Reports;
using PX.Reports.Data;
using PX.SM;
using PX.Reports;

namespace PX.Objects.AP
{
public class APReleaseProcess_Extension : PXGraphExtension<APReleaseProcess>
{
[InjectDependency]
protected IReportLoaderService ReportLoader { get; private set; }

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

#region Event Handlers
public delegate List<APRegister> ReleaseDocProcDelegate(JournalEntry je, APRegister doc, Boolean isPrebooking, ref List<INRegister> inDocs);
[PXOverride]
public List<APRegister> ReleaseDocProc(JournalEntry je, APRegister doc, Boolean isPrebooking, ref List<INRegister> inDocs, ReleaseDocProcDelegate baseMethod)
{
var retVal = baseMethod(je, doc, isPrebooking, ref inDocs);

APInvoice apInvoice = new PXSelect<APInvoice, Where<APInvoice.refNbr, Equal<Required<APInvoice.refNbr>>,
And<APInvoice.docType, Equal<Required<APInvoice.docType>>>>>(Base).Select(doc.RefNbr, doc.DocType);

if (apInvoice == null)
{
return retVal;
}

//Report Paramenters
Dictionary<String, String> parameters = new Dictionary<String, String>();
parameters["DocType"] = apInvoice.DocType;
parameters["RefNbr"] = apInvoice.RefNbr;
PXReportSettings settings = new PXReportSettings("MA31099");

//Report Processing
PX.Reports.Controls.Report report =
ReportLoader.CheckIfNull(nameof(ReportLoader)).LoadReport("MA31099", 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 + "- " + apInvoice.RefNbr + ".pdf", null, data);

var uploadFileMaintenance = PXGraph.CreateInstance<UploadFileMaintenance>();
uploadFileMaintenance.SaveFile(file);

PXNoteAttribute.AttachFile(Base.Caches[typeof(APInvoice)], apInvoice , file);

return retVal;
}

#endregion
}
}

As an alternative options I tried updating cache as below as well. But no luck.
 

 Base.Caches[typeof(APInvoice)].Insert(apInvoice);

Do you have any idea about this?

icon

Best answer by charithalakshan49 20 August 2023, 18:57

View original

7 replies

Userlevel 7
Badge +17

Hi @charithalakshan49   I do not see any issue with the ReleaseDocProcDelegate override code and it should work.

Can you please confirm that this code is not invoking when you debug or your custom logic is not working?

 

public class APReleaseProcess_Extension : PXGraphExtension<PX.Objects.AP.APReleaseProcess>
{
#region Event Handlers
public delegate List<APRegister> ReleaseDocProcDelegate(JournalEntry je, APRegister doc, Boolean isPrebooking, ref List<INRegister> inDocs);
[PXOverride]
public List<APRegister> ReleaseDocProc(JournalEntry je, APRegister doc, Boolean isPrebooking, ref List<INRegister> inDocs, ReleaseDocProcDelegate baseMethod)
{
var retVal = baseMethod(je,doc,isPrebooking,ref inDocs);

//Your code goes here.

PXTrace.WriteInformation("My override worked!");

return retVal;
}


#endregion
}

 

Userlevel 4
Badge +1

@Naveen Boga  Yes, Code is invoking without any issue. But there should be some issue in my logic. 

Userlevel 5
Badge +1

Hi @charithalakshan49 

The issue might be due to the use of PXNoteAttribute.AttachFile method. This method doesn't save the changes, it just attaches the file to the NoteID of the record in the cache. But since your code runs in the context of APReleaseProcess, the changes in APInvoice might not get persisted in the database because APReleaseProcess may not commit changes in APInvoiceEntry graph, resulting in the attachment not getting saved as expected.

Instead of using PXNoteAttribute.AttachFile, you can try using the UploadFileMaintenance graph to attach the file, which might resolve your problem. Here is an example code snippet:

 

UploadFileMaintenance graphUpload = PXGraph.CreateInstance<UploadFileMaintenance>();
file.UID = Guid.NewGuid();
graphUpload.SaveFile(file);
PXNoteAttribute.SetFileNotes(Base.Caches[typeof(APInvoice)], apInvoice, new[] { file.UID });

 

This code creates an instance of UploadFileMaintenance, generates a new GUID for the file's UID property, saves the file using the SaveFile method, and then attaches the file to the APInvoice's NoteID by using SetFileNotes method.

Remember to replace the appropriate variables with your own. After using the above code, the attached file should be saved even when you're using the APReleaseProcess graph.

Please try this approach and see if this resolves the issue.

Userlevel 4
Badge +1

Hi @davidnavasardyan09 Thanks for the response. However I tried your suggestion before as well. But no luck. 

Userlevel 7
Badge

Hi @charithalakshan49 were you able to find a solution? Thank you!

Userlevel 4
Badge +1

Hi All, I was unable to do this directly from code . But I managed to do this using following steps.

  1.  Implement an action to attach file in APInvoiceEntry Graph
  2.  Create a Generic Inquiry according to our need
  3.  Create a Business event accordingly to be triggered for each record of generic inquiry
  4.  Create an import scenario to trigger implemented action using the business event

 

Hope this helps!

 

Thanks all.

Userlevel 7
Badge

Thank you for sharing your solution with the community @charithalakshan49!

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved