Advertisement
deleriumbg

CRM Account Email Plugin

Jun 14th, 2019
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.66 KB | None | 0 0
  1. using System;
  2. using Microsoft.Xrm.Sdk;
  3.  
  4. namespace UOP.Plugins.Account
  5. {
  6.     public class OnUpdateAccountEmail_CreateEmailChangeRequestCase : IPlugin
  7.     {
  8.         private const string PreImageAlias = "Pre";
  9.         private const string PostImageAlias = "Post";
  10.  
  11.         public void Execute(IServiceProvider serviceProvider)
  12.         {
  13.             // Obtain the tracing service
  14.             ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
  15.  
  16.             // Obtain the execution context from the service provider.
  17.             IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
  18.             if (context == null)
  19.             {
  20.                 tracingService.Trace($"Error in plugin {nameof(OnUpdateAccountEmail_CreateEmailChangeRequestCase)} - Service context is null");
  21.                 throw new ArgumentNullException("context");
  22.             }
  23.  
  24.             if (context.Depth > 1)
  25.             {
  26.                 return;
  27.             }
  28.  
  29.             // Obtain the organization service reference.
  30.             IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
  31.             IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
  32.  
  33.             // The InputParameters collection contains all the data passed in the message request.
  34.             if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
  35.             {
  36.                 // Obtain the target entity from the input parameters.
  37.                 Entity target = (Entity)context.InputParameters["Target"];
  38.                 try
  39.                 {
  40.                     //If it's update get the latest data
  41.                     if (context.MessageName.Equals("update", StringComparison.InvariantCultureIgnoreCase))
  42.                     {
  43.                         tracingService.Trace($"Attempting to retrieve account email.");
  44.  
  45.                         //Obtain the pre image and post image entities
  46.                         Entity preImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains(PreImageAlias)) ? context.PreEntityImages[PreImageAlias] : null;
  47.                         Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(PostImageAlias)) ? context.PostEntityImages[PostImageAlias] : null;
  48.  
  49.                         if (preImageEntity == null || !preImageEntity.Attributes.Contains("emailaddress1") || preImageEntity.GetAttributeValue<string>("emailaddress1") == null)
  50.                         {
  51.                             throw new InvalidPluginExecutionException($"An error occurred in {nameof(OnUpdateAccountEmail_CreateEmailChangeRequestCase)} plugin. The preImage fields are empty!");
  52.                         }
  53.                         if (postImageEntity == null || !postImageEntity.Attributes.Contains("emailaddress1") || postImageEntity.GetAttributeValue<string>("emailaddress1") == null)
  54.                         {
  55.                             throw new InvalidPluginExecutionException($"An error occurred in {nameof(OnUpdateAccountEmail_CreateEmailChangeRequestCase)} plugin. The new value for Email field is null.");
  56.                         }
  57.  
  58.                         string previousEmail = preImageEntity.GetAttributeValue<string>("emailaddress1");
  59.                         string newEmail = postImageEntity.GetAttributeValue<string>("emailaddress1");
  60.  
  61.                         if (previousEmail == newEmail)
  62.                         {
  63.                             tracingService.Trace($"Newly entered Email is the same as the old one. Aborting plugin execution.");
  64.                             return;
  65.                         }
  66.  
  67.                         target["emailaddress1"] = previousEmail;
  68.                         tracingService.Trace($"Email field value set to the previous one: {previousEmail}");
  69.                         service.Update(target);
  70.  
  71.                         //Create case
  72.                         Entity caseRecord = new Entity("incident");
  73.                         tracingService.Trace($"Creating new case.");
  74.  
  75.                         //Setting case attributes
  76.                         caseRecord["title"] = "Email Change Request";
  77.                         tracingService.Trace($"Case Title set to Email Change Request");
  78.  
  79.                         caseRecord["new_previousemail"] = previousEmail;
  80.                         tracingService.Trace($"Case Previous Email set to {previousEmail}");
  81.  
  82.                         caseRecord["new_newemail"] = newEmail;
  83.                         tracingService.Trace($"Case New Email set to {newEmail}");
  84.  
  85.                         caseRecord["subjectid"] = new EntityReference("subject", Guid.Parse("1F5C140F-DA8D-E911-A97D-000D3A26C11D"));
  86.                         tracingService.Trace($"Case Subject set to Email Change Request");
  87.  
  88.                         caseRecord.Attributes.Add("customerid", new EntityReference("account", target.Id));
  89.                         tracingService.Trace($"Case connected with account id {target.Id}");
  90.  
  91.                         service.Create(caseRecord);
  92.                         tracingService.Trace($"Case with id {caseRecord.Id} created ");
  93.                     }
  94.                 }
  95.                 catch (Exception ex)
  96.                 {
  97.                     tracingService.Trace($"An error occurred in {nameof(OnUpdateAccountEmail_CreateEmailChangeRequestCase)}. Exception details: {ex.Message}");
  98.                     throw new InvalidPluginExecutionException(ex.Message);
  99.                 }
  100.             }
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement