Skip to main content

Email Processing - Distributor Batch


Often we need to send emails during a process in Dynamics 365 for Finance and Operations. That's quite a standard requirement which often makes you look up some examples of your own or web.


Here is yet another blog post on email sending. 


In this blog post I will provide a few examples of sending email. But first let me introduce you some terms.


  • Email template - Templates stored underOrganization administration>Setup>Organization email templates. The underlying tables are SysEmailTable and SysEmailMessageTable.
  • Placeholder - a string enclosed within percent (%) symbols. E.g. %placeholder%.
  • Email processing table - two tables SysOutgoingEmailTable and SysOutgoingEmailData that store emails to be processed by a batch job. Emails can be found under System administration>Periodic tasks>Email processing>Email sending status.

I assume you have all you email parameters in place and its functional.

Let's send some emails! 

The simplest way is to send it directly. Check out the following example:

var builder = new SysMailerMessageBuilder();

builder.addTo("receiver@test.com");
builder.setFrom("sender@test.com");

builder.setSubject("The greatest email of all time");
builder.setBody("This is the best email ever. I believe in code!");
var message = builder.getMessage();

SysMailerFactory::getNonInteractiveMailer().sendNonInteractive(message);

TipaddTo method accepts semicolon separated email addresses.

Let's be a little bit more sophisticated and create an email template.
image

Now let's send an email using the template.


SysEmailTable        sysEmailTable        = SysEmailTable::find('MyTemplate');
SysEmailMessageTable sysEmailMessageTable = SysEmailMessageTable::find(sysEmailTable.EmailId, sysEmailTable.DefaultLanguage);

var builder = new SysMailerMessageBuilder();

builder.addTo("receiver@test.com");
builder.setFrom(sysEmailTable.SenderAddr, sysEmailTable.SenderName);

builder.setSubject(sysEmailMessageTable.Subject);
builder.setBody(sysEmailMessageTable.Mail);
var message = builder.getMessage();

SysMailerFactory::getNonInteractiveMailer().sendNonInteractive(message);

What's the template without placeholders, right?

Let's add a couple of placeholders %UserName% and %DateTime% and resolve them. But first we need to add them to the template.


image


See the code example bellow on how to resolve the placeholders:


SysEmailTable        sysEmailTable        = SysEmailTable::find('MyTemplate');
SysEmailMessageTable sysEmailMessageTable = SysEmailMessageTable::find(sysEmailTable.EmailId, sysEmailTable.DefaultLanguage);

str messageBody = sysEmailMessageTable.Mail;
str subject = sysEmailMessageTable.Subject;

Map placeholderMap = new Map(Types::String, Types::String);

placeholderMap.insert("UserName", "Evaldas Landauskas");
placeholderMap.insert("DateTime", strFmt("%1", DateTimeUtil::getSystemDateTime()));

messageBody = SysEmailMessage::stringExpand(messageBody, placeholderMap);
subject = SysEmailMessage::stringExpand(subject, placeholderMap);

var builder = new SysMailerMessageBuilder();

builder.addTo("receiver@test.com");
builder.setFrom(sysEmailTable.SenderAddr, sysEmailTable.SenderName);

builder.setSubject(subject);
builder.setBody(messageBody);
var message = builder.getMessage();

SysMailerFactory::getNonInteractiveMailer().sendNonInteractive(message);

Note: Keys in the map are not enclosed with %, the code inside stringExpand encloses the keys.

image


OK. Let's try one more thing. Let's say you would like to have a control on emails, like being able to resend in case of some failure.

In this example we will use email processing table instead of directly sending an email. 


SysEmailTable        sysEmailTable        = SysEmailTable::find('MyTemplate');
SysEmailMessageTable sysEmailMessageTable = SysEmailMessageTable::find(sysEmailTable.EmailId, sysEmailTable.DefaultLanguage);

str messageBody = sysEmailMessageTable.Mail;
str subject = sysEmailMessageTable.Subject;

Map placeholderMap = new Map(Types::String, Types::String);

placeholderMap.insert("UserName", "Evaldas Landauskas");
placeholderMap.insert("DateTime", strFmt("%1", DateTimeUtil::getSystemDateTime()));

messageBody = SysEmailMessage::stringExpand(messageBody, placeholderMap);
subject = SysEmailMessage::stringExpand(subject, placeholderMap);

SysOutgoingEmailTable outgoingEmailTable;
SysOutgoingEmailData outgoingEmailData;

outgoingEmailTable.EmailItemId = EventInbox::nextEventId();
outgoingEmailTable.TemplateId = sysEmailTable.EmailId;
outgoingEmailTable.Sender = sysEmailTable.SenderAddr;
outgoingEmailTable.SenderName = sysEmailTable.SenderName;
outgoingEmailTable.Recipient = 'receiver@test.com';
outgoingEmailTable.Subject = subject;
outgoingEmailTable.Message = messageBody;
outgoingEmailTable.Priority = sysEmailTable.Priority;
outgoingEmailTable.WithRetries = true;
outgoingEmailTable.RetryNum = 0;
outgoingEmailTable.UserId = curuserid();
outgoingEmailTable.Status = SysEmailStatus::Unsent;
outgoingEmailTable.LatestStatusChangeDateTime = DateTimeUtil::getSystemDateTime();
outgoingEmailTable.insert();

And the result is this.

image

The emails in this table are processed via batch job
System administration>Periodic tasks>Email processing>Email distributor batch.

Comments

Popular posts from this blog

Edit Method on Form

Edit Method D365 for a form Data Source 1. To create an edit method first create a controller class. with following properties  public static edit MainAccountNum LedgerJournalTransLedger(LedgerJournalTrans _ledgerjournal, boolean _set, MainAccountNum _id) { MainAccountNum accountId = _id; MainAccount mainAccount = MainAccount::findByMainAccountId(_id); if(_set) { if(_ledgerjournal.AccountType== LedgerJournalACType::Ledger) { mainAccount = MainAccount::findByMainAccountId(accountId); if(_ledgerjournal.LedgerDimension) { DimensionDefault defaultDim = LedgerDimensionFacade::getDefaultDimensionFromLedgerDimension(_ledgerjournal.LedgerDimension); _ledgerjournal.LedgerDimension = LedgerDimensionDefaultingEngine::getLedgerDimensionFromAccountAndDim(mainAccount.RecId, DimensionHierarchy::getAccountStructure(mainAccount.RecId), defaultDim); } else { _ledgerjournal.LedgerDimension = LedgerDimensionDefaultingEngine::getLedgerDimensionFromAccountAndDim(mainAccount.RecId, DimensionHierarchy::getAcc

Security Objects In D365

   PRIVILEGES, DUTIES AND ROLES IN D365 FinOps To add customize security privilege, duty and role you should follow this flow because it is considered as the best practices  Role---> Duty---->Privilege Duty and Privilege would be created at the back end and where as role would be created at front end  1. create privilege from solution explorer in a project  and add new entry point for output, display or action menus to refer in privilege that for which entity we have to give privilege to the user 3. Now Create a duty from solution explorer same as privilege and add this new created privilege to the duty  Now you can refer this duty to the role created on the front end.   HEY HEY HEY !!!! HACK OF THE DAY !!           THE HIGHEST ACCESS LEVEL  FOR ACTION MENU ITEM IS   DELETE     

Deep Links

  DEEP LINKS FOR SALES ORDERS In this blog we will discuss about generating deep links for any form , record or datasource. Deep links are basically termed for generating URL's through code for any specific record in D365. Using Deep links other environments can access D365 records by using this URL generated from it. 1. In below blog I am creating Deep links for sales order header record. 2. To Access these links one should always be added the user in FinOps to access through URL. Step #1 Create and extension class of  URLUtility class  and also add following code snippet to access this class : using Microsoft.Dynamics.AX.Framework.Utilities; using Microsoft.Dynamics.@Client.ServerForm.Contexts; public static str generateRecordUrl(str _menuItemName, MenuItemType _menuItemType, DataSourceName _dataSourceName, Map _indexFieldValuesMap, DataAreaId _dataAreaId = curExt()) {   System.Uri host = SessionContext::Get_Current().Get_RequestUrl();   UrlHelper.UrlGenerator generator = new Url