Skip to main content
Solved

What is the best practice to send a custom report to DeviceHub and update a field in to show that it has been printed?

  • January 16, 2023
  • 3 replies
  • 485 views

lauraj46
Captain II
Forum|alt.badge.img+8

Developer gurus,

What is the best practice to send a custom report to DeviceHub and update a field to show that it has been printed?

We have a custom Workflow Action that prints a custom report (labels) from the Production Order Maintenance screen.  The action opens the report on the screen and uses the “Field Updates” tab to update the IsReportPrinted field.

Now instead of printing to the screen, we would like to print this report directly using DeviceHub.  Is there any way in the Low Code/NoCode framework to do this?

I think we have some working code based on a very helpful thread and the response by @Gabriel Michaud, but I’m just wondering is there any way to accomplish this using the new workflow engine?  We hope to build in such a way that this will upgrade easily as Acumatica makes advancements to the workflow engine.

Code is based on the example attached to this thread:

 

using System;
using PX.Data;
using PX.Objects.IN;
using PX.Objects.CS;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using PX.Objects.AM.GraphExtensions;
using PX.Common;
using PX.Objects.Common;
using PX.Objects.SO;
using PX.Objects.AM.Attributes;
using System.Linq;
using PX.Objects.AM.CacheExtensions;
using PX.Objects.PO;
using PX.Data.BQL.Fluent;
using PX.Data.BQL;
using PX.Objects.AR;
using PX.Objects.CR;
using PX.Data.WorkflowAPI;
using PX.Objects;
using PX.Objects.AM;
using PX.SM;

namespace PX.Objects.AM
{
  public class ProdMaint_Extension : PXGraphExtension<PX.Objects.AM.ProdMaint>
  {
    #region Event Handlers
        public PXSelect<AMProdItem, Where<AMProdItem.orderType, Equal<Optional<AMProdItem.orderType>>>> ProdMaintRecords;

        public PXSave<PX.Objects.AM.AMProdItem> Save;

        public PXAction<PX.Objects.AM.AMProdItem> Printer;

        [PXButton(CommitChanges = true)]
        [PXUIField(DisplayName = "Print Label to Device Hub")]
        protected void printer()
        {
            SMPrinter printer = PXSelect<SMPrinter,
Where<SMPrinter.printerName, Equal<Required<SMPrinter.printerName>>>>.Select(Base, "BROTHER");

            AMProdItem doc = ProdMaintRecords.Current;
            Dictionary<string, string> parametersDictionary = new Dictionary<string, string>();
            string actualReportID = "AM625099";

            parametersDictionary["ProdOrdID"] = doc.ProdOrdID;
            parametersDictionary["OrderType"] = doc.OrderType;

            PrintSettings printSettings = new PrintSettings()
            {
                PrinterID = printer.PrinterID,
                NumberOfCopies = 1,
                PrintWithDeviceHub = true,
                DefinePrinterManually = false
            };


            PXGraph.CreateInstance<SMPrintJobMaint>().CreatePrintJob(printSettings, actualReportID, parametersDictionary, "label report");

            doc.IsReportPrinted = true;
            ProdMaintRecords.Update(doc);
            Save.Press();

        }
        #endregion

  }
}

Laura

Best answer by Yuriy Zaletskyy

The best practice to send a custom report to DeviceHub and update a field to show that it has been printed in Acumatica ERP would be to use the Acumatica workflow engine to automate the process.

Here is an example of how you can do this:

  1. Create a new custom workflow step that is triggered when the Production Order Maintenance screen is opened.
  2. In the custom workflow step, check the value of the "IsReportPrinted" field. If it is set to false, then send the report to DeviceHub using the SMPrintJobMaint graph and the PrintSettings class.
  3. Update the "IsReportPrinted" field to true to indicate that the report has been printed.
  4. Update the Production Order record with the new value of the "IsReportPrinted" field.

This way, the report will be automatically sent to DeviceHub and the "IsReportPrinted" field will be updated every time the Production Order Maintenance screen is opened, and the report hasn't been printed yet.

Here is the C# code for custom workflow step:

using PX.Data.WorkflowAPI;
using PX.Objects;
using PX.Objects.AM;
using PX.SM;
using System.Collections.Generic;

namespace PX.Objects.AM
{
    public class ProdMaint_Workflow : PXGraph<ProdMaint_Workflow>
    {
        public PXCancel<AMProdItem> Cancel;
        public PXAction<AMProdItem> print;
        public PXSelect<AMProdItem> ProdMaintRecords;

