Today want to share with you how to send emails from Acumatica custom code based on Notification Templates.
Basically, if we do that manually we need to create new Email activity, fill all details there, attach to entity and send it than,
The obvious way to automate it is usage of Notification Templates.
((Document.OrderNbr)) - Document here is data view name, OrderNbr is field name in the main DAC of the data view. Hopefully you do not need to know the name if view, as you can easily select these fields from special dialog:
As soon as we have notification template we can link it with any possible setup screen to use from the code.
Good, now we can actually use it from code. To send notification we are going to use TemplateNotificationGenerator class that actually will do all the dirty work for us. And we just need to provide entity and notification template to it
public static void AddEmailActivity(SOOrder order, Int32 notificationTemplateID)
SOOrderEntry graph = PXGraph.CreateInstance<SOOrderEntry>();
SOBillingContact contact = PXSelect<SOBillingContact,
Notification notification = PXSelect<Notification, Where<Notification.notificationID,
bool sent = false;
string sError = "Failed to send E-mail.";
TemplateNotificationGenerator sender = TemplateNotificationGenerator
sender.MailAccountId = (notification.NFrom.HasValue)
sender.RefNoteID = order.NoteID;
sender.Owner = order.OwnerID;
sender.To = contact.Email;
sent |= sender.Send().Any();
catch (Exception ex)
sent = false;
sError = ex.Message;
throw new PXException(sError);
Please note that generated Email Activity is linked to exact entity (in my case it is sales order) by Note ID reference. Note ID here is a unique identifier of any record in DB, you can read more about it here.
As the result code above, you will see email activity attached to your order.
Also note that email is create, but might not be sent automatically. It depends on how your email processing is configured.
Have a nice notifications ;)