Friday, July 26, 2013

How to send Email in HTML Format with dynamic data and dynamic attachments?

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");
SetSearchSpec("Id", "1-ALUGT");                                               // Row Id of the Quote Record
var isRecord = FirstRecord();
var strFinalFilePath = "";
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", "");
psOCMInp.SetProperty("AttachFileList", strFinalFilePath);
OCMBS.InvokeMethod("SendMessage", psOCMInp, psOCMOut);


Finally, here below is the email that I received:


  1. Eureka!!! The hidden method "ExpandCommTemplate"!!
    That was exactly what I was breaking my head on for past few days.
    Thanks a lot Gaurav and I really appreciate your quick response !!

    Again!! Great post!!


  2. Hi Gaurav,

    I have one more challenge with this implementation.
    I have attached a HTML file(to render the email msg body) as a Comm Template Item to the respective comm template.
    Somehow the above method "ExpandCommTemplate" is not using the HTML template attachment to extract the HTML msg body. And I am getting a blank email instead.

    Any idea about this one?

  3. Hi VP,

    ExpandCommTemplate method doesn't use the attachment. It will only use the text provided in "Text" field of template. Please refer first screen shot of this post.


  4. Hi Gaurav,

    That way "SendMessage" is online and is there is any problem with sending you are not able to resend the email.

    Two questions:
    1) If there is a problem attaching files, there is an event or error . I've seen that send it anyway if it can't attach the files for any reason.
    2) And the most important, there's any form to do the same for batch "CreateRequest" that works the same adding dynamic data and dynamic attachments, that way you have a feedback is the sending is wrong?

    Thanks you so much,

  5. Hi Gaurav,
    I have requirement like Service Request -> Service Request Attachment -> There is one Record with Attachment
    Now when I click on F9 the pop up email template should automatically attach file from SR Attachment Record
    Can you provide your inputs please

    Thanks in Advance

  6. Is it possible to send the html attachment (as body in the email instead of attachment?

    say for eg, activity attachment BC has an attachment file - html file and i want to send that html file in the body of the email. Is this possible?

  7. yes,it is possible, if you attach the html attachment to template item Under communication template before going to hit the send email process.
    In Template Item Under Communication Template we have a fields "Message Body". just set to "Y" you will get the email as body instead of attachment.