在Acumatica中创建PO时向请求者发送通知

问题描述 投票:0回答:1

当从Acumatica 6.1中的申请创建采购订单时,我需要能够向原始请求者发送电子邮件。

根据Acumatica,通知屏幕无法处理此功能,因此我使用此代码扩展POOrder条目图表,该图表在创建采购订单时(以及RQRequisitionEntryExt触发器)从请购单发送电子邮件至客户的联系电子邮件:

public class POOrderEntryExt : PXGraphExtension<POOrderEntry>
{
    private bool sendEmailNotification = false;

    public bool SendEmailNotification
    {
        get
        {
            return sendEmailNotification;
        }
        set
        {
            sendEmailNotification = value;
        }
    }

    [PXOverride]
    public void Persist(Action del)
    {
        using (var ts = new PXTransactionScope())
        {
            if (del != null)
            {
                del();
            }

            if (SendEmailNotification)
            {
                bool sent = false;
                string sError = "Failed to send E-mail.";

                try
                {
                    Notification rowNotification = PXSelect<Notification,
                        Where<Notification.name, Equal<Required<Notification.name>>>>
                        .Select(Base, "PurchaseOrderNotification");

                    if (rowNotification == null)
                        throw new PXException("Notification Template was not found.");

                    var order = Base.Document.Current;
                    var requisition = (RQRequisition)PXSelect<RQRequisition,
                        Where<RQRequisition.reqNbr, Equal<Current<POOrder.rQReqNbr>>>>
                        .SelectSingleBound(Base, new object[] { order });


                    if (requisition.CustomerID != null)
                    {
                        var customer = (BAccountR)PXSelectorAttribute.Select<RQRequisition.customerID>(
                            Base.Caches[typeof(RQRequisition)], requisition);
                        if (customer != null)
                        {
                            var defCustContact = (Contact)PXSelectorAttribute.Select<BAccountR.defContactID>(
                                Base.Caches[typeof(BAccountR)], customer);

                            if (String.IsNullOrEmpty(defCustContact.EMail))
                                throw new PXException("E-mail is not specified for Customer Contact.");

                            var sender = TemplateNotificationGenerator.Create(order,
                                rowNotification.NotificationID.Value);
                            sender.RefNoteID = order.NoteID;
                            sender.MailAccountId = rowNotification.NFrom.HasValue ?
                                                   rowNotification.NFrom.Value :
                                                   PX.Data.EP.MailAccountManager.DefaultMailAccountID;
                            sender.To = defCustContact.EMail;
                            sent |= sender.Send().Any();
                        }
                    }

                }
                catch (Exception Err)
                {
                    sent = false;
                    sError = Err.Message;
                }

                if (!sent)
                    throw new PXException(sError);
            }
            ts.Complete();
        }
    }
}

这要修改RQRequisitionEntry:

 public class RQRequisitionEntryExt : PXGraphExtension<RQRequisitionEntry>
{
    public PXAction<RQRequisition> createPOOrder;
    [PXButton(ImageKey = PX.Web.UI.Sprite.Main.DataEntry)]
    [PXUIField(DisplayName = Messages.CreateOrders)]
    public IEnumerable CreatePOOrder(PXAdapter adapter)
    {
        PXGraph.InstanceCreated.AddHandler<POOrderEntry>((graph) =>
        {
            graph.GetExtension<POOrderEntryExt>().SendEmailNotification = true;
        });

        return Base.createPOOrder.Press(adapter);
    }
}

为了从请求中向请求者(员工)联系电子邮件发送电子邮件,我修改了POOrderEntryExt以从Request对象和Employee的Contact电子邮件中提取信息(我将RQRequisitionEntryExt保留为相同且就地):

public class POOrderEntryExt : PXGraphExtension<POOrderEntry>
{
    private bool sendEmailNotification = false;

    public bool SendEmailNotification
    {
        get
        {
            return sendEmailNotification;
        }
        set
        {
            sendEmailNotification = value;
        }
    }

    [PXOverride]
    public void Persist(Action del)
    {
        using (var ts = new PXTransactionScope())
        {
            if (del != null)
            {
                del();
            }

            if (SendEmailNotification)
            {
                bool sent = false;
                string sError = "Failed to send E-mail.";

                try
                {
                    Notification rowNotification = PXSelect<Notification,
                        Where<Notification.name, Equal<Required<Notification.name>>>>
                        .Select(Base, "PurchaseOrderNotification");

                    if (rowNotification == null)
                        throw new PXException("Notification Template was not found.");

                    var order = Base.Document.Current;
                    var requisition = (RQRequisition)PXSelect<RQRequisition,
                        Where<RQRequisition.reqNbr, Equal<Current<POOrder.rQReqNbr>>>>
                        .SelectSingleBound(Base, new object[] { order });

                    var request = (RQRequest)PXSelectJoin<RQRequest,
                        InnerJoin<RQRequisitionContent,
                          On<RQRequisitionContent.orderNbr, Equal<RQRequest.orderNbr>>>,
                        Where<RQRequisitionContent.reqNbr, Equal<POOrder.rQReqNbr>>>
                        .SelectSingleBound(Base, new object[] { order });

                    if (request.EmployeeID != null)
                    {
                        var employee = (BAccountR)PXSelectorAttribute.Select<RQRequest.employeeID>(
                              Base.Caches[typeof(RQRequest)], request);
                        if (employee != null)
                        {
                             var defEmpContact = (Contact)PXSelectorAttribute.Select<BAccountR.defContactID>(
                                 Base.Caches[typeof(BAccountR)], employee);

                            if (String.IsNullOrEmpty(defEmpContact.EMail))
                                throw new PXException("E-mail is not specified for Employee Contact.");

                            var sender = TemplateNotificationGenerator.Create(order,
                                rowNotification.NotificationID.Value);
                            sender.RefNoteID = order.NoteID;
                            sender.MailAccountId = rowNotification.NFrom.HasValue ?
                                                   rowNotification.NFrom.Value :
                                                   PX.Data.EP.MailAccountManager.DefaultMailAccountID;
                            sender.To = defEmpContact.EMail;
                            sent |= sender.Send().Any();
                        }

                        else
                            throw new PXException("Customer not found.");
                    }

                    else
                        throw new PXException("Request not found.");
                }
                catch (Exception Err)
                {
                    sent = false;
                    sError = Err.Message;
                }

                if (!sent)
                    throw new PXException(sError);
            }
            ts.Complete();
        }
    }
}

我可以在我的开发环境中获取原始代码以发送电子邮件,但我修改的代码仅返回外部“无法发送电子邮件”错误。

任何人都可以帮我指出正确的方向让我的修改工作吗?

acumatica
1个回答
1
投票

因为在Acumatica中RQRequisition和RQRequest之间存在一对多的关系,我认为更好的方法是遍历链接到当前请求的所有请求并组成请求者的电子邮件列表。之后,我们可以继续向所有请求者发送电子邮件,作为创建订单操作的一部分:

public class POOrderEntryExt : PXGraphExtension<POOrderEntry>
{
    private bool sendEmailNotification = false;

    public bool SendEmailNotification
    {
        get
        {
            return sendEmailNotification;
        }
        set
        {
            sendEmailNotification = value;
        }
    }

    [PXOverride]
    public void Persist(Action del)
    {
        using (var ts = new PXTransactionScope())
        {
            if (del != null)
            {
                del();
            }

            if (SendEmailNotification)
            {
                bool sent = false;
                string sError = "Failed to send E-mail.";

                try
                {
                    Notification rowNotification = PXSelect<Notification,
                        Where<Notification.name, Equal<Required<Notification.name>>>>
                        .Select(Base, "PONotification");

                    if (rowNotification == null)
                        throw new PXException("Notification Template was not found.");

                    var order = Base.Document.Current;
                    var emails = new List<string>();
                    var requests = PXSelectJoinGroupBy<RQRequest,
                        InnerJoin<RQRequisitionContent,
                            On<RQRequest.orderNbr, 
                                Equal<RQRequisitionContent.orderNbr>>>,
                        Where<RQRequisitionContent.reqNbr, 
                            Equal<Required<RQRequisition.reqNbr>>>,
                        Aggregate<GroupBy<RQRequest.orderNbr>>>
                        .Select(Base, order.RQReqNbr);

                    foreach (RQRequest request in requests)
                    {
                        if (request.EmployeeID != null)
                        {
                            var requestCache = Base.Caches[typeof(RQRequest)];
                            requestCache.Current = request;
                            var emplOrCust = (BAccountR)PXSelectorAttribute
                                .Select<RQRequest.employeeID>(requestCache, request);
                            if (emplOrCust != null)
                            {
                                var defEmpContact = (Contact)PXSelectorAttribute
                                    .Select<BAccountR.defContactID>(
                                        Base.Caches[typeof(BAccountR)], emplOrCust);
                                if (!String.IsNullOrEmpty(defEmpContact.EMail) && 
                                    !emails.Contains(defEmpContact.EMail))
                                {
                                    emails.Add(defEmpContact.EMail);
                                }
                            }
                        }
                    }

                    foreach (string email in emails)
                    {
                        var sender = TemplateNotificationGenerator.Create(order, 
                            rowNotification.NotificationID.Value);
                        sender.RefNoteID = order.NoteID;
                        sender.MailAccountId = rowNotification.NFrom.HasValue ?
                            rowNotification.NFrom.Value :
                            PX.Data.EP.MailAccountManager.DefaultMailAccountID;
                        sender.To = email;
                        sent |= sender.Send().Any();
                    }
                }
                catch (Exception Err)
                {
                    sent = false;
                    sError = Err.Message;
                }

                if (!sent)
                    throw new PXException(sError);
            }
            ts.Complete();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.