# How to create Mail Template Since we are migrating all mail templates to new structure, there are few things that changed. ## New Mail Template structure From now on, all mail templates are supposed to be in the `MailVorlagen` table, where we are saving every mail template with its own classification. In order to accomplish that, in the class `MailTemplateReference`, you can see what defines an actual mail template (`ObjectKind`, `ObjectI3D`, `SubObjectKind`, `TemplatePrio`). Why are these properties important? – Every type of mail-template can be uniquely identified by the combination of these values. For example Offer-Mails have ObjectKind=Offer, and all other values are NULL. If there is a MailTemplate in the database with ObjectKind=Offer, and all other values NULL, we know it has to be of type “Offer-Mail”. ObjectI3D can be used to attach this mail-template to some other database row (for example a mail-template with ObjectKind=HelpdeskType, and ObjectI3D is that specific Helpdesk-Type it is used for). SubObjectKind can be used as another unique identifier, if you want to group similar mail-templates together. For example, ObjectKind=Escalation has multiple mail-templates for different cases. They don’t depend on other database rows tho, so we can’t use the ObjectI3D to distinguish them. That’s where we use the SubObjectKind then. How do we do that? – We have created a class named `MailTemplateType` which will contain the mail template skeleton definition. Here is an example how it can be added. ```csharp public static class AccountActivities { public static MailTemplateReference CrmActivity => MailTemplateReference.Create( CentronObjectKindNumeric.AccountActivity, (int)AccountActivityKind.CRM, null, defaultSubject: string.Empty, defaultBody: string.Empty); public static MailTemplateReference Note => MailTemplateReference.Create( CentronObjectKindNumeric.AccountActivity, (int)AccountActivityKind.Note, null, defaultSubject: string.Empty, defaultBody: string.Empty); public static MailTemplateReference PhoneNote => MailTemplateReference.Create( CentronObjectKindNumeric.AccountActivity, (int)AccountActivityKind.PhoneNote, null, defaultSubject: string.Empty, defaultBody: string.Empty); } ``` Please make sure you also add default text for Subject and Body. This can be done in the same file, in class named MailTemplateDefaultText. The Body text can be added as plain text, because we are going to make sure that it’s converted to RTF in the central method. ```csharp public class MailTemplateDefaultText { public static string WebOfferDefaultSubject = @"Default subject"; public static string WebOfferDefaultBody = @"Default body"; } ``` Now after this step is done, you should make sure that Mail Templates Module uses the correct Mail Template Type for the corresponding Mail Template. This step is quite easy, but if there are questions just ask me and I will gladly help. In the `MailTemplatsViewModel` that is in Controls, in the method `CreateMailTemplateTreeViewItemChildren` where all children are created, go to the method that is creating mail templates with the corresponding `CentronObjectKindNumeric` value. There you must replace the parameters that are sent to `CreateMailTemplate` method with the `MailTemplateType` values for the needed parameters. > Tip: For example, you can check how it’s done in the `CreateAccountActivitiesMailTemplates` method. > Note: This approach will be much more simplified after we are done with the migration part, but for now we stick with it. ## Using new methods Now that we have covered that part, it’s time to replace the way we get those mail templates and replace each call with the new method that is returning correct new mail template. This step is also easy and very simple, but it is a bit different if we are using the logic in c-entron.NET or one of our other applications. In `MailTemplateBL.cs` there are 2 methods. One is for the internal (in the Web-Service) usage ```csharp public Result GetMailTemplate(MailTemplateReference mailTemplateReference, int? branchI3D) ``` and the other one is for external (SBO, Outlook AddIn, …) usage. ```csharp public Result GetMailTemplate(CentronObjectKindNumeric objectKind, int? subObjectKind, int? objectId, int? branchI3D) ``` You probably ask yourself what the difference is and why do we have two of them. - Well, there’s no difference. The method for internal usage is just calling the method for external usage, just because of the method parameters. Like I have already mentioned, we have this `MailTemplateType` class which is accessible only in c-entron.NET, so external apps must pass each of the parameters, that are defining each mail template skeleton. ## Migration/Creation of the Mail Template Now one more thing that we need to cover is migration of the existing mail templates that are saved elsewhere and creation of new mail templates. No matter if you are creating a new template, or migrating an existing one, the process is the same. In order to make this step easier and to avoid mistakes, we have created a new method in `ScriptHelper`. You just have to make sure to pass the scripts for getting the correct subject and body and the other of the parameters, the rest will be handeled by the method itself. This method will also make sure that the body is inserted as RTF. ```csharp internal static void InsertMailTemplate(DAOSession session, string subjectScript, string bodyScript, CentronObjectKindNumeric objectKind, int? subObjectKind, int? objectI3D, int? templatePrio = null) ``` Here is one example of how the migration can be done. ```csharp public override Result ExecuteScript(DAOSession session) { ScriptHelpers.InsertMailTemplate(session, GenerateAppSettingsSQL(AppSettingsConst.HelpdeskForwardingInternalEmailSubject), GenerateAppSettingsSQL(AppSettingsConst.HelpdeskForwardingInternalEmailBody), CentronObjectKindNumeric.HelpdeskClass, 1, null); ScriptHelpers.InsertMailTemplate(session, GenerateAppSettingsSQL(AppSettingsConst.HelpdeskForwardingExternalEmailSubject), GenerateAppSettingsSQL(AppSettingsConst.HelpdeskForwardingExternalEmailBody), CentronObjectKindNumeric.HelpdeskClass, 2, null); } private string GenerateAppSettingsSQL(AppSettingsConst settingsConst) { return $@"SELECT WertMemo FROM Stammdat WHERE I3D = {(int)settingsConst}"; } private string GenerateTextModuleSQL(TextModuleType type) { return $@"SELECT Text FROM Textbaustein WHERE Art = {(int)type} AND SichbenuI3D = 0 AND KundenI3D = 0"; } ``` > :exclamation: Please, make sure that this step is required only if the mail template is not in the MailVorlagen table. > Tip: In the `MailTemplateBL.cs` there is still a method `GetAllDelphiMailTemplates`, which can help you to get the needed parameters info faster as well.