Getting Additional Fields from Lookup while using Email Template

Introduction
In our previous post, we explored the utilization of email templates for custom entities, leveraging the capability to retrieve fields from the primary entity. However, suppose we have a scenario where we need to extract fields from a related entity and incorporate them into our emails. In this post, we will delve into the implementation of this specific use case utilizing Plugins.

Details
Suppose we aim to extract a field from the parent entity of email regarding and insert it into the email body. To fulfill this requirement, we must execute two operations. First retrieve data based on the regardingobjectid field and append it to the email body and second, ensure that the newly added content maintains the same formatting.

For the initial task, we can develop a plugin triggered on Email Create, where we read the regarding object ID and obtain the field value as follows:

if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                    {
            _email = (Entity)context.InputParameters["Target"];
                if(_email.Contains("description") && _email.Contains("regardingobjectid") && && _email.GetAttributeValue<EntityReference>("regardingobjectid").LogicalName=="YourEntityName"))
                    {
                        string currentDescription = _email.GetAttributeValue<string>("description");
                        string _emailfield = GetMoreDetails(_email.GetAttributeValue<EntityReference>("regardingobjectid").Id);

                        if(_emailfield !=string.Empty)
                        {
                          
                            currentDescription = currentDescription + Environment.NewLine + "<div style='-webkit-text-stroke-width:0px;color:#242424;font-family:&quot;Times New Roman&quot;; font-size:medium; font-style:normal; font-variant-caps:normal; font-variant-ligatures:normal; font-weight:400; letter-spacing:normal; orphans:2; text-align:start; text-decoration-color:initial; text-decoration-style:initial; text-decoration-thickness:initial; text-indent:0px; text-transform:none; white-space:normal; widows:2; word-spacing:0px'>Note this is an automated message. If you have any questions, please reach out to the CRM Team "+_emailfield +"</div></div>";

                            Entity emailupdate = new Entity("email");
                            emailupdate.Id = _email.Id;
                            emailupdate["description"] = currentDescription;
                            service.Update(emailupdate);
                         
                        }

                    }


                }

In above code GetMoreDetails is used to get more details from the lookup in the custom entity, We can easily get parent entity field value while query child entity. We can easily design FetchXML for this requirement. Now to address the second task, we can debug the code to inspect the current style applied to emails. We will then utilize the same style for our new line, as outlined in the code.

Summary
We learned how we can use plugin to get fields from the parent entity of the regarding lookup and put it on the email body.

Hope it will help someone !!
Keep learning and Keep Sharing !!

Leave a Reply