Hello Sean,
To create plug-ins please take a look at this documentation:
https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/plugin-development
A plugin can essentially be registered to different events happening on records. For your required scenario, I suggest using the 'Create' and/or 'Update' event messages on the record where the ID value and birth date are located, in my example below I assumed you're using 'contact' but, this can be any entity.
Plugins can run on different stages of the application's pipeline:
- PreValidation - runs before CRM does any validation checks, you will get dirty data
- PreOperation - runs after CRM validation but, before anything is written to the database
- PostOperation - runs after everything is done and written to the database
For your required scenario I suggest using 'PreOperation' as this allows you to run the code when a user saves the record but, just before it's actually been written to the database, so you can make a last-minute addition: extracting the birth date and adding it to the record.
Take a look at these specific docs to get started:
https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/write-plugin
https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/understand-data-context-passed-plugin
https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/register-deploy-plugins
Here's my example of a plug-in that does what you need, I kept it close to the JavaScript example.
This code is not very optimized and could be improved but, I wrote it in a hopefully easy to understand manner for you to use as a starting point.
If you prefer a code sample with syntax highlighting (colored and better readable), I added a highlighted copy here:
https://gist.github.com/justlikeed/1433668720dc1717543ea1f2e920e898
using Microsoft.Xrm.Sdk;
using System;
using System.ServiceModel;
namespace ExtractBirthdaySample
{
public class ExtractBirthdaySamplePlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
//Extract the tracing service for use in debugging sandboxed plug-ins.
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
// The InputParameters collection contains all the data passed in the message request.
// "Target" is the entity the plugin started on
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parameters.
Entity entity = (Entity)context.InputParameters["Target"];
// Verify that the target entity represents an contact and that a value was entered for new_nameofidfield
// Plugins (by default) only receive values for fields that have been changed
if (entity.LogicalName != "contact" ||
entity.Contains("new_nameofidfield"))
{
return;
}
try
{
// Read the current value of the ID field
string idValue = entity.GetAttributeValue<string>("new_nameofidfield");
//If there is any value and its length is 13 (assumption) continue, otherwise ignore it:
if (!string.IsNullOrWhiteSpace(idValue) && idValue.Length == 13)
{
// Define empty integers
int yy, mm, dd;
// From the idValue, use substr to get the parts - Substring is roughly the same as MID in Excel
// use int.TryParse to gracefully convert the strings to integers:
if (int.TryParse(idValue.Substring(0, 2), out yy) &&
int.TryParse(idValue.Substring(2, 2), out mm) &&
int.TryParse(idValue.Substring(4, 2), out dd))
{
// Avoiding a Y2K bug:
if (yy <= 30)
{
yy += 2000; // << Assuming all years 00-30 are 2000-2030
}
else
{
yy += 1900; // << Asusming all other years are 19xx
}
// Creating a real date object:
DateTime dateValue = new DateTime(yy, mm, dd); // No need to do mm-1 here!
entity["birthdate"] = dateValue; // << setting the value to the attribute on the entity
}
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
tracingService.Trace(ex.ToString());
throw new InvalidPluginExecutionException("An error occurred in " + nameof(ExtractBirthdaySamplePlugin) + ": " + ex.Message, ex);
}
catch (Exception ex)
{
tracingService.Trace(ex.ToString());
throw;
}
}
}
}
}