Hi There,
Today want to share with you how to send emails from Acumatica custom code based on Notification Templates.
User scenario is very simple – lets assume we want to send an email from Acumatica that informs about contract, quote expiration or something else. But in the same time we want to keep activity linked to our document for the future reference.
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.
Notification templates are special type of emails, that can be combined dynamically with data from Acumatica. On screenshot above you can see grayed out text, that represents a name of the data view and field in Acumatica.
((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:
Note: Because of names of the views and fields each notification template is linked with Acumatica screen. so be careful when you using notification from code – only specially designed templates (which are linked with exact screen) can work with entity you going to send.
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, Where<SOBillingContact.contactID, Equal<Required<SOBillingContact.contactID>>>> .Select(graph, order.BillContactID); Notification notification = PXSelect<Notification, Where<Notification.notificationID, Equal<Required<Notification.notificationID>>>> .Select(graph, notificationTemplateID); bool sent = false; string sError = "Failed to send E-mail."; try { TemplateNotificationGenerator sender = TemplateNotificationGenerator.Create(order, notification.NotificationID.Value); sender.MailAccountId = (notification.NFrom.HasValue) ? notification.NFrom.Value : PX.Data.EP.MailAccountManager.DefaultMailAccountID; sender.RefNoteID = order.NoteID; sender.Owner = order.OwnerID; sender.To = contact.Email; sent |= sender.Send().Any(); } catch (Exception ex) { sent = false; sError = ex.Message; } if (!sent) { 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.
Profit!
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 😉
Hello Sergey,
This is exactly what we need in our environment! Notifications for expiring quotes. I am new to this dev stuff and I am trying to get this to work in our sandbox. Where do I enter this code? I know how to get to the customization screens, but I’m not sure where to enter it.
Thank you for helping a wanna be developer. lol
Hi Nathan,
If you need a notifications for the expired quotes, i think it will be best for you to user business events.
https://www.acumatica.com/blog/technical-tuesday-use-business-events-acumatica-automate-processes/
Thank you sir
Hi Andrey B.
As far as I know (may be here I need to read to dev team) Notification templates does not allow to use code snippets and for-each loops.
Workaround for you will be to modify template/email on the fly or send multiple notifications for every approver.
Hope it helps.
Hello Sergey,
I'm trying to implement approval reminders for employees using approach you suggested.
With your code I am now able to send email from the custom action on Employee, but I faced another problem. I hope you can help to unblock me here..
To compile a list of approvals I added view into class EmployeeMaint_Extension: PXGraphExtension
public PXSelectReadonly<EPApproval,Where<EPApproval.ownerID, Equal<Current<EPEmployee.userID>>,
And<EPApproval.status, Equal<EPApprovalStatus.pending>>>> CurrentApprovals;
I was hoping to to create Notification template that will use construction to list all linked pending approvals, see below:
<foreach view="CurrentApprovals">
((CurrentApprovals.RefNoteID)) ((CurrentApprovals.BAccountID))
</foreach>
//and so on..
.. but apparently system doesn't print anything.
Can you help understand why?
Best regards,
Andrey B.
Acronis
This comment has been removed by the author.