Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xrm.Sdk;
- using System;
- using System.Linq;
- namespace UOP.Plugins.Account
- {
- public class OnUpdateAccountFirstLastName_OnPostUpdateSetCustomFullName : IPlugin
- {
- private readonly string preImageAlias = "Pre";
- private readonly string postImageAlias = "Post";
- public void Execute(IServiceProvider serviceProvider)
- {
- // Obtain the tracing service
- ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
- // Obtain the execution context from the service provider.
- IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
- if (context == null)
- {
- tracingService.Trace($"Error in plugin {nameof(OnUpdateAccountFirstLastName_OnPostUpdateSetCustomFullName)} - Service context is null");
- throw new InvalidPluginExecutionException("Service context is null");
- }
- if (context.Depth > 1)
- {
- return;
- }
- // Obtain the organization service reference.
- IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
- IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
- // The InputParameters collection contains all the data passed in the message request.
- if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
- {
- // Obtain the target entity from the input parameters.
- Entity target = (Entity)context.InputParameters["Target"];
- try
- {
- //If it's update get the latest data
- if (context.MessageName.Equals("update", StringComparison.InvariantCultureIgnoreCase))
- {
- tracingService.Trace($"Attempting to retrieve account data.");
- //Obtain the pre image and post image entities
- Entity preImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null;
- Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;
- string previousFirstName = preImageEntity.GetAttributeValue<string>("new_firstname");
- string newFirstName = postImageEntity.GetAttributeValue<string>("new_firstname");
- string previousLastName = preImageEntity.GetAttributeValue<string>("new_lastname");
- string newLastName = postImageEntity.GetAttributeValue<string>("new_lastname");
- if (newFirstName == null && newLastName == null)
- {
- tracingService.Trace($"Both First Name and Last Name fields cannot be empty. Account entity was not updated.");
- return;
- }
- if (previousFirstName == newFirstName && previousLastName == newLastName)
- {
- tracingService.Trace($"Newly entered First Name and Last Name are the same as the old ones. Account entity was not updated.");
- return;
- }
- tracingService.Trace($"First name updated from {previousFirstName ?? "Empty field"} to {newFirstName}");
- tracingService.Trace($"Last name updated from {previousLastName ?? "Empty field"} to {newLastName}");
- //Join the first name and last name (ignoring nulls)
- string previousFullName = preImageEntity.GetAttributeValue<string>("new_customfullname");
- string newFullName = string.Join(" ", new string[] { newFirstName, newLastName }.Where(n => !string.IsNullOrWhiteSpace(n)));
- //Update target, overriding whatever CRM sets
- target["new_customfullname"] = newFullName;
- target["name"] = newFullName;
- tracingService.Trace($"Full name updated successfully from {previousFullName ?? "Empty field"} to {newFullName}");
- service.Update(target);
- }
- }
- catch (Exception ex)
- {
- tracingService.Trace($"Failed to update Custom Full Name field. Error details: {ex.Message}");
- throw new InvalidPluginExecutionException(ex.Message);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment