Sending notification to requester when PO is creat

2019-08-27 00:50发布

问题:

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.

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

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

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?

回答1:

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