        [PXUIField(DisplayName = "Print Label to Device Hub")]
        [PXButton]
        public virtual IEnumerable Print(PXAdapter adapter)
        {
            AMProdItem doc = ProdMaintRecords.Current;

            if (!doc.IsReportPrinted)
            {
                // Send report to Device Hub
                SMPrinter printer = PXSelect<SMPrinter,
                Where<SMPrinter.printerName, Equal<Required<SMPrinter.printerName>>>>.Select(this, "BROTHER");

                Dictionary<string, string> parametersDictionary = new Dictionary<string, string>();
                string actualReportID = "AM625099";

                parametersDictionary["ProdOrdID"] = doc.ProdOrdID;
                parametersDictionary["OrderType"] = doc.OrderType;

                PrintSettings printSettings = new PrintSettings()
                {
                    PrinterID = printer.PrinterID,
                    NumberOfCopies = 1,
                    PrintWithDeviceHub = true,
                    DefinePrinterManually = false
                };

                PXGraph.CreateInstance<SMPrintJobMaint>().CreatePrintJob(printSettings, actualReportID, parametersDictionary, "label report");

                // Update the IsReportPrinted field
                doc.IsReportPrinted = true;
                ProdMaintRecords.Update(doc);
            }

            return adapter.Get();
        }
    }
}

that would by my suggestion, also some specific business needs I’ve ommited.

View original
Did this topic help you find an answer to your question?

3 replies

Yuriy Zaletskyy
Jr Varsity I
Forum|alt.badge.img+3

The best practice to send a custom report to DeviceHub and update a field to show that it has been printed in Acumatica ERP would be to use the Acumatica workflow engine to automate the process.

Here is an example of how you can do this:

  1. Create a new custom workflow step that is triggered when the Production Order Maintenance screen is opened.
  2. In the custom workflow step, check the value of the "IsReportPrinted" field. If it is set to false, then send the report to DeviceHub using the SMPrintJobMaint graph and the PrintSettings class.
  3. Update the "IsReportPrinted" field to true to indicate that the report has been printed.
  4. Update the Production Order record with the new value of the "IsReportPrinted" field.

This way, the report will be automatically sent to DeviceHub and the "IsReportPrinted" field will be updated every time the Production Order Maintenance screen is opened, and the report hasn't been printed yet.

Here is the C# code for custom workflow step:

using PX.Data.WorkflowAPI;
using PX.Objects;
using PX.Objects.AM;
using PX.SM;
using System.Collections.Generic;

namespace PX.Objects.AM
{
    public class ProdMaint_Workflow : PXGraph<ProdMaint_Workflow>
    {
        public PXCancel<AMProdItem> Cancel;
        public PXAction<AMProdItem> print;
        public PXSelect<AMProdItem> ProdMaintRecords;

        [PXUIField(DisplayName = "Print Label to Device Hub")]
        [PXButton]
        public virtual IEnumerable Print(PXAdapter adapter)
        {
            AMProdItem doc = ProdMaintRecords.Current;

            if (!doc.IsReportPrinted)
            {
                // Send report to Device Hub
                SMPrinter printer = PXSelect<SMPrinter,
                Where<SMPrinter.printerName, Equal<Required<SMPrinter.printerName>>>>.Select(this, "BROTHER");

                Dictionary<string, string> parametersDictionary = new Dictionary<string, string>();
                string actualReportID = "AM625099";

                parametersDictionary["ProdOrdID"] = doc.ProdOrdID;
                parametersDictionary["OrderType"] = doc.OrderType;

                PrintSettings printSettings = new PrintSettings()
                {
                    PrinterID = printer.PrinterID,
                    NumberOfCopies = 1,
                    PrintWithDeviceHub = true,
                    DefinePrinterManually = false
                };

                PXGraph.CreateInstance<SMPrintJobMaint>().CreatePrintJob(printSettings, actualReportID, parametersDictionary, "label report");

                // Update the IsReportPrinted field
                doc.IsReportPrinted = true;
                ProdMaintRecords.Update(doc);
            }

            return adapter.Get();
        }
    }
}

that would by my suggestion, also some specific business needs I’ve ommited.


Yuriy Zaletskyy
Jr Varsity I
Forum|alt.badge.img+3

The code you provided is a good starting point, but the best practice would be to move the code that sends the report to DeviceHub and updates the "IsReportPrinted" field to a workflow step, rather than an event handler in the custom graph extension.


lauraj46
Captain II
Forum|alt.badge.img+8
  • Author
  • Captain II
  • 591 replies
  • January 22, 2023

Hi @Yuriy Zaletskyy ,

Thank you so much for sharing this advice and sample code!   


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