Macros not being replace by handle template for email with macros user task
07:17 Apr 10, 2025
Hi,
I’m using the "Handle Template for Email with Macros" user task, but the output still contains the original template with macros untouched. The macros are not being replaced with actual values, and the Body
parameter is returning the same template content.
Can you help me understand why the macro fields are not being filled in?
Like
1 comments
11:21 Apr 14, 2025
I Created my own User task which handle all macro which have macro source filled in.
protected override bool InternalExecute(ProcessExecutingContext context) { // IMPORTANT: When implementing long-running operations, it is crucial to // enable timely and responsive cancellation. To achieve this, ensure that your code is designed to // respond appropriately to cancellation requests using the context.CancellationToken mechanism. // For more detailed information and examples, please, refer to our documentation. if (TemplateCode == Guid.Empty || RecordId == Guid.Empty) { isSuccess = false; return true; } var templateEsq = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "EmailTemplate"); var bodyColumn = templateEsq.AddColumn("Body"); var macroColumn = templateEsq.AddColumn("Object"); var templateEntity = templateEsq.GetEntity(UserConnection, TemplateCode); if (templateEntity == null) { isSuccess = false; return true; } Guid macroSource = templateEntity.GetTypedColumnValue<Guid>("ObjectId"); string body = templateEntity.GetColumnValue(bodyColumn.Name)?.ToString() ?? ""; var macroSourceEsq = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "SysSchema"); var idFilter = macroSourceEsq.CreateFilterWithParameters(FilterComparisonType.Equal, "UId", macroSource); macroSourceEsq.Filters.Add(idFilter); var schemaNameColumn = macroSourceEsq.AddColumn("Name"); var macroSourceEntities = macroSourceEsq.GetEntityCollection(UserConnection); if (macroSourceEntities == null || macroSourceEntities.Count == 0) { isSuccess = false; return true; } string schemaName = macroSourceEntities[0].GetColumnValue(schemaNameColumn.Name)?.ToString() ?? ""; var macroRegex = new Regex(@"\[#(.*?)#\]"); var matches = macroRegex.Matches(body); if (matches.Count == 0) { ReturnBody = body; isSuccess = false; return true; } var esq = new EntitySchemaQuery(UserConnection.EntitySchemaManager, schemaName); var columnMap = new Dictionary<string, string>(); foreach (Match match in matches) { var macro = match.Groups[1].Value; if (!columnMap.ContainsKey(macro)) { var column = esq.AddColumn(macro); columnMap[macro] = column.Name; } } var entity = esq.GetEntity(UserConnection, RecordId); if (entity == null) { ReturnBody = body; isSuccess = false; return true; } var result = body; foreach (Match match in matches) { var macro = match.Groups[1].Value; if (columnMap.TryGetValue(macro, out string esqColumnName)) { try { var value = entity.GetColumnValue(esqColumnName)?.ToString() ?? ""; result = result.Replace(match.Value, value); } catch (Terrasoft.Common.ItemNotFoundException) { try { var value = entity.GetColumnValue(esqColumnName + "Id")?.ToString() ?? ""; result = result.Replace(match.Value, value); } catch { result = result.Replace(match.Value, ""); } } } } ReturnBody = result; isSuccess = true; return true; }
Show all comments