Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Microsoft.Crm.Sdk.Messages;
- using Microsoft.Xrm.Sdk;
- using Microsoft.Xrm.Sdk.Query;
- using PluginXrm;
- namespace UOP.Plugins.Case
- {
- public class OnCreateEmailChangeRequestCase_SendEmailToAccountPreviousEmail : IPlugin
- {
- private Guid _emailId;
- private Guid _contactId;
- private Guid _userId;
- 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(OnCreateEmailChangeRequestCase_SendEmailToAccountPreviousEmail)} - Service context is null");
- throw new ArgumentNullException(nameof(context));
- }
- //Depth check to prevent infinite loop
- if (context.Depth > 2)
- {
- 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 Case entity from the input parameters
- Entity target = (Entity)context.InputParameters["Target"];
- try
- {
- //If it's update get the latest data
- if (context.MessageName.Equals("create", StringComparison.InvariantCultureIgnoreCase) && target["title"].Equals("Email Change Request"))
- {
- tracingService.Trace($"Attempting to retrieve case with title Email Change Request...");
- // Get a system user to send the email (From: field)
- WhoAmIRequest systemUserRequest = new WhoAmIRequest();
- WhoAmIResponse systemUserResponse = (WhoAmIResponse)service.Execute(systemUserRequest);
- _userId = systemUserResponse.UserId;
- //Get the email recipient user (Contact entity)
- EntityReference customer = (EntityReference)target.Attributes["customerid"];
- Entity caseCustomer = service.Retrieve("account", customer.Id, new ColumnSet("primarycontactid"));
- if (caseCustomer == null)
- {
- return;
- }
- EntityReference contactReference = (EntityReference)caseCustomer.Attributes["primarycontactid"];
- Entity contact = service.Retrieve("contact", contactReference.Id, new ColumnSet(true));
- _contactId = contact.Id;
- // Create the 'From:' activity party for the email
- ActivityParty fromParty = new ActivityParty
- {
- PartyId = new EntityReference(SystemUser.EntityLogicalName, _userId)
- };
- // Create the 'To:' activity party for the email
- ActivityParty toParty = new ActivityParty
- {
- PartyId = new EntityReference(Contact.EntityLogicalName, _contactId)
- };
- tracingService.Trace("Created To and From activity parties.");
- // Create an email message entity
- Email email = new Email
- {
- To = new ActivityParty[] { toParty },
- From = new ActivityParty[] { fromParty },
- Subject = "Confirm email change request",
- Description = $"Hello, {contact.GetAttributeValue<string>("fullname")}. \r\n" +
- $"We received your request to change your primary email address from {target.GetAttributeValue<string>("new_previousemail")} " +
- $"to {target.GetAttributeValue<string>("new_newemail")}.\r\n" +
- $"Please CLICK HERE to confirm your request",
- DirectionCode = true
- };
- _emailId = service.Create(email);
- tracingService.Trace($"Created {email.Subject} email.");
- tracingService.Trace($"Email description: {email.Description}.");
- SendEmailRequest sendEmailRequest = new SendEmailRequest
- {
- EmailId = _emailId,
- TrackingToken = "",
- IssueSend = true
- };
- SendEmailResponse sendEmailResponse = (SendEmailResponse)service.Execute(sendEmailRequest);
- tracingService.Trace($"Sent the confirm email change request message to user with id {contact.Id}.");
- }
- }
- catch (Exception ex)
- {
- tracingService.Trace($"An error occurred in {nameof(OnCreateEmailChangeRequestCase_SendEmailToAccountPreviousEmail)}. Exception details: {ex.Message}");
- throw new InvalidPluginExecutionException(ex.Message);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment