deleriumbg

CRM Account Plugin

Jun 12th, 2019
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.96 KB | None | 0 0
  1. using Microsoft.Xrm.Sdk;
  2. using System;
  3. using System.Linq;
  4.  
  5.  
  6. namespace UOP.Plugins.Account
  7. {
  8.     public class OnUpdateAccountFirstLastName_OnPostUpdateSetCustomFullName : IPlugin
  9.     {
  10.         private readonly string preImageAlias = "Pre";
  11.         private readonly string postImageAlias = "Post";
  12.  
  13.         public void Execute(IServiceProvider serviceProvider)
  14.         {
  15.             // Obtain the tracing service
  16.             ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
  17.  
  18.             // Obtain the execution context from the service provider.
  19.             IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
  20.             if (context == null)
  21.             {
  22.                 tracingService.Trace($"Error in plugin {nameof(OnUpdateAccountFirstLastName_OnPostUpdateSetCustomFullName)} - Service context is null");
  23.                 throw new InvalidPluginExecutionException("Service context is null");
  24.             }
  25.  
  26.             if (context.Depth > 1)
  27.             {
  28.                 return;
  29.             }
  30.  
  31.             // Obtain the organization service reference.
  32.             IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
  33.             IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
  34.  
  35.             // The InputParameters collection contains all the data passed in the message request.
  36.             if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
  37.             {
  38.                 // Obtain the target entity from the input parameters.
  39.                 Entity target = (Entity)context.InputParameters["Target"];
  40.                 try
  41.                 {
  42.                     //If it's update get the latest data
  43.                     if (context.MessageName.Equals("update", StringComparison.InvariantCultureIgnoreCase))
  44.                     {
  45.                         tracingService.Trace($"Attempting to retrieve account data.");
  46.  
  47.                         //Obtain the pre image and post image entities
  48.                         Entity preImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null;
  49.                         Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;
  50.  
  51.                         string previousFirstName = preImageEntity.GetAttributeValue<string>("new_firstname");
  52.                         string newFirstName = postImageEntity.GetAttributeValue<string>("new_firstname");
  53.  
  54.                         string previousLastName = preImageEntity.GetAttributeValue<string>("new_lastname");
  55.                         string newLastName = postImageEntity.GetAttributeValue<string>("new_lastname");
  56.                        
  57.                         if (newFirstName == null && newLastName == null)
  58.                         {
  59.                             tracingService.Trace($"Both First Name and Last Name fields cannot be empty. Account entity was not updated.");
  60.                             return;
  61.                         }
  62.  
  63.                         if (previousFirstName == newFirstName && previousLastName == newLastName)
  64.                         {
  65.                             tracingService.Trace($"Newly entered First Name and Last Name are the same as the old ones. Account entity was not updated.");
  66.                             return;
  67.                         }
  68.  
  69.                         tracingService.Trace($"First name updated from {previousFirstName ?? "Empty field"} to {newFirstName}");
  70.                         tracingService.Trace($"Last name updated from {previousLastName ?? "Empty field"} to {newLastName}");
  71.  
  72.                         //Join the first name and last name (ignoring nulls)
  73.                         string previousFullName = preImageEntity.GetAttributeValue<string>("new_customfullname");
  74.                         string newFullName = string.Join(" ", new string[] { newFirstName, newLastName }.Where(n => !string.IsNullOrWhiteSpace(n)));
  75.  
  76.                         //Update target, overriding whatever CRM sets
  77.                         target["new_customfullname"] = newFullName;
  78.                         target["name"] = newFullName;
  79.                         tracingService.Trace($"Full name updated successfully from {previousFullName ?? "Empty field"} to {newFullName}");
  80.  
  81.                         service.Update(target);
  82.                     }
  83.                 }
  84.                 catch (Exception ex)
  85.                 {
  86.                     tracingService.Trace($"Failed to update Custom Full Name field. Error details: {ex.Message}");
  87.                     throw new InvalidPluginExecutionException(ex.Message);
  88.                 }
  89.             }
  90.         }
  91.     }  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment