Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Microsoft.Xrm.Sdk;
- namespace UOP.Plugins.Account
- {
- public class OnUpdateAccountEmail_CreateEmailChangeRequestCase : IPlugin
- {
- private const string PreImageAlias = "Pre";
- private const 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(OnUpdateAccountEmail_CreateEmailChangeRequestCase)} - Service context is null");
- throw new ArgumentNullException("context");
- }
- 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 email.");
- //Obtain the pre image and post image entities
- Entity preImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains(PreImageAlias)) ? context.PreEntityImages[PreImageAlias] : null;
- Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(PostImageAlias)) ? context.PostEntityImages[PostImageAlias] : null;
- if (preImageEntity == null || !preImageEntity.Attributes.Contains("emailaddress1") || preImageEntity.GetAttributeValue<string>("emailaddress1") == null)
- {
- throw new InvalidPluginExecutionException($"An error occurred in {nameof(OnUpdateAccountEmail_CreateEmailChangeRequestCase)} plugin. The preImage fields are empty!");
- }
- if (postImageEntity == null || !postImageEntity.Attributes.Contains("emailaddress1") || postImageEntity.GetAttributeValue<string>("emailaddress1") == null)
- {
- throw new InvalidPluginExecutionException($"An error occurred in {nameof(OnUpdateAccountEmail_CreateEmailChangeRequestCase)} plugin. The new value for Email field is null.");
- }
- string previousEmail = preImageEntity.GetAttributeValue<string>("emailaddress1");
- string newEmail = postImageEntity.GetAttributeValue<string>("emailaddress1");
- if (previousEmail == newEmail)
- {
- tracingService.Trace($"Newly entered Email is the same as the old one. Aborting plugin execution.");
- return;
- }
- target["emailaddress1"] = previousEmail;
- tracingService.Trace($"Email field value set to the previous one: {previousEmail}");
- service.Update(target);
- //Create case
- Entity caseRecord = new Entity("incident");
- tracingService.Trace($"Creating new case.");
- //Setting case attributes
- caseRecord["title"] = "Email Change Request";
- tracingService.Trace($"Case Title set to Email Change Request");
- caseRecord["new_previousemail"] = previousEmail;
- tracingService.Trace($"Case Previous Email set to {previousEmail}");
- caseRecord["new_newemail"] = newEmail;
- tracingService.Trace($"Case New Email set to {newEmail}");
- caseRecord["subjectid"] = new EntityReference("subject", Guid.Parse("1F5C140F-DA8D-E911-A97D-000D3A26C11D"));
- tracingService.Trace($"Case Subject set to Email Change Request");
- caseRecord.Attributes.Add("customerid", new EntityReference("account", target.Id));
- tracingService.Trace($"Case connected with account id {target.Id}");
- service.Create(caseRecord);
- tracingService.Trace($"Case with id {caseRecord.Id} created ");
- }
- }
- catch (Exception ex)
- {
- tracingService.Trace($"An error occurred in {nameof(OnUpdateAccountEmail_CreateEmailChangeRequestCase)}. Exception details: {ex.Message}");
- throw new InvalidPluginExecutionException(ex.Message);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement