Friday, 18 August 2017

Adding new Report Button on Invoices and Memos screen

Hi All,

Today want to share with you the case how to add a new button to print alternative Invoice Form right from Invoices and Memos screen.
Lets check how does that work and check how original button "Print Invoice/Memo" is configured.

Automation Steps
Fist of all if you check the code, there is no handler for "Print Invoice/Memo". That is some sort of virtual button defined through Acumatica Automation Workflow.
If you check Automation Steps for "Invoices and Memos" screen you can find that menu "Print Invoice/Memo" is added to action "Report".  That is exactly action that will be executed in the code.

If you click "Fill With Values" button you also can see that this action passing some parameters to code.
Fields with normal name (like "Printed") are real DAC fields. Acumatica will put there your value when you click on button.
Fields those started with "@" is not a read field, they are parameter of functions in the code.

Code Handler
If you check the code of ARDataEntryGraph (that is a parent ARInvoiceEntry and ARCashSaleEntry) than you can find that action handler:
You can see hare that this method has the same name as Action in the Automation Steps and it has parameter - reportID, that was provided from "Fill with Values" dialog box.

Depend on reportID provided that action just fills report parameter from the code. And than just throws the proper PXReportRequiredException:

That's all, now we know how does it work.

So accordingly to our investigation, if you need to add new button there we need to do 2 things:
  • Modify a button handler to handle new report ID
  • Amend Automation Steps to register a new Action Menu.
Lets start with the code - here is an example of extension how can you add new report ID to the code:
public class ARInvoiceEntry_Extension : PXGraphExtension<ARInvoiceEntry>
       public delegate IEnumerable ReportDelegate(PXAdapter adapter, String reportID);
       public IEnumerable Report(PXAdapter adapter, String reportID,
ReportDelegate baseMethod)
              IEnumerable records = baseMethod(adapter, reportID);

              //if we are here that means that report is not identified by base method
              PXReportRequiredException ex = null;
              foreach (ARInvoice doc in records)
                      var parameters = new Dictionary<string, string>();
                      if (reportID == "AR641001")
                             parameters["ARInvoice.DocType"] = doc.DocType;
                             parameters["ARInvoice.RefNbr"] = doc.RefNbr;

                             ex = PXReportRequiredException.CombineReport(ex,
reportID, parameters);
              if (ex != null) throw ex;

              return records;

As soon as it is published, you can use Automation Steps to configure new Action:

Please note, that Action will be enabled only on steps where it is defined. That means you may need to add this new action to every single step in Automation (ex Balanced, Hold, Released, etc).

Now you can save everything and test the result.

Have a nice customization!

No comments: