I wanted to have step in workflow process where draft Email will be created using template.
There are two out of the box options regarding Email step in workflow. We can create draft email with specified body and subject. Other option is to send email using some workflow.
In order to create draft email with rendered template I created custom workflow activity which will execute InstantiateTemplateRequest, and for a result return rendered subject and message body.
This is a part of workflow activity:
InstantiateTemplateRequest instTemplateReq = new InstantiateTemplateRequest
{
TemplateId = templateId,
ObjectId = contactId,
ObjectType = "contact"
};
InstantiateTemplateResponse instTemplateResp = (InstantiateTemplateResponse)service.Execute(instTemplateReq);
Three parameters are sent, Template Id (Guid of email template), Contact Id as Object Id and contact Object Type. Here is rendered template of Contact type.
After executing request we need to pull out result details from InstantiateTemplateResponse object:
Entity template = instTemplateResp.EntityCollection.Entities[0];
if (template != null && template.Attributes.Contains("description"))
{
TemplateSubject.Set(executionContext, template.Attributes["subject"].ToString());
TemplateDescription.Set(executionContext, template.Attributes["description"].ToString());
}
In last workflow step I added Create new email step where Subject and Description are applied on new email record.