Posted in CRM, Plugin

Sending an Email if notes has been Created or Updated or Deleted

screenshot-12

Sending an email to the user Manager(Email Address2)

using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using Microsoft.Xrm.Sdk;
 //using CRMEntityWrapper;
 using Microsoft.Crm.Sdk.Messages;
 using Microsoft.Xrm.Sdk.Query;
 namespace NotesMail
 {
 public class SendMail :IPlugin
 {
 public void Execute(IServiceProvider serviceProvider)
 {
 IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
 IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
 IOrganizationService service = (IOrganizationService)serviceFactory.CreateOrganizationService(context.UserId);
 ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
 try
 {
 //if (context.MessageName == "Create" || context.MessageName == "Update")
 //{
 if (context.InputParameters.Contains("Target") && (context.InputParameters["Target"] is Entity || context.InputParameters["Target"] is EntityReference))
 {
 // create an email
 Annotation note = service.Retrieve(Annotation.EntityLogicalName, context.PrimaryEntityId, new ColumnSet(true)).ToEntity();
 Email emailCreate = new Email();
 Lead lead = new Lead();
 lead = null;
 Opportunity opportunity = new Opportunity();
 tracingService.Trace(note.ObjectId.ToString());
 tracingService.Trace(note.ObjectId.LogicalName.ToString());
 if (note.ObjectId.LogicalName == Lead.EntityLogicalName)
 lead = service.Retrieve(Lead.EntityLogicalName, note.ObjectId.Id, new ColumnSet(true)).ToEntity();
 else if (note.ObjectId.LogicalName == Opportunity.EntityLogicalName)
 opportunity = service.Retrieve(Opportunity.EntityLogicalName, note.ObjectId.Id, new ColumnSet(true)).ToEntity();
 string subject = null;
 if (context.MessageName == "Create")
 subject = " The Notes Created for the " + note.ObjectId.LogicalName.ToString() + ":" + (lead != null ? lead.FullName.ToString() : opportunity.ParentAccountId.Name.ToString());
 else if (context.MessageName == "Update")
 subject = "The Notes Updated for the " + note.ObjectId.LogicalName.ToString() + ":" + (lead != null ? lead.FullName.ToString() : opportunity.ParentAccountId.Name.ToString());
 else if (context.MessageName == "Delete")
 subject = "The Notes Deleted for the" + note.ObjectId.LogicalName.ToString() + ":" + (lead != null ? lead.FullName.ToString() : opportunity.ParentAccountId.Name.ToString());
 tracingService.Trace(subject);
 emailCreate.Subject = subject;
 string description = null;
 if (context.MessageName == "Create")
 //description = "

Hi sir,


The salesperson created the notes for " + note.ObjectId.LogicalName.ToString() + "


description:"+note.NoteText.ToString()+"
created by:" + note.OwnerId.Name.ToString();
 description = "

Hi ,


The Notes has been created by : " + note.OwnerId.Name.ToString() + " for the " + note.ObjectId.LogicalName.ToString() + " :" + (lead != null ? lead.FullName.ToString() : opportunity.ParentAccountId.Name.ToString()) + " with the following details:

" + "
 Notes Title:" + note.Subject.ToString() + "


 Description:" + note.NoteText.ToString() + "

" + "



 Thanks and Regards 
" + note.OwnerId.Name.ToString() + "

";

else if (context.MessageName == "Update")
 //description = "

Hi sir,


The salesperson Updated the notes for " + note.ObjectId.LogicalName.ToString() + "


description:" + note.NoteText.ToString() + "
created by:" + note.OwnerId.Name.ToString();
 description = "

Hi ,


The Notes has been updated by : " + note.OwnerId.Name.ToString() + " for the " + note.ObjectId.LogicalName.ToString() + " :" + (lead != null ? lead.FullName.ToString() : opportunity.ParentAccountId.Name.ToString()) + " with the following details:

" + "
 Notes Title : " + note.Subject.ToString() + "


 Description : " + note.NoteText.ToString() + "

" + "



 Thanks and Regards 
" + note.OwnerId.Name.ToString() + "

";
 else if (context.MessageName == "Delete")
 description = "

Hi ,


The Notes has been deleted by : " + note.OwnerId.Name.ToString() + " for the " + note.ObjectId.LogicalName.ToString() + " :" + (lead != null ? lead.FullName.ToString() : opportunity.ParentAccountId.Name.ToString()) + " with the following details:

" + "
 Notes Title : " + note.Subject.ToString() + "


 Description : " + note.NoteText.ToString() + "

" + "



 Thanks and Regards 
" + note.OwnerId.Name.ToString() + "

";
 emailCreate.Description = description;
 tracingService.Trace(description);
 //specify the owner for the mail
 emailCreate.OwnerId = new EntityReference(SystemUser.EntityLogicalName, context.InitiatingUserId);

//Fetching Manager Id
 EntityReference manager = (EntityReference)(service.Retrieve(SystemUser.EntityLogicalName, context.InitiatingUserId, new ColumnSet("parentsystemuserid"))).Attributes["parentsystemuserid"];
 string emailto = (string)(service.Retrieve(SystemUser.EntityLogicalName, manager.Id, new ColumnSet("personalemailaddress"))).Attributes["personalemailaddress"];
 string fetchContact = string.Format(Resource1.String1, new string[] { emailto });
 EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchContact));

ActivityParty ap = new ActivityParty();
 ap.PartyId = ec.Entities[0].ToEntityReference();
 //emailCreate.ownerid.type = EntityName.systemuser.ToString();
 //emailCreate.ownerid.Value = new Guid(ownerID);

// specify to part of the email
 emailCreate.To = new ActivityParty[] { ap };

// specify the from part of the email
 ActivityParty from = new ActivityParty();
 from.PartyId = new EntityReference(SystemUser.EntityLogicalName, context.InitiatingUserId);
 // from.PartyId.type = EntityName.SystemUser.ToString();
 // from.PartyId.Value = new Guid(UserID);
 emailCreate.From = new ActivityParty[] { from };

// finally create the email and get the guid of the email
 Guid emailId = service.Create(emailCreate);
 tracingService.Trace(emailId.ToString());
 if (note.FileName != null)
 {
 ActivityMimeAttachment attachment = new ActivityMimeAttachment();
 attachment.FileName = note.FileName.ToString();
 attachment.Body = note.DocumentBody.ToString();
 attachment.MimeType = "text/plain";
 attachment.AttachmentNumber = 1;
 attachment.ObjectId = new EntityReference(Email.EntityLogicalName, emailId);
 attachment.ObjectTypeCode = Email.EntityLogicalName;
 service.Create(attachment);
 }

SendEmailRequest req = new SendEmailRequest();
 req.EmailId = emailId;
 req.TrackingToken = "";
 req.IssueSend = true;

// Finally Send the email message.
 SendEmailResponse res = (SendEmailResponse)service.Execute(req);

//}
 //else
 //{

//}
 }
 }
 catch (Exception ex)
 {
 throw new InvalidPluginExecutionException(ex.Message);
 }
 }
 }
 }

Resource File- Contact (user manger email address2== email(contact-usermanager))

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s