3

We have created some customized reports and these will open up based on Sales Order type in details tab. Same way we want to send report based on order type when we use Email Invoice/Memo action from Actions menu.

We tried to override the code but still we are seeing default report is sent in email. How can I fix this? My code is below:

        [PXOverride]
        public PXAction<ARInvoice> sendARInvoiceMemo;
        [PXUIField(DisplayName = "Send AR Invoice/Memo", MapEnableRights = PXCacheRights.Select, MapViewRights = PXCacheRights.Select)]
        [PXLookupButton]
        public IEnumerable SendARInvoiceMemo(PXAdapter adapter, String reportID)
        {
            PXReportRequiredException ex = null;

            foreach (ARInvoice doc in adapter.Get<ARInvoice>())
            {
                var parameters = new Dictionary<string, string>();

                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";
                        Dictionary<string, string> mailParams = new Dictionary<string, string>();
                        if (reportID == "KR501011")
                        {
                            mailParams["DocType"] = doc.DocType;
                            mailParams["RefNbr"] = doc.RefNbr;
                            if (!ReportNotificationGenerator.Send(reportID, mailParams).Any())
                            {
                                throw new PXException(ErrorMessages.MailSendFailed);
                            }
                        }
                        Base.Clear();
                        Base.Document.Current = Base.Document.Search<ARInvoice.refNbr>(doc.RefNbr, doc.DocType);
                    }

                    if (TranData.SOOrderType == "RS" || TranData.SOOrderType == "RO" || TranData.SOOrderType == "PS" || TranData.SOOrderType == "QT")
                    {
                        if (reportID == null) reportID = "KR501012";
                        Dictionary<string, string> mailParams = new Dictionary<string, string>();
                        if (reportID == "KR501012")
                        {
                            mailParams["DocType"] = doc.DocType;
                            mailParams["RefNbr"] = doc.RefNbr;
                            if (!ReportNotificationGenerator.Send(reportID, mailParams).Any())
                            {
                                throw new PXException(ErrorMessages.MailSendFailed);
                            }
                        }
                        Base.Clear();
                        Base.Document.Current = Base.Document.Search<ARInvoice.refNbr>(doc.RefNbr, doc.DocType);
                    }
                }
            }           

            if (ex != null) throw ex;

            return adapter.Get();
        }
halfer
  • 19,824
  • 17
  • 99
  • 186
John
  • 763
  • 3
  • 11
  • It looks like you're only assigning a custom reportID if the reportID is null. Are you sure the reportID is null at that point? – Deetz Dec 02 '20 at 12:20

1 Answers1

0

Note, no version was specified so my work was done against 2020r2.

SendARInvoiceMemo isn't called by the "Email Invoice/Memo" action. Rather, Notification is the delegate for that action.

Note that notification isn't actually calling a report ID, rather it relies on notification CD and your mailing settings to determine what report to run. I modified the code to change to a new notificationCD I created called INVOICEALT. This is configured to my alternative Report ID.

    [PXOverride()]
    [PXUIField(DisplayName = "Notifications", Visible = false)]
    [PXButton(ImageKey = PX.Web.UI.Sprite.Main.DataEntryF)]
    public virtual IEnumerable Notification(PXAdapter adapter,
    [PXString]
    string notificationCD)
    {
        foreach (ARInvoice doc in adapter.Get().RowCast<ARInvoice>())
        {
            Base.Document.Current = doc;

            Dictionary<string, string> parameters = new Dictionary<string, string>
            {
                ["DocType"] = doc.DocType,
                ["RefNbr"] = doc.RefNbr
            };

            using (var ts = new PXTransactionScope())
            {
                if (ProjectDefaultAttribute.IsProject(Base, doc.ProjectID) && Base.Activity.IsProjectSourceActive(doc.ProjectID, notificationCD))
                {
                    Base.Activity.SendNotification(PMNotificationSource.Project, notificationCD, doc.BranchID, parameters);
                }
                else
                {
                    //Base.Activity.SendNotification(ARNotificationSource.Customer, notificationCD, doc.BranchID, parameters); //This is what was there
                    //Inserted switch based on Sales Order Type >>

                    ARTran TranData = PXSelectReadonly<ARTran, Where<ARTran.tranType, Equal<Required<ARTran.tranType>>,
                        And<ARTran.refNbr, Equal<Required<ARTran.refNbr>>>>>.Select(Base, doc.DocType, doc.RefNbr);
                    switch (TranData.SOOrderType)
                    {
                        case "IN":
                            Base.Activity.SendNotification(ARNotificationSource.Customer, "INVOICEALT", doc.BranchID, parameters);
                            break;
                        default:
                            Base.Activity.SendNotification(ARNotificationSource.Customer, notificationCD, doc.BranchID, parameters);
                            break;
                    }
                    //<< Inserted switch based on Sales Order Type
                }
                Base.Save.Press();

                ts.Complete();
            }

            yield return doc;
        }
    }

As an alternative to the above, if you're just talking about users running the action, you could hide the Email Invoice/Memo action and put a new action in place.

    public PXAction<ARInvoice> sendARInvoiceMemoAlt;
    [PXUIField(DisplayName = "Alt Email Invoice/Memo", MapEnableRights = PXCacheRights.Select, MapViewRights = PXCacheRights.Select)]
    [PXLookupButton]
    public IEnumerable SendARInvoiceMemoAlt(PXAdapter adapter, String reportID)
    {
        PXReportRequiredException ex = null;

        foreach (ARInvoice doc in adapter.Get<ARInvoice>())
        {
            var parameters = new Dictionary<string, string>();

            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 == "SO")// || TranData.SOOrderType == "WS" || TranData.SOOrderType == "WO" || TranData.SOOrderType == "TS" || TranData.SOOrderType == "IM")
                {
                    if (reportID == null) reportID = "AR641000";
                    Dictionary<string, string> mailParams = new Dictionary<string, string>();
                    if (reportID == "AR641000")
                    {
                        mailParams["DocType"] = doc.DocType;
                        mailParams["RefNbr"] = doc.RefNbr;
                        if (!ReportNotificationGenerator.Send(reportID, mailParams).Any())
                        {
                            throw new PXException(ErrorMessages.MailSendFailed);
                        }
                    }
                    Base.Clear();
                    Base.Document.Current = Base.Document.Search<ARInvoice.refNbr>(doc.RefNbr, doc.DocType);
                }

                if (TranData.SOOrderType == "IN")// || TranData.SOOrderType == "RS" || TranData.SOOrderType == "RO" || TranData.SOOrderType == "PS" || TranData.SOOrderType == "QT")
                {
                    if (reportID == null) reportID = "AR641001";
                    Dictionary<string, string> mailParams = new Dictionary<string, string>();
                    if (reportID == "AR641001")
                    {
                        mailParams["DocType"] = doc.DocType;
                        mailParams["RefNbr"] = doc.RefNbr;
                        if (!ReportNotificationGenerator.Send(reportID, mailParams).Any())
                        {
                            throw new PXException(ErrorMessages.MailSendFailed);
                        }
                    }
                    Base.Clear();
                    Base.Document.Current = Base.Document.Search<ARInvoice.refNbr>(doc.RefNbr, doc.DocType);
                }
            }
        }

        if (ex != null) throw ex;

        return adapter.Get();
    }

    public override void Initialize()
    {
        base.Initialize();

        Base.ActionsMenuItem.AddMenuAction(sendARInvoiceMemoAlt);
    }
yc9nyw08vf
  • 183
  • 11