After reading my earlier post on sending emails in HTML format, two readers Rishikesh and VP asked, if it is possible sending email using dynamic email template and with the attachments. After reading the complete requirement, I thought its worth a separate post. So, here is the solution -
1. Lets assume we have an email template based on Quote business component as per below screen shot:
2. Create a Quote record as per below details:
3. Attach two files under the Quote in Attachments view:
So, now system is required to send the email in HTML format with dynamic data coming-up from the Quote record and that email should also have the attachments tied with the Quote.
Here below is the script to execute (you can create the workflow also as per your need)
====================================================================
====================================================================
// 1. Retrieve the email body after the substitution
var OCMBS =
TheApplication().GetService("Outbound Communications Manager");
var psOCMInp =
TheApplication().NewPropertySet();
var psOCMOut =
TheApplication().NewPropertySet();
psOCMInp.SetProperty("CommTemplateName",
"Template for Quote"); // Email Template Name
psOCMInp.SetProperty("SourceBusObj",
"Quote");
psOCMInp.SetProperty("SourceId",
"1-ALUGT"); // Row Id of the
Quote Record
// Undocumented method for retrieving email body after substitution
OCMBS.InvokeMethod("ExpandCommTemplate",
psOCMInp, psOCMOut);
var strEmailBody = psOCMOut.GetProperty("ExpandedText"); // Get the Email Body
var strEmailSubject
= psOCMOut.GetProperty("ExpandedSubject"); // Get the Email Subject
// 2. Retrieve the attachments path
var bsFINSService =
TheApplication().GetService("FINS Industry BC Facility Service");
var psFINSInp =
TheApplication().NewPropertySet();
var psFINSOut =
TheApplication().NewPropertySet();
psFINSInp.SetProperty("BusObjName",
"Quote");
psFINSInp.SetProperty("RootBusCompName",
"Quote");
psFINSInp.SetProperty("FileBusCompName",
"Quote Attachment");
psFINSInp.SetProperty("FileNameField",
"QuoteFileName");
psFINSInp.SetProperty("RowId",
"1-ALUGT"); // Row Id of the Quote Record
var QuoteBO =
TheApplication().GetBusObject("Quote");
var QuoteBC = QuoteBO.GetBusComp("Quote");
var QuoteAttachBC =
QuoteBO.GetBusComp("Quote Attachment");
with(QuoteBC)
{
ClearToQuery();
SetViewMode(AllView);
SetSearchSpec("Id",
"1-ALUGT"); // Row Id of the Quote Record
ExecuteQuery();
with(QuoteAttachBC)
{
var isRecord =
FirstRecord();
var strFinalFilePath
= "";
while(isRecord)
{
psFINSInp.SetProperty("AttachmentId",
GetFieldValue("Id"));
bsFINSService.InvokeMethod("GetFile",
psFINSInp, psFINSOut);
strFinalFilePath = strFinalFilePath
+ "*" + psFINSOut.GetValue();
isRecord =
NextRecord();
}
}
}
// 3. Send the email using “SendMessage”
psOCMInp.SetProperty("CommProfile",
"SiebelMantra Profile");
psOCMInp.SetProperty("MsgHTMLBody",
strEmailBody);
psOCMInp.SetProperty("MsgSubject",
strEmailSubject);
psOCMInp.SetProperty("MsgToList",
"siebelmantra@gmail.com");
psOCMInp.SetProperty("AttachFileList",
strFinalFilePath);
OCMBS.InvokeMethod("SendMessage",
psOCMInp, psOCMOut);
Finally, here below is the email that I received: