1

Basically i want to create a new action button On Print Invoice and Memos screen to print a report for selected invoices.

Why we are creating new action button is, here we need to print different formats for each invoice (SO type) so when user selects 3 different records in grid for an example 1. INV1234 and so type is TS then i need to print xyz report 2. INV9875 and this has not created through SO then i need to print ABC report 3. CRM4567 and SO type is TS (like above 1 option)

so here 1 & 3 should print in one page (Like same how process button is working in default acumatica) 2 option report should print in new tab.

If i get a sample code on to print same report in single page and other one in another tab is fine.

Below is the code

public PXAction<PrintInvoicesFilter> PrintReport;
        [PXUIField(DisplayName = "Print Sales Invoice with Price", MapEnableRights = PXCacheRights.Select, MapViewRights = PXCacheRights.Select)]
        [PXLookupButton]
        public virtual IEnumerable printReport(PXAdapter adapter, [PXString] string reportID)
        {
            PXReportRequiredException ex = null;

            foreach (ARInvoice doc in Base.ARDocumentList.Cache.Cached)
            {
                var parameters = new Dictionary<string, string>();
                if (doc.Selected == true)
                {
ARTran TranData = PXSelectReadonly<ARTran, Where<ARTran.tranType, Equal<Required<ARTran.tranType>>,
                        And<ARTran.refNbr, Equal<Required<ARTran.refNbr>>>>>.Select(Base, doc.DocType, doc.RefNbr);

                    if (TranData != null)
                    {
                        if (TranData.SOOrderType == "WS" || TranData.SOOrderType == "WO" || TranData.SOOrderType == "TS" || TranData.SOOrderType == "IM")
                        {
                            if (reportID == null) reportID = "KR501011";

                            if (reportID == "KR501011")
                            {
                                parameters["DocType"] = doc.DocType;
                                parameters["RefNbr"] = doc.RefNbr;
                            }
                            ex = PXReportRequiredException.CombineReport(ex, "KR501011", parameters,false);
                        }
if (TranData.SOOrderType == "RX")
                        {
                            if (reportID == null) reportID = "KR501016";

                            if (reportID == "KR501016")
                            {
                                parameters["DocType"] = doc.DocType;
                                parameters["RefNbr"] = doc.RefNbr;
                            }
                            ex = PXReportRequiredException.CombineReport(ex, "KR501016", parameters,false);
                        }

                        if (string.IsNullOrEmpty(TranData.SOOrderType))
                        {
                            if (reportID == null) reportID = "KR501038";

                            if (reportID == "KR501038")
                            {
                                parameters["DocType"] = doc.DocType;
                                parameters["RefNbr"] = doc.RefNbr;
                            }
                            ex = PXReportRequiredException.CombineReport(ex, "KR501038", parameters,false);
                        }
                    }
                }
            }
if (ex != null)
            {
                ex.Mode = PXBaseRedirectException.WindowMode.New;
                ex.SeparateWindows = false;
                throw ex;
            }

Thanks in advance.

BhavyaSri
  • 150
  • 10

1 Answers1

0

Redirecting to multiple report or combining multiple report in a single document can only be achieved with method PXReportRequiredException.CombineReport.

The redirection exception has two options to control how the reports are combined:

  1. Print all report as a single PDF file – ex.SeparateWindows = false;

  2. Open each separate report in a new tab – ex.SeparateWindows = true;

Your requirement asked for 1 and 2 at the same time which is not possible. You can only choose option 1 or 2. To get both you would need two actions button to launch the reports.

The reason for the limitation is because to redirect to a report you have to throw an exception. Once the exception is thrown you can't execute code anymore to launch a new report. It is possible to print multiple reports with a single exception as explained below but you have to choose between all reports in same tab (same document) or one report per tab (one document per report).

Blog Source: https://asiablog.acumatica.com/2017/03/launch-multiple-reports-with-one-exception.html

Code example from that blog source:

PXReportRequiredException ex = null;

if(row.ARRefNumber != null)
{
  Dictionary<string, string> dictionary = new Dictionary<string, string>();
  dictionary["DocType"] = row.ARDocType;
  dictionary["RefNbr"] = row.ARRefNumber;
  ex = PXReportRequiredException.CombineReport(ex, row.ARBatchNumber == null ? "AR610500" : "AR622000", dictionary, false);
}

if (row.APRefNumber != null)
{
  APInvoice inv = PXSelectorAttribute.Select<DocHeader.aPRefNumber>(Document.Cache, row) as APInvoice;
  Dictionary<string, string> dictionary = new Dictionary<string, string>();
  dictionary["DocType"] = row.APDocType;
  dictionary["RefNbr"] = row.APRefNumber;
  dictionary["PeriodTo"] = PX.Objects.GL.OpenPeriodAttribute.FormatForDisplay(inv.FinPeriodID);
  dictionary["PeriodFrom"] = PX.Objects.GL.OpenPeriodAttribute.FormatForDisplay(inv.FinPeriodID);
  ex = PXReportRequiredException.CombineReport(ex, row.APBatchNumber == null ? "AP610500" : "AP622000", dictionary, false);
}

if (ex != null)
{
  ex.Mode = PXBaseRedirectException.WindowMode.New;
  ex.SeparateWindows = true;
  throw ex;
}
Hugues Beauséjour
  • 8,067
  • 1
  • 9
  • 22
  • I have tried the same but it is not working for me, please review my code. – BhavyaSri Mar 12 '20 at 19:10
  • Please explain what is not working. Is there any error message on screen or in the traces window. – Hugues Beauséjour Mar 12 '20 at 19:22
  • My issue is, i have selected 4 records in grid and clicked on print report new action in the selected invoices 1 falls under "KR501011" reportID 2&3 falls under "KR501038" reportID and 4 falls under "KR501011" reportID, but the report is printing only 1&4 reportID for the rest report values is printing blank, with other report id the report is not printing, please help me how to print the other 2 invoice in different tab with "KR501038" reportID. – BhavyaSri Mar 13 '20 at 17:42
  • @BhavyaSri did you find a solution? Curious to know if this is solvable. – Cory Apr 14 '20 at 17:36
  • If the report is printing blank it's likely because of the parameter value passed. – Hugues Beauséjour Apr 15 '20 at 18:11
  • No, not the issue with parameter when i want to print multiple reports based on the condition it is just printing only the first satisfied condition even it satisfies other but printing only first report ID the others or not redirecting to the respective report IDs, @HuguesBeauséjour can you please see the above example code and correct me if i am doing anything wrong please. – BhavyaSri Apr 20 '20 at 15:02
  • I still don't understand the issue 'report values is printing blank', what does that mean? It sounds like you passed invalid reports to the parameter, I can't verify that with your code alone. – Hugues Beauséjour Apr 20 '20 at 15:33
  • Hi Hugues, May i know what else you required other than code please so that i can try to share you. – BhavyaSri Apr 27 '20 at 12:16
  • I guess the complete solution along with a very precise explanation of the issue. There's not really any reason to believe the error is in the code snippet so everything need to be debugged. – Hugues Beauséjour May 01 '20 at 14:29