I have gone through one requirement from one of our client needing email template to get attached with print management and email according to that template. For this i have created a customized email template setup form. Then I have to map Email Template Id to print management settings table. That customization was not very much usual like we use to customize other D365FO forms so I thought to write a blog.

- I have added a new field in PrintMgmtSettings tables of EmailTemplateId.

- Created an extension of PrintMgmtSetupUIMain form which we use to open from this navigation. AR->Setup->Forms->Forms Setup->Print Managment

- Added an un-mapped string field for my EmailTemplateID (Please note that this is not a datasource mapped field since datasource mapping was not available for this field in this form. This form only have the PrintMgmtDocInstance datasource and our field is present in PrintMgmtSettings table which has a child parent relationship with PrintMgmtDocInstance)

- The data flow of PrintMgmtDocInstance table is controlled from PrintMgmtSetupUICtrlDocInstance  and PrintMgmtSetupDocInstance class. So, to get the record of PrintMgmtDocInstance table in my form extension class i need to create get method in extension of PrintMgmtSetupUICtrlDocInstance as shown below

[ExtensionOf(classStr(PrintMgmtSetupUICtrlDocInstance))]
final class PrintMgmtSetupUICtrlDocInstance_Extension
{
/// <summary>
/// Gets a reference to an instance of the corresponding data class.
/// </summary>
public PrintMgmtSetupDocInstance getPrintMgmtSetupDocInstance()
{
return data;
}
}

- After that i created a extension class of PrintMgmtSetupUIMain form to save,show and lookup EmailTemplateID value in Print Management form.

[ExtensionOf(formStr(PrintMgmtSetupUIMain))]
final class PrintMgmtSetupUIMain_Extension
{
/// <summary>
/// get and set instance of PrintMgmtSetupUICtrl which is already initialized in form init method
/// </summary>
/// <param name="_ctrl"></param>
public PrintMgmtSetupUICtrl parmCtrl(PrintMgmtSetupUICtrl _ctrl = ctrl)
{
ctrl = _ctrl;
return ctrl;
}

/// <summary>
/// On email template control modified event we need to store recid of that email template in printMgmtSettings table
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
[FormControlEventHandler(formControlStr(PrintMgmtSetupUIMain, EmailTemplate), FormControlEventType::Modified)]
public static void EmailTemplate_OnModified(FormControl sender, FormControlEventArgs e)
{
FormRun _element = sender.formRun();
PrintMgmtSetupUICtrl _ctrl = _element.parmCtrl();
PrintMgmtSetupUICtrlDocInstance printMgmtDocInstance = _ctrl.parmDocInstanceCtrl();
PrintMgmtSetupDocInstance printMgmtSetupDocInstance = printMgmtDocInstance.getPrintMgmtSetupDocInstance();
PrintMgmtSettings printMgmtSettings;
PrintMgmtSetupSettings printMgmtSetupSettings =_ctrl.parmDocInstanceCtrl().parmSettingsCtl().parmData();
idxEmailTemplate idxEmailTemplate;
PrintMgmtReportFormatId reportFormatID;

select idxEmailTemplate
where idxEmailTemplate.Name == sender.valueStr();

select forupdate firstonly printMgmtSettings
where printMgmtSettings.RecId == printMgmtSetupSettings.parmRecId();


ttsbegin;
printMgmtSettings.EmailTemplate = EmailTemplate.RecId;
printMgmtSettings.doUpdate();
ttscommit;

printMgmtSetupSettings.parmRecVersion(printMgmtSettings.RecVersion);

}

/// <summary>
/// lookup email template
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
[FormControlEventHandler(formControlStr(PrintMgmtSetupUIMain, EmailTemplate), FormControlEventType::Lookup)]
public static void EmailTemplate_OnLookup(FormControl sender, FormControlEventArgs e)
{
///Lookup code for email template
SysTableLookup sysTableLookup = SysTableLookup::newParameters(tableNum(EmailTemplate), sender);
Query query = new Query();
QueryBuildDataSource queryBuildDataSource;

// Add fields
sysTableLookup.addLookupfield(fieldNum(EmailTemplate,Name));
sysTableLookup.addLookupfield(fieldNum(EmailTemplate, Description));

queryBuildDataSource = query.addDataSource(tableNum(EmailTemplate));

// Run lookup
sysTableLookup.parmQuery(query);
sysTableLookup.performFormLookup();

FormControlCancelableSuperEventArgs ce = e as FormControlCancelableSuperEventArgs;

//cancel super() to prevent error.
ce.CancelSuperCall();
}

/// <summary>
/// On tree selection changed we need to show value of email template fetching from email template master through
/// recid saved in printMgmtSettings table
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
[FormControlEventHandler(formControlStr(PrintMgmtSetupUIMain, Tree), FormControlEventType::SelectionChanged)]
public static void Tree_OnSelectionChanged(FormControl sender, FormControlEventArgs e)
{
FormRun _element = sender.formRun();
PrintMgmtSetupUICtrl _ctrl = _element.parmCtrl();
PrintMgmtSetupUICtrlDocInstance printMgmtDocInstance = _ctrl.parmDocInstanceCtrl();
PrintMgmtSetupDocInstance printMgmtSetupDocInstance = printMgmtDocInstance.getPrintMgmtSetupDocInstance();
PrintMgmtSettings printMgmtSettings;
EmailTemplate EmailTemplate;
PrintMgmtReportFormatId reportFormatID;

if(printMgmtSetupDocInstance)
{
select forupdate printMgmtSettings
where printMgmtSettings.ParentId == printMgmtSetupDocInstance.parmRecId();

select EmailTemplate
where EmailTemplate.RecId == printMgmtSettings.EmailTemplate;

FormStringControl formstrControl = _element.control(_element.controlId(formControlStr(PrintMgmtSetupUIMain, EmailTemplate)));
formstrControl.text(EmailTemplate.Name);
}
}
}

That's it,

Hope it helps.