Skip to main content
Solved

Create a report and attach it to shipment record

  • November 18, 2023
  • 2 replies
  • 154 views

param2022
Jr Varsity II
Forum|alt.badge.img

I am trying to attach a file one specific report to the shipment record when the shipment is confirmed. Can anyone here help me with the flow on how to do this with a customization.

 

 

Thanks

Best answer by vardan22

To be able to attach report in the any document you need to go through with following steps:

  1. Get the .pdf file byte array for the particular report using the report Parameters and ScreenID
  2. Make sure the used parameters are listed in the Viewer Fields tab of the Schema Builder on the Acumatica Report Designer
  3. Save the byte array as file in the db using UploadFileMaintenance graph
  4. Set as attachment to the corresponding document using SetFileNotes method from PXNoteAttribute

Below the complete example for the Shipments entry:

using PX.Data;
using PX.Objects.SO;
using PX.Reports;//from PX.Reports.dll
using PX.Reports.Controls;//from PX.Reports.dll
using PX.Reports.Data;//from PX.Reports.dll
using PX.SM;
using System.Collections.Generic;
using System.Linq;

namespace AttachReport
{
	public class SOShipmentEntryExt : PXGraphExtension<SOShipmentEntry>
    {
        public static bool IsActive() => true;

		#region InjectDependency

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

		[InjectDependency]
		protected IReportDataBinder ReportDataBinder { get; private set; }
		#endregion

		#region Override
		public delegate void ConfirmShipmentDelegate(SOOrderEntry docgraph, SOShipment shiporder);
		[PXOverride]
		public virtual void ConfirmShipment(SOOrderEntry docgraph, SOShipment shiporder, ConfirmShipmentDelegate shipmentDelegate)
		{
			shipmentDelegate(docgraph, shiporder);
			const string reportID = "SO642000";
			var parameters = new Dictionary<string, string> { };
			parameters.Add(nameof(SOShipment) + "." + nameof(SOShipment.ShipmentNbr), shiporder.ShipmentNbr);
			byte[] byteArray = GetPDFDocumentFromReport(parameters, reportID);
			UploadFileMaintenance upl = PXGraph.CreateInstance<UploadFileMaintenance>();
			FileInfo fileinfo = new FileInfo($"{shiporder.ShipmentNbr}.pdf", null, byteArray);
			if (upl.SaveFile(fileinfo, FileExistsAction.CreateVersion))
			{
				PXNoteAttribute.SetFileNotes(this.Base.Document.Cache, this.Base.Document.Current, fileinfo.UID.Value);
				this.Base.Save.Press();
			}
		}
		#endregion

		#region helper
		public virtual byte[] GetPDFDocumentFromReport(Dictionary<string, string> parameters, string reportID)
		{
			Report _report = ReportLoader.LoadReport(reportID, null);
			ReportLoader.InitReportParameters(_report, parameters, new PXReportSettings());
			ReportNode reportNode = ReportDataBinder.ProcessReportDataBinding(_report);
			//Generation PDF
			byte[] data = PX.Reports.Mail.Message.GenerateReport(reportNode, RenderType.FilterPdf).First();
			return data;
		}
		#endregion
	}
}

 

View original

Connor Morse
Jr Varsity III
Forum|alt.badge.img

Hi @param2022 , just want to confirm a few things….

  1. When the shipment is Confirmed, a report is attached to the shipment record in Acumatica. (not an additional report attachment on the shipment confirmation notification to customer).
  2. Does the attached report run off parameters needed from the provided shipment? 

vardan22
Jr Varsity III
Forum|alt.badge.img+1
  • Jr Varsity III
  • November 20, 2023

To be able to attach report in the any document you need to go through with following steps:

  1. Get the .pdf file byte array for the particular report using the report Parameters and ScreenID
  2. Make sure the used parameters are listed in the Viewer Fields tab of the Schema Builder on the Acumatica Report Designer
  3. Save the byte array as file in the db using UploadFileMaintenance graph
  4. Set as attachment to the corresponding document using SetFileNotes method from PXNoteAttribute

Below the complete example for the Shipments entry:

using PX.Data;
using PX.Objects.SO;
using PX.Reports;//from PX.Reports.dll
using PX.Reports.Controls;//from PX.Reports.dll
using PX.Reports.Data;//from PX.Reports.dll
using PX.SM;
using System.Collections.Generic;
using System.Linq;

namespace AttachReport
{
	public class SOShipmentEntryExt : PXGraphExtension<SOShipmentEntry>
    {
        public static bool IsActive() => true;

		#region InjectDependency

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

		[InjectDependency]
		protected IReportDataBinder ReportDataBinder { get; private set; }
		#endregion

		#region Override
		public delegate void ConfirmShipmentDelegate(SOOrderEntry docgraph, SOShipment shiporder);
		[PXOverride]
		public virtual void ConfirmShipment(SOOrderEntry docgraph, SOShipment shiporder, ConfirmShipmentDelegate shipmentDelegate)
		{
			shipmentDelegate(docgraph, shiporder);
			const string reportID = "SO642000";
			var parameters = new Dictionary<string, string> { };
			parameters.Add(nameof(SOShipment) + "." + nameof(SOShipment.ShipmentNbr), shiporder.ShipmentNbr);
			byte[] byteArray = GetPDFDocumentFromReport(parameters, reportID);
			UploadFileMaintenance upl = PXGraph.CreateInstance<UploadFileMaintenance>();
			FileInfo fileinfo = new FileInfo($"{shiporder.ShipmentNbr}.pdf", null, byteArray);
			if (upl.SaveFile(fileinfo, FileExistsAction.CreateVersion))
			{
				PXNoteAttribute.SetFileNotes(this.Base.Document.Cache, this.Base.Document.Current, fileinfo.UID.Value);
				this.Base.Save.Press();
			}
		}
		#endregion

		#region helper
		public virtual byte[] GetPDFDocumentFromReport(Dictionary<string, string> parameters, string reportID)
		{
			Report _report = ReportLoader.LoadReport(reportID, null);
			ReportLoader.InitReportParameters(_report, parameters, new PXReportSettings());
			ReportNode reportNode = ReportDataBinder.ProcessReportDataBinding(_report);
			//Generation PDF
			byte[] data = PX.Reports.Mail.Message.GenerateReport(reportNode, RenderType.FilterPdf).First();
			return data;
		}
		#endregion
	}
}

 


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings