Insert dynamic values in Email Templates from Custom Entities

As a Microsoft Dynamics CRM developer some times you need to work with Email Templates. Basically email templates save time when creating multiple email messages. Email templates contain pre defined data of CRM records, so we don’t have to reenter the same information again. Specifying dynamic values from out of the box entities is pretty simple but there are some instances when we might want to add dynamic values from custom entities. To which we don’t have any straight forward approach in CRM as we cannot add custom entity in email template.

So here is the requirement:

Send an email upon successful creation of Patient record using email template.
Registration email should contain the following format and values from Patient Record.

Dear <Patient Name>,

Thank you for visiting our Hospital.
We would like to inform you that your registration is successful, Please find the details below

<Type : Single Line of Text>
Patient Name : <Name of the patient>
Patient ID : <Patient Business ID>

<Type : Lookup>
Hospital : <Hospital ID>
Doctor : <Patient ID>

<Type : Date Time>
Registration Date with Time: <Registration Date>
Date Of Birth with Time : <Date Of Birth>

<Type : Currency>
Treatment Cost : <Treatment Cost>

<Type : Decimal Number>
Consultation Fee : <Consultation Fee>

<Type : Floating Point Number>
Estimated Amount : <Estimated Amount>

<Type : Multi Line of Text>
Address : <Address >
Patient Details : <Patient Details>


<Type : Multi Select Option Set>
Symptoms : <Type : Option Set>

<Type : Option Set>
Gender : <Patient Gender>
Patient Status : <Patient Status>
Status : 

<Type : Two Option set>
Hospitalization Required : <Hospitalization Required>
Show Patient Details : <Show Patient Details>

Best Regards,
Hospital Management.

Step – 1:
First create an Email Template of type “Global” by following below navigation Settings < Templates and then clicking Email templates and Click on ” New” button then Select Template Type “Global”  and click on “OK”.

Global Email Template-1

Give a proper name like “Patient Registration Template” as shown below

Patient Registration Email Template

Step – 2:
The below table gives the syntax for inserting dynamic values from Custom entities

S.No Field Type Syntax Example
1 Single Line of Text {!EntityLogicalName:FieldLogicalName;} {!new_patient:new_patientname;} or {!new_patient:new_patientbusinessid;}
2 Lookup {!EntityLogicalName:FieldLogicalName/@name;} {!new_patient:new_hospitalid/@name;} or {!new_patient:new_doctorid/@name;}
3 Date {!EntityLogicalName:FieldLogicalName/@date;} {!new_patient:createdon/@date;} or {!new_patient:new_dateofbirth/@date;}
4 Time {!EntityLogicalName:FieldLogicalName/@time;} {!new_patient:createdon/@time;} or {!new_patient:new_dateofbirth/@time;}
5 Currency {!EntityLogicalName:FieldLogicalName;} {!new_patient:new_treatmentcost;}
6 Decimal Number {!EntityLogicalName:FieldLogicalName;} {!new_patient:new_consultationfee;}
7 Floating Number {!EntityLogicalName:FieldLogicalName;} {!new_patient:new_estimatedamount;}
8 Multiline of Text {!EntityLogicalName:FieldLogicalName;} {!new_patient:new_address;} or {!new_patient:new_patientdetails;}
9 Multi Select Option Set {!EntityLogicalName:FieldLogicalName/@name;} {!new_patient:new_symptoms/@name;}
10 Option Set {!EntityLogicalName:FieldLogicalName/@name;} {!new_patient:new_gender/@name;}
11 Two Options {!EntityLogicalName:FieldLogicalName/@name;} {!new_patient:new_hospitalizationrequired/@name;} or {!new_patient:new_showpatientdetails/@name;}

Step – 3: Open the email template and place the required details in email body like below

PatientDetails1

Save the email template and you will see the updated email template like shown below

PatientDetails2

Step -4 : Create a workflow on Patient entity with create event and select the send the email step, associate the email template created above as shown below

PatientRegistrationEmailWorkflow-1

Click on Set Properties and to see the below window and provide details in From/To and select “Patient Registration Email Template” and click on save and close and finally Activate the workflow

PatientRegistrationEmailWorkflow-2

Step – 5: Create a patient record with relevant details as shown below

PatientRecordDetails

Step – 6: Go to Advanced find and search for Email Messages and click on results, You will see list of Email messages created by the system and select the relevant one and open

EmailMessages-1

Here you go, your Patient Registration Email contains all the details which you have mentioned on the email template…!!!

EmailMessages-2

Define different types of Input and Output arguments for custom workflow activity

As a MS Dynamics CRM Developer there are certain situations where you need to pass some of the entity field values as Input arguments to a Custom Workflow activity and some cases you may require to get some of the code execution results as Output arguments from a custom workflow activity.

In order to accomplish this you definitely need to have an idea on the Syntax of the Input and Output arguments for different types of fields.

Here i considered an entity called Patient which is having different types of fields and those are given below
1. Patient Name : String
2. Date Of Birth : Date and Time
3. Hospital : Lookup (to an entity called Hospital)
4. Patient Status : Option Set
5. Hospitalization Required : Two Option Set
6. Patient Age : Whole Number
7. Consultation Fee : Decimal Number
8. Estimated Amount : Floating Point Number
9. Treatment Cost : Currency
10. Patient Id : String

Types of arguments

  1. Input : Entity field value can be passed to Custom workflow activity
  2. Output : Execution result (if any) can be passed as output from Custom workflow activity
  3. Input/Output : It can be used as both Input and Output

The following Custom Workflow Activity code will explains how to declare Input and Output arguments.

using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using System.Activities;

namespace InputOutputParameters
{
    public class InputOutputParameters : CodeActivity
    {
        //Data Type : String 
        [Input("patientname")]
        [RequiredArgument]
        public InArgument<string> PatientName { get; set; }

        //Data Type : Date and Time
        [Input("dateofbirth")]
        [RequiredArgument]
        public InArgument DateOfBirth { get; set; }

        //Data Type : Lookup
        [Input("hospital")]
        [ReferenceTarget("new_hospital")]
        [RequiredArgument]
        public InArgument Hospital { get; set; }

        //Data Type : Option Set
        [Input("patientstatus")]
        [RequiredArgument]
        [AttributeTarget("new_patient", "statuscode")]
        public InArgument PatientStatus { get; set; }

        //Data Type : Two Option Set
        [Input("hospitalizationrequired")]
        [RequiredArgument]
        public InArgument<bool> HospitalizationRequired { get; set; }

        //Data Type : Whole Number
        [Input("patientage")]
        [RequiredArgument]
        public InArgument<int> PatientAge { get; set; }

        //Data Type : Decimal Number
        [Input("consultationfee")]
        [RequiredArgument]
        public InArgument<decimal> ConsultationFee { get; set; }

        //Data Type : Floating Point Number
        [Input("estimatedamount")]
        [RequiredArgument]
        public InArgument<decimal> EstimatedAmount { get; set; }

        //Data Type : Currency
        [Input("treatmentcost")]
        [RequiredArgument]
        public InArgument TreatmentCost { get; set; }

        //Data Type : String
        [Output("showpatientdetails")]
        public OutArgument<string> ShowPatientDetails { get; set; }


        //Data Type : String (Can be used as Input/Output)
        [Input("patientinput")]
        [Output("patientoutput")]
        [RequiredArgument]
        public InOutArgument<string> PatientInOut { get; set; }


        protected override void Execute(CodeActivityContext context)
        {
            try
            {
                IWorkflowContext workflowContext = context.GetExtension();
                IOrganizationServiceFactory serviceFactory = context.GetExtension();
                IOrganizationService service = serviceFactory.CreateOrganizationService(workflowContext.UserId);
                ITracingService tracingService = context.GetExtension();

                tracingService.Trace("Patient Details using input and output parameters Workflow Started.");
                var patientName = PatientName.Get(context);
                var dateOfBirth = DateOfBirth.Get(context);
                var hospital = Hospital.Get(context)?.Name;
                var patientStatus = PatientStatus.Get(context).Value;
                var hospitalizationRequired = HospitalizationRequired.Get(context);
                var patientAge = PatientAge.Get(context);
                var consultationFee = ConsultationFee.Get(context);
                var estimatedAmount = EstimatedAmount.Get(context);
                var treatmentCost = TreatmentCost.Get(context).Value;
                var patientId = PatientInOut.Get(context);
                tracingService.Trace($"Patient Name : {patientName}, Date Of Birth : {dateOfBirth}, Hospital : {hospital}, Patient Status : {patientStatus}, Hospitalization Required: {hospitalizationRequired}, Patient Age: {patientAge}, Consultation Fee : {consultationFee}, Estimated Amount : {estimatedAmount}, Treatment Cost : {treatmentCost}, Patient ID : {patientId}");
                var patientDetails = $"Patient Name : {patientName}, Date Of Birth : {dateOfBirth}, Hospital : {hospital}, Patient Status : {patientStatus}, Hospitalization Required: {hospitalizationRequired}, Patient Age: {patientAge}, Consultation Fee : {consultationFee}, Estimated Amount : {estimatedAmount}, Treatment Cost : {treatmentCost}, Patient ID : {patientId}";
                PatientInOut.Set(context, PatientInOut.ToString());
                ShowPatientDetails.Set(context, patientDetails);
                tracingService.Trace("Patient Details using input and output parameters Workflow Ended.");
            }
            catch(Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
    }

 

The below screen shot will show you the representation of the Input arguments once after you successfully build and deploy the above custom workflow code activity.

Input_Output_Parameters

Download Dynamics CRM Tools for v9.x

Steps to be followed:

  1. Go to Windows, Search for “Windows Power Shell” and Open it. (Shown below)

Windows PowerShell

2. Navigate to the folder where you want to download the tools, for example if you want to download in a D365Tools folder in  C drive, Type  cd D365Tools\ (Shown Below)

Windows PowerShell

3. Copy  and paste  the following Power Shell script in to the Power Shell window and Press Enter. (Shown Below)

$sourceNugetExe = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"
$targetNugetExe = ".\nuget.exe"
Remove-Item .\Tools -Force -Recurse -ErrorAction Ignore
Invoke-WebRequest $sourceNugetExe -OutFile $targetNugetExe
Set-Alias nuget $targetNugetExe -Scope Global -Verbose

##
##Download Plugin Registration Tool
##
./nuget install Microsoft.CrmSdk.XrmTooling.PluginRegistrationTool -O .\Tools
md .\Tools\PluginRegistration
$prtFolder = Get-ChildItem ./Tools | Where-Object {$_.Name -match 'Microsoft.CrmSdk.XrmTooling.PluginRegistrationTool.'}
move .\Tools\$prtFolder\tools\*.* .\Tools\PluginRegistration
Remove-Item .\Tools\$prtFolder -Force -Recurse

##
##Download CoreTools
##
./nuget install Microsoft.CrmSdk.CoreTools -O .\Tools
md .\Tools\CoreTools
$coreToolsFolder = Get-ChildItem ./Tools | Where-Object {$_.Name -match 'Microsoft.CrmSdk.CoreTools.'}
move .\Tools\$coreToolsFolder\content\bin\coretools\*.* .\Tools\CoreTools
Remove-Item .\Tools\$coreToolsFolder -Force -Recurse

##
##Download Configuration Migration
##
./nuget install Microsoft.CrmSdk.XrmTooling.ConfigurationMigration.Wpf -O .\Tools
md .\Tools\ConfigurationMigration
$configMigFolder = Get-ChildItem ./Tools | Where-Object {$_.Name -match 'Microsoft.CrmSdk.XrmTooling.ConfigurationMigration.Wpf.'}
move .\Tools\$configMigFolder\tools\*.* .\Tools\ConfigurationMigration
Remove-Item .\Tools\$configMigFolder -Force -Recurse

##
##Download Package Deployer 
##
./nuget install Microsoft.CrmSdk.XrmTooling.PackageDeployment.WPF -O .\Tools
md .\Tools\PackageDeployment
$pdFolder = Get-ChildItem ./Tools | Where-Object {$_.Name -match 'Microsoft.CrmSdk.XrmTooling.PackageDeployment.Wpf.'}
move .\Tools\$pdFolder\tools\*.* .\Tools\PackageDeployment
Remove-Item .\Tools\$pdFolder -Force -Recurse

##
##Download Package Deployer PowerShell module
##
./nuget install Microsoft.CrmSdk.XrmTooling.PackageDeployment.PowerShell -O .\Tools
$pdPoshFolder = Get-ChildItem ./Tools | Where-Object {$_.Name -match 'Microsoft.CrmSdk.XrmTooling.PackageDeployment.PowerShell.'}
move .\Tools\$pdPoshFolder\tools\*.* .\Tools\PackageDeployment.PowerShell
Remove-Item .\Tools\$pdPoshFolder -Force -Recurse

##
##Remove NuGet.exe
##
Remove-Item nuget.exe

Windows PowerShell

4. You will find the Tools in the folder C:\D365Tools\Tools (The folder you mentioned before)

5. To get the latest version of the tools, Repeat the same steps.