As you may have read from my previous posts, I am moving on from my current employer and away from the world of Infor Syteline, into a position developing a Microsoft NAV environment. Whilst I am working my notice I am trying to standardise a few things we implemented in the early days of development. One particular function we have probably 10 different methods to do on is sending emails, and the organisation is looking to replace the email server (going cloud based) so I want to standardise the way we send emails as much as possible.
Here’s a piece of very poor code implemented many moons ago to send an email warning a duplicate customer order may of been entered into the system as the PO and customer number matches an order already on the system :-
Sub PBTI_EmailDuplicatePo()
‘executed from the event: CustPoWarning_DB
Dim SmtpServer As New SmtpClient(“exchange.wells.pbti”)
Dim mail As New MailMessage()
SmtpServer.Port = 25
mail.IsBodyHtml = True
‘Me.IDOClient.LoadCollection(“UserNames”, “Username = ” & Application.Variables(“User_UserID”).Value, “”, 1)mail.From = New MailAddress(<email1>)
mail.To.Add(New MailAddress(<email2>))mail.Subject = Application.Variables(“User_UserID”).Value & ThisForm.PrimaryIDOCollection.GetCurrentObjectProperty(“TakenBy”) & _
” has entered a duplicate PO: ” & _
ThisForm.PrimaryIDOCollection.GetCurrentObjectProperty(“CustPo”) & ” customer:” & ThisForm.PrimaryIDOCollection.GetCurrentObjectProperty(“CustNum”) & _
” On” & Application.ConfigurationName
mail.Body = Application.Variables(“User_UserID”).Value & ThisForm.PrimaryIDOCollection.GetCurrentObjectProperty(“TakenBy”) & _
” has entered a duplicate PO: ” & _
ThisForm.PrimaryIDOCollection.GetCurrentObjectProperty(“CustPo”) & ” customer:” & ThisForm.PrimaryIDOCollection.GetCurrentObjectProperty(“CustNum”) & _
” On ” & Application.ConfigurationName & _SmtpServer.Send(mail)
End Sub
On changing the email server, the email would simply not send, Using the Event Management system which in turn uses the intranets page for its email configuration means I can use the “GenericSendEmail” event to transmit this data.
The first thing I have done is create a new event handler on the customer orders form as shown here :-
Then I have set up the parameters as shown here :-
The string for the message body is : “Duplicate po! User V(User_UserID) has entered a duplicate po for order P(CoNum) customer P(CustNum). The Customer PO is P(CustPo). It was taken by P(TakenBy)”
When the event handler is hit it now generates an email as shown here :-
And finally, to tie it into the code the sub that was listed above, now looks like this :-
Sub PBTI_EmailDuplicatePo()
ThisForm.GenerateEvent(“PBTI_DuplicatePoEmail”)
End Sub
There is a limitation with the generic event whereas it sends as plain text, not HTML as standard, if this is not good enough in your organisation I would propose making an identical copy of the event with a different name and setting the HTML flag accordingly as shown here :-
This results in an email which is much more readable as shown here :-