Build HTML and send email in D365 FO with X++
1. Generate HTML
Note: If you want to add an attribute to a tag, you must follow the below rule:
- Use addAttribute before
- Next use renderBeginTag
Exp:
htmlTxtWriter.addAttribute("colspan", "9");
htmlTxtWriter.renderBeginTag('th');
htmlTxtWriter.renderEndTag();
Result: <th colspan = 9>
public void runNotificationExPicking()
{
Query q;
QueryRun qRun;
QueryBuildDataSource qbds,qbdsProdJournalBOM;
QueryBuildRange
qbr,qbrJournalName,qbrJournalJournal;
InventTable inventTable;
ProdTable prodTable;
ProdJournalBOM prodJournalBOM;
ProdJournalTable prodJournalTable, prodJournalTableGroup, prodJournalTableNotifyLocal;
JournalId journalId;
TransDate currentDate;
int i
utcDateTime now;
ABC_EmailConfig emailConfig;
HtmlTextWriter htmlTxtWriter;
boolean hasData;
#Define.ItemGroup("SERVICE")
emailConfig = ABC_EmailConfig::find();
htmlTxtWriter = new HtmlTextWriter();
while select JournalId from prodJournalTableGroup
group by JournalId
where prodJournalTableGroup.LUV_NotifyExPickingCompleted == NoYes::No
&& prodJournalTableGroup.JournalNameId == emailConfig.ExPickingJournalNameId
{
i = 0;
if(hasData)
{
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::br());
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::br());
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::hr());
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
}
prodJournalTable = ProdJournalTable::find(prodJournalTableGroup.JournalId);
this.initValue(prodJournalTable.JournalId);
now = DateTimeUtil::applyTimeZoneOffset(
prodJournalTable.createdDateTime,DateTimeUtil::getCompanyTimeZone());
currentDateStr = "Ngày "+strFmt("%1",DateTimeUtil::day(now))+ " tháng "+strFmt("%1",DateTimeUtil::month(now))+" năm "+strFmt("%1",DateTimeUtil::year(now));
journalId = prodJournalTable.JournalId;
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::table());
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::tr());
htmlTxtWriter.addAttribute("colspan", "9");
htmlTxtWriter.renderBeginTag('th');
htmlTxtWriter.renderBeginTag('b');
htmlTxtWriter.write("ĐỀ NGHỊ CẤP LẠI NGUYÊN PHỤ LIỆU");
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::tr());
htmlTxtWriter.addAttribute('colspan', '4');
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.write(currentDateStr);
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderEndTag();
htmlTxtWriter.addAttribute('colspan', '4');
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderBeginTag('b');
htmlTxtWriter.renderBeginTag('u');
htmlTxtWriter.write(strFmt("Số phiếu đề nghị: %1", journalId));
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::tr());
htmlTxtWriter.addAttribute('colspan', '2');
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderBeginTag('b');
htmlTxtWriter.renderBeginTag('u');
htmlTxtWriter.write("Người đề nghị:");
htmlTxtWriter.renderEndTag();
htmlTxtWriter.write(' ');
htmlTxtWriter.renderEndTag();
htmlTxtWriter.write(userRequest);
htmlTxtWriter.renderEndTag();
htmlTxtWriter.addAttribute('colspan', '2');
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderEndTag();
htmlTxtWriter.addAttribute('colspan', '4');
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.write("Bộ phận: SẢN XUẤT");
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::tr());
htmlTxtWriter.addAttribute('colspan', '2');
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderBeginTag('b');
htmlTxtWriter.renderBeginTag('u');
htmlTxtWriter.write("Tên PO:");
htmlTxtWriter.renderEndTag();
htmlTxtWriter.write(' ');
htmlTxtWriter.renderEndTag();
htmlTxtWriter.write(poNumber);
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.write(strFmt("Style: %1", itemFG));
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.write(' ');
htmlTxtWriter.renderEndTag();
htmlTxtWriter.addAttribute('colspan', '5');
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.write(strFmt("Số lượng: %1 PSC", strFmt("%1",itemFGQty)));
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::tr());
htmlTxtWriter.addAttribute('colspan', '9');
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderBeginTag('b');
htmlTxtWriter.renderBeginTag('u');
htmlTxtWriter.write("Lý do:");
htmlTxtWriter.renderEndTag();
htmlTxtWriter.write(' ');
htmlTxtWriter.renderEndTag();
htmlTxtWriter.write(reason);
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::tr());
htmlTxtWriter.addAttribute('colspan', '9');
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::tr());
htmlTxtWriter.addAttribute('colspan', '9');
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderBeginTag('b');
htmlTxtWriter.renderBeginTag('u');
htmlTxtWriter.write("Gia công tại:");
htmlTxtWriter.renderEndTag();
htmlTxtWriter.write('
');
htmlTxtWriter.renderEndTag();
htmlTxtWriter.write(wrkCtrId);
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::tr());
htmlTxtWriter.addAttribute('colspan', '9');
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderBeginTag('b');
htmlTxtWriter.renderBeginTag('u');
htmlTxtWriter.write("Ngày nhận NPL:");
htmlTxtWriter.renderEndTag();
htmlTxtWriter.write(' ................................................');
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.addAttribute(HtmlTextWriterAttribute::border(), '1');
htmlTxtWriter.addAttribute(HtmlTextWriterAttribute::cellpadding(), '4');
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::table());
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::tr());
htmlTxtWriter.renderBeginTag('b');
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.write("STT");
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderBeginTag('b');
htmlTxtWriter.write("Mã NPL");
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderBeginTag('b');
htmlTxtWriter.write("Mã NCC");
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderBeginTag('b');
htmlTxtWriter.write("Tên NPL");
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderBeginTag('b');
htmlTxtWriter.write("ĐVT");
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderBeginTag('b');
htmlTxtWriter.write("ĐỊNH MỨC");
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderBeginTag('b');
htmlTxtWriter.write("SL ĐỀ NGHỊ CẤP");
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderBeginTag('b');
htmlTxtWriter.write("TỔNG SỐ NPL CẦN CẤP");
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderBeginTag('b');
htmlTxtWriter.write("NOTES");
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
while select prodJournalBOM
where prodJournalBOM.JournalId == prodJournalTableGroup.JournalId
{
inventTable = prodJournalBOM.inventTable();
if (inventTable.itemGroupId() != #ItemGroup)
{
i++;
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::tr());
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.write(strFmt("%1",i));
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.write(prodJournalBOM.ItemId);
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.write(ABC_GeneralLedger::getSupplierCode(prodJournalBOM.ItemId));
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.write(inventTable.itemName());
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.write(prodJournalBOM.BOMUnitId);
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.write(strFmt("%1",prodJournalBOM.BOMProposal));
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.write(strFmt("%1",prodJournalBOM.BOMConsump));
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.write(strFmt("%1",prodJournalBOM.BOMConsump));
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.write("");
htmlTxtWriter.renderEndTag();
}
}
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::tr());
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderEndTag();
htmlTxtWriter.addAttribute('colspan', '2');
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderBeginTag('b');
htmlTxtWriter.write("TỔNG CỘNG");
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::tr());
htmlTxtWriter.addAttribute('colspan', '3');
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderBeginTag('b');
htmlTxtWriter.write("Người đề nghị");
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderBeginTag('b');
htmlTxtWriter.write("Giám đốc sản xuất");
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.addAttribute('colspan', '3');
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderBeginTag('b');
htmlTxtWriter.write("Quản lý kho");
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.addAttribute('colspan', '2');
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderBeginTag('b');
htmlTxtWriter.write("Giám đốc");
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
hasData = true;
this.updateProdJournalTable(journalId);
}
if(hasData)
{
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::table());
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::tr());
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderBeginTag('i');
htmlTxtWriter.write("This is an automated email notification - please do not reply");
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::tr());
htmlTxtWriter.renderBeginTag(HtmlTextWriterTag::td());
htmlTxtWriter.renderBeginTag('b');
htmlTxtWriter.write("AX Dept");
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
htmlTxtWriter.renderEndTag();
}
eMailBody += htmlTxtWriter.getHtmlString();
}
2. Send email class
public void sendEmail()
{
SysEmailParameters parameters = SysEmailParameters::find();
SMTPRelayServerName relayServer;
SMTPPortNumber portNumber;
SMTPUserName userName;
SMTPPassword password;
Str1260 subject;
System.Exception e;
SysMailerMessageBuilder messageBuilder;
str emailSenderName;
str emailSenderAddr;;
SysEmailTable emailTable;
SysEmailMessageTable messageTable;
ABC_EmailConfig emailConfig;
;
try
{
emailConfig = ABC_EmailConfig::find();
messageBuilder = new SysMailerMessageBuilder();
if(eMailBody)
{
emailTable = SysEmailTable::find(emailConfig.ExPickingJournalEmailTemplate);
messageTable = SysEmailMessageTable::find(emailConfig.ExPickingJournalEmailTemplate, "en-us");
emailSenderAddr = emailTable.SenderAddr;
emailSenderName = emailTable.SenderName;
messageBuilder.setFrom(emailSenderAddr, emailSenderName);
subject = strfmt("%1 %2",messageTable.Subject, today());
messageBuilder.setSubject(subject);
messageBuilder = this.addEmailReceiver(messageBuilder);
messageBuilder.setBody(eMailBody);
SysMailerFactory::getNonInteractiveMailer().
sendNonInteractive(messageBuilder.getMessage());
info("Email has been sent successfully");
}
}
catch (Exception::CLRError)
{
e = ClrInterop::getLastException();
while (e)
{
info(e.get_Message());
e = e.get_InnerException();
}
info ("Failed to Send Email some Error occure");
}
}
Result:
Comments
Post a Comment