Hi,
I'd liket to create a script task in a business process, that given an email template, it executes it on a specific context. I mean, if the email template is based on a custom macro related with the Lead entity, I can pass the LeadID and the emailTemplateId to the script task, and the result is the text of the email template executed for that specific lead.
Is there way to do that?
Thanks!
Ignacio
Like
Hi Ignacio,
You can pass LeadId and the TemplateId as process parameters (create two Guid parameters in the process settings). Then you can use the "Read data" element and read specific columns of the lead record (use the LeadId parameter in the "Read data" filtration to get the value for needed columns). Then you can create additional text parameters and pass the read column values to them (using formulas for example). The same approach is for the "Email template" object and TemplateId parameter (you need to get the template body, so you need to read it in the "Read data" process element). And finally you can use string interpolation (or String.Replace method) in the script task to replace macroses (macorses, not their values, like [#UsrSomeColumn#], it's passed in the template body) with the actual values read from the "Lead" object using the "Read data" element (the ones that we wrote to the process parameters). To get process parameters values in the script task you need to use Get<string>("ParameterCode") (see more examples here) construction in the script task, then you can use String.Replace in the following manner:
string UsrStringParameterValueToUse = Get<string>("UsrStringParameterValue"); string EmailTemplateBody = Get<string>("UsrEmailTemplateBodyParamter"); EmailTemplateBody= EmailTemplateBody.Replace("[#UsrSomeColumn#]", UsrStringParameterValueToUse);
And then you can return EmailTemplateBody to some another string parameter value using Set<string> command and as a result you will get the result needed.
Thank you for your response Oleg!
Right, that would be the "manual" approach. I was hoping I could reuse the same service that is being used when creating an email from a template. For example, I can use a ReportService to generate a printable inside a business process. I was hoping that we have in Creatio something like "EmailService" that processes an email template and returns the generated text.
Pavel Shkomov,
You need to use the script-task to send the email from the code directly. Please see this Academy article that describes how to form and send the email from the code (this article also have examples of implementations, please use the "Examples" tab at the top of the article).
Ignacio Alvarez,
there is another approach:
var macrosHelperClassFactory = ClassFactory.Get<MacrosHelperV2>(new ConstructorArgument("userConnection", UserConnection)); string emailTemplateCode = Get<string>("ResultEmailTemplatePlain"); string templateEntityName = Get<string>("ContactEntityName"); Guid templateEntityId = Get<Guid>("ContactRecordId"); var replacementResult = macrosHelperClassFactory.GetTextTemplate(emailTemplateCode, templateEntityName, templateEntityId); Set<string>("ResultEmailTemplateHTML", replacementResult); return true;
There should be also the following parameter values:
1) the code is ContactEntityName - Text (50 characters) data type ("Contact" value by default) - represents the object from which data for macros is selected
2) the code is ContactRecordId - Unique identifier data type - represents the record Id to get data for macroses
3) the code is ResultEmailTemplateHTML - Unlimited length text data type - contains the template with filled macroses
4) the code is ResultEmailTemplatePlain - Unlimited length text data type - contains the template without filled macroses
Also you need to add the following namespaces to the process usings:
Terrasoft.Configuration.Utils
Terrasoft.Core.Factories
You need to use the "Read data" process element and read specific email template (the object is "Message template") and you need only the "Body" column value. Then the read body of the template should be passed to the "ResultEmailTemplatePlain" parameter using the "Formula" process element.
As a result of the script-task process element you will get the formed email template body with filled macroses in the ResultEmailTemplateHTML parameter that can be then displayed for example using the autogenerated page.