在Acumatica中创建采购订单时向请求者发送通知 [英] Sending notification to requester when PO is created in Acumatica

查看:77
本文介绍了在Acumatica中创建采购订单时向请求者发送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to be able to send an email to the original requester when a PO is created from a Requisition in Acumatica 6.1.

对于每个Acumatica,通知屏幕无法处理此功能,因此我具有以下代码以扩展POOrder Entry图,该图会在创建PO时(连同RQRequisitionEntryExt触发器一起)从请购单向客户的联系电子邮件发送电子邮件:

Per Acumatica, the Notification screen cannot handle this functionality, so I have this code to extend the POOrder Entry graph, which sends an email to the Customer's contact email from the Requisition when a PO is created (along with the RQRequisitionEntryExt trigger):

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:

And this to modify 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以从请求对象和员工的联系电子邮件中提取信息(我将RQRequisitionEntryExt保留在相同的位置):

In order to send an email to the Requester's (Employee) contact email from the Request, I modified the POOrderEntryExt to pull the information from the Request object and the Employee's Contact email (I left the RQRequisitionEntryExt the same and in place):

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();
        }
    }
}

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

I can get the original code to send an email in my development environment, but my modified code only returns the outer "Failed to send E-mail" error.

任何人都可以帮我指出右边

Can anyone help point me in the right direction to get my modifications to work?

推荐答案

在Acumatica中, RQRequisition RQRequest ,我认为更好的方法是遍历链接到当前请购单的所有请求并撰写请求者的电子邮件列表。之后,我们可以继续进行操作,并作为创建订单操作的一部分向所有请求者发送电子邮件:

Because in Acumatica there is one-to-many relationship between RQRequisition and RQRequest, I believe the better approach is to loop through all requests linked to the current requisition and compose a list of requester's emails. After that we can go ahead and send emails to all requesters as part of the Create Orders operation:

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();
        }
    }
}

这篇关于在Acumatica中创建采购订单时向请求者发送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