在Acumatica中创建采购订单时向请求者发送通知

在Acumatica中创建采购订单时向请求者发送通知,acumatica,Acumatica,在Acumatica 6.1中从请购单创建采购订单时,我需要能够向原始请求者发送电子邮件 根据Acumatica,通知屏幕无法处理此功能,因此我有此代码来扩展POOrder条目图,它在创建PO时从请购单向客户的联系电子邮件发送电子邮件(以及RQRequisitionEntryNext触发器): public类POOrderEntryExt:PXGraphExtension { 私有bool sendmailnotification=false; 公共bool发送电子邮件通知 { 收到 { 返回发

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

根据Acumatica,通知屏幕无法处理此功能,因此我有此代码来扩展POOrder条目图,它在创建PO时从请购单向客户的联系电子邮件发送电子邮件(以及RQRequisitionEntryNext触发器):

public类POOrderEntryExt:PXGraphExtension
{
私有bool sendmailnotification=false;
公共bool发送电子邮件通知
{
收到
{
返回发送电子邮件通知;
}
设置
{
sendEmailNotification=值;
}
}
[PXOverride]
公共无效持续(操作del)
{
使用(var ts=new PXTransactionScope())
{
如果(del!=null)
{
del();
}
如果(发送电子邮件通知)
{
bool-sent=false;
string sError=“发送电子邮件失败。”;
尝试
{
通知行通知=PXSelect
.选择(基本,“PurchaseOrderNotification”);
if(rowNotification==null)
抛出新的PXException(“未找到通知模板”);
变量顺序=Base.Document.Current;
var请购单=(RQRequisition)PXSelect
.SelectSingleBound(基本,新对象[]{order});
如果(requisition.CustomerID!=null)
{
var customer=(BAccountR)PXSelectorAttribute.Select(
基本缓存[类型(RQRequisition)],请购单;
如果(客户!=null)
{
var defCustContact=(Contact)PXSelectorAttribute.Select(
基本缓存[类型(BAccountR)],客户);
if(String.IsNullOrEmpty(defCustContact.EMail))
抛出新的PXException(“未为客户联系人指定电子邮件”);
var sender=TemplateNotificationGenerator.Create(订单,
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();
}
}
}
捕获(异常错误)
{
发送=错误;
sError=错误消息;
}
如果(!已发送)
抛出新的PXException(sError);
}
ts.完成();
}
}
}
要修改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);
    }
}
公共类RQRequisitionEntryText:PXgrapherExtension
{
公共行动创造秩序;
[PXButton(ImageKey=PX.Web.UI.Sprite.Main.DataEntry)]
[PXUIField(DisplayName=Messages.CreateOrders)]
公共IEnumerable CreatePOOrder(PXAdapter适配器)
{
PXGraph.InstanceCreated.AddHandler((图形)=>
{
graph.GetExtension().SendEmailNotification=true;
});
返回Base.createPOOrder.Press(适配器);
}
}
为了从请求中向请求者(员工)的联系电子邮件发送电子邮件,我修改了POOrderEntryExt以从请求对象和员工的联系电子邮件中提取信息(我将RQRequisitionEntryExt保留在相同的位置):

public类POOrderEntryExt:PXGraphExtension
{
私有bool sendmailnotification=false;
公共bool发送电子邮件通知
{
收到
{
返回发送电子邮件通知;
}
设置
{
sendEmailNotification=值;
}
}
[PXOverride]
公共无效持续(操作del)
{
使用(var ts=new PXTransactionScope())
{
如果(del!=null)
{
del();
}
如果(发送电子邮件通知)
{
bool-sent=false;
string sError=“发送电子邮件失败。”;
尝试
{
通知行通知=PXSelect
.选择(基本,“PurchaseOrderNotification”);
if(rowNotification==null)
抛出新的PXException(“未找到通知模板”);
变量顺序=Base.Document.Current;
var请购单=(RQRequisition)PXSelect
.SelectSingleBound(基本,新对象[]{order});
var请求=(RQRequest)PXSelectJoin
.SelectSingleBound(基本,新对象[]{order});
if(request.EmployeeID!=null)
{
var employee=(BAccountR)PXSelectorAttribute.Select(
Base.Caches[typeof(RQRequest)],请求);
if(employee!=null)
{
var defEmpContact=(Contact)PXSelectorAttribute.Select(
基本缓存[类型(BAccountR)],员工);
if(String.IsNullOrEmpty(defEmpContact.EMail))
抛出新的PXException(“未为员工联系人指定电子邮件”);
var sender=TemplateNotificationGenerator.Create(o
 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);
    }
}
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();
        }
    }
}
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();
        }
    }
}