serving the solutions day and night

Pages

Wednesday, March 30, 2016

Dynamics CRM - Update Contact Fullname

My client want the fullname with the suffix and custom order like LastName, FirstName Middle Name Suffix. Dynamics CRM System Settings doesn't have the option to set the custom order fullname. Contact entity fullname is readonly attribute, it won't allow to update the fullname directly.

You can update through only pre create or update operation plugin.

i created one image for pre update, it contains firstname,lastname,middlename,dns_suffix and also FilteringAttributes.
Plugin Step & image
<Plugin Description="Update Full Name with Suffix" FriendlyName="FullName" Name="DNS.VM.Plugins.FullName" Id="00000000-0000-0000-0000-000000000000" TypeName="DNS.VM.Plugins.FullName">
  <Steps>
    <clear />
    <Step CustomConfiguration="" Name="PreCreate" Description="Update Full Name" Id="00000000-0000-0000-0000-000000000000" MessageName="Create" Mode="Synchronous" PrimaryEntityName="contact" Rank="1" SecureConfiguration="" Stage="PreInsideTransaction" SupportedDeployment="ServerOnly" />
    <Step CustomConfiguration="" Name="PreUpdate" Description="Update Full Name" FilteringAttributes="firstname,lastname,middlename,dns_suffix" Id="00000000-0000-0000-0000-000000000000" MessageName="Update" Mode="Synchronous" PrimaryEntityName="contact" Rank="1" SecureConfiguration="" Stage="PreInsideTransaction" SupportedDeployment="ServerOnly">
      <Images>
<Image Attributes="firstname,lastname,middlename,dns_suffix" EntityAlias="PreImage" Id="00000000-0000-0000-0000-000000000000" MessagePropertyName="Target" ImageType="PreImage" />
      </Images>
    </Step>
  </Steps>
</Plugin>

Plugin Code
public class FullName : Plugin
{
public FullName()
   : base(typeof(FullName))
{
   base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(20, "Create", 'contact', new Action<LocalPluginContext>(ExecutePreCreate)));
   base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(20, "Update", 'contact', new Action<LocalPluginContext>(ExecutePreUpdate)));
}

/// Plugin steps:
/// create - contact - pre-operation - synchronous
/// update - contact - pre-operation - synchronous - filtering attributes: firstname, lastname, suffix, middlename
/// </summary>
protected void ExecutePreCreate(LocalPluginContext localContext)
{
   if (localContext == null) throw new ArgumentNullException("localContext");

   IPluginExecutionContext context = localContext.PluginExecutionContext;
   IOrganizationService service = localContext.OrganizationService;

   if (context.InputParameters != null && context.InputParameters.Contains("Target"))
   {
Entity target = (Entity)context.InputParameters["Target"];
Entity img = (context.PreEntityImages != null && context.PreEntityImages.Contains("PreImage")) ? localContext.PluginExecutionContext.PreEntityImages["PreImage"] : null;

SetFullName(target, img);
   }
}

protected void ExecutePreUpdate(LocalPluginContext localContext)
{
   if (localContext == null) throw new ArgumentNullException("localContext");

   IPluginExecutionContext context = localContext.PluginExecutionContext;
   IOrganizationService service = localContext.OrganizationService;

   if (context.InputParameters != null && context.InputParameters.Contains("Target"))
   {
Entity target = (Entity)context.InputParameters["Target"];
Entity img = (context.PreEntityImages != null && context.PreEntityImages.Contains("PreImage")) ? localContext.PluginExecutionContext.PreEntityImages["PreImage"] : null;

SetFullName(target, img);
   }
}

private void SetFullName(Entity target, Entity img)
{
   string firstName = GetValue(target, img, 'fistname');
   string lastName = GetValue(target, img, 'lastname');
   string middlename = GetValue(target, img, 'middlename');
   string suffix = string.Empty;


   //Suffix
   if (target.Attributes.Contains('dns_suffix'))
   {
if (target.Attributes['dns_suffix'] != null)
   suffix = GetSuffix(Int32.Parse(((OptionSetValue)(target.Attributes['dns_suffix'])).Value.ToString()));
   }
   else if (img != null && img.Attributes.Contains('dns_suffix'))
   {
if (img.Attributes['dns_suffix'] != null)
   suffix = GetSuffix(Int32.Parse(((OptionSetValue)(img.Attributes['dns_suffix'])).Value.ToString()));
   }

   //Full Name
   string fullName = string.Join(" ", new string[] { lastName+",", firstName, middlename, suffix }.Where(a => !string.IsNullOrWhiteSpace(a)));

   target['fullname'] = fullName;
}

private string GetValue(Entity target, Entity img, string attname)
{
string attval = string.Empty;
if (target.Attributes.Contains(attname) && target.Attributes[attname]!=null)
attval = target.Attributes[attname].ToString()
else if (img != null && img.Attributes.Contains(attname) && img.Attributes[attname] != null)
attval = img.Attributes[attname].ToString();
return attval;
}

private string GetSuffix(int value)
{
   switch (value)
   {
case 0: return "JR";
case 1: return "SR";
case 2: return "II";
case 3: return "III";
case 4: return "IV";
case 5: return "V";
case 6: return "VI";
case 7: return "VII";
default: return "";
   }
        }
}

2 comments:

Suseela Susiee said...


Great and useful article. Creating content regularly is very tough. Your points are motivated me to move on.


SEO Company in Chennai

Philips Huges said...


Its very useful to me. Wonderful blog.. Thanks for sharing informative Post.

Installment loans
Payday loans
Title loans