Refer here: To know about Follow Functionality in Dynamics 365 CRM
This blog describes the functionality where all users will receive an email notification for case bring followed by them is updated. To demonstrate this we are considering the case origin field on the Case entity.
Through this, the user will know about any actions being performed on the case instead of manually looking into it.
Users can also unfollow the case from receiving email notifications whenever they wish to.
Plugin Code:
namespace Crm.Plugins
{
public class EmailToUsersForCaseFollowed: IPlugin
{
public void Execute(IServiceProvider
serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory =(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service =
serviceFactory.CreateOrganizationService(context.UserId);
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
Entity targetentity = (Entity)context.InputParameters["Target"];
EntityCollection users = GetUsersWhoFollowCase(service,
targetentity.Id);
if (users.Entities.Count > 0)
{
EntityCollection toPartyCollection = new EntityCollection();
foreach (Entity user in users.Entities)
{
Guid userId = user.GetAttributeValue<EntityReference>("ownerid").Id;
Entity toParty = new Entity("activityparty");
toParty["partyid"] = new EntityReference("systemuser", userId);
toPartyCollection.Entities.Add(toParty);
}
if (toPartyCollection.Entities.Count > 0)
{
EntityReference caseref = new EntityReference("incident", targetentity.Id);
//Please change GUID for
SystemUserID to send an email
EntityReference from = new EntityReference("systemuser", new Guid("319c52a2-fc46-ec11-8c60-000d3af29037"));
Entity fromParty = new Entity("activityparty");
fromParty.Attributes.Add("partyid", from);
EntityCollection fromPartyCollection = new EntityCollection();
fromPartyCollection.EntityName = "systemuser";
fromPartyCollection.Entities.Add(fromParty);
Entity email = new Entity("email");
email.Attributes.Add("regardingobjectid", caseref);
email.Attributes.Add("subject", "Case Origin is being Updated");
email.Attributes.Add("description", "your detail description about case updated goes here");
email.Attributes.Add("from", fromPartyCollection);
email.Attributes.Add("to", toPartyCollection);
Guid emailID = service.Create(email);
SendEmailRequest reqSendEmail = new SendEmailRequest();
reqSendEmail.EmailId = emailID;
reqSendEmail.TrackingToken = "";
reqSendEmail.IssueSend = true;
SendEmailResponse res = (SendEmailResponse)service.Execute(reqSendEmail);
}
}
}
public EntityCollection GetUsersWhoFollowCase(IOrganizationService orgService, Guid caseId)
{
EntityCollection userEntityCollection = new EntityCollection();
QueryExpression caseQuery = new QueryExpression("postfollow")
{
NoLock = true,
ColumnSet = new ColumnSet("regardingobjectid", "ownerid")
};
caseQuery.Criteria.AddCondition("regardingobjectid", ConditionOperator.Equal, caseId);
userEntityCollection = orgService.RetrieveMultiple(caseQuery);
return userEntityCollection;
}
}
}
Register the Plugin on
- Update of Case Entity
- with Filtering Attributes caseorigincode.
You can Check the Timeline for the email
received by the user whenever the records are updated.
No comments:
Post a Comment