How many cases are created with in 30 days, if more than 3 and less than 5 send a mail to team lead, if more than 5 and less than 9 then send a mail to manager using plugin.
If any case created, then check for the same user how many cases are created with in 30 days, if more than 2 and less than 5 send a mail to team lead, if more than 5 and less than 9 then send a mail to manager using plugin.
So hear when ever a case is created with a same user , lets take user as account and check the cases where created with in 30 days.
So hear for same user if cases are more than 2 and less than 5 then send a mail to Team lead.
Next for same user if cases are more than 5 and less than 9 then send a mail to Manager.
Step 1: Create a Plugin
For creating of plugin > open Visual Studio ,Create a project with Class Library and add Nuget packages (Microsoft.Xrm.Tooling.Connector assembly).
Plugin Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace checkCONDITIOINmail123
{
public class PluginMail : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
try
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
ITracingService tracing = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity caseEntity = (Entity)context.InputParameters["Target"];
EntityReference user = caseEntity.Contains("customerid") ? (EntityReference)caseEntity.Attributes["customerid"] : null;
Guid guid = new Guid(user.Id.ToString());
tracing.Trace("Guid is {0}", guid);
string dynamicGuid = guid.ToString();
dynamicGuid = dynamicGuid.Replace("{", "").Replace("}", "");// to use in fetchxml
tracing.Trace("DynamicsGuid is {0} ", dynamicGuid);
EntityReference contactuser = caseEntity.Contains("primarycontactid") ? (EntityReference)caseEntity.Attributes["primarycontactid"] : null;
Guid guid1 = new Guid(contactuser.Id.ToString());
tracing.Trace("Guid is {0}", guid1);
string dynamicGuid1 = guid.ToString();
dynamicGuid1 = dynamicGuid1.Replace("{", "").Replace("}", "");// to use in fetchxml
tracing.Trace("DynamicsGuid is {0} ", dynamicGuid1);
DateTime dateTime = new DateTime();
dateTime = DateTime.UtcNow;
tracing.Trace("Date check {0} ", dateTime.ToString());
// Extract necessary information, such as user and case creation date.
if (user != null)
{
string fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
" <entity name='incident'>" +
" <attribute name='title' />" +
" <attribute name='ticketnumber' />" +
" <attribute name='createdon' />" +
" <attribute name='incidentid' />" +
" <attribute name='caseorigincode' />" +
" <order attribute='title' descending='false' />" +
" <filter type='and'>" +
" <condition attribute='customerid' operator='eq' uiname='A. Datum Corporation' uitype='account' value='" + dynamicGuid + "' />" +
" </filter>" +
" </entity>" +
"</fetch>";
//tracing.Trace("fetch xml {0}", fetchXml);
EntityCollection userRecord = service.RetrieveMultiple(new FetchExpression(fetchXml));
tracing.Trace("Count is {0} ", userRecord.Entities.Count);
var ACCOUNTcount = userRecord.Entities.Count;
//initialize email entity
// Entity email = new Entity("email");
// Entity fromparty = new Entity("activityparty");
// Entity toparty = new Entity("activityparty");
//// tracing.Trace(context.UserId.ToString());
// fromparty["partyid"] = new EntityReference("account", user.Id);
// toparty["partyid"] = new EntityReference("contact", contactuser.Id);
// email["from"] = new Entity[] { fromparty };
// email["to"] = new Entity[] { toparty };
// email["subject"] = "Welcome case field" +" "+ dateTime;
// email["description"] = "Hi, How many cases were resolved"+ " "+ dateTime;
// email["directioncode"] = true;
// email["regardingobjectid"] = new EntityReference("contact", contactuser.Id);
// //create an email activity record
// Guid emailId = service.Create(email);
if (ACCOUNTcount > 2 && ACCOUNTcount < 5)
{
//initialize email entity
Entity email = new Entity("email");
Entity fromparty = new Entity("activityparty");
Entity toparty = new Entity("activityparty");
// tracing.Trace(context.UserId.ToString());
fromparty["partyid"] = new EntityReference("account", user.Id);
toparty["partyid"] = new EntityReference("contact", contactuser.Id);
email["from"] = new Entity[] { fromparty };
email["to"] = new Entity[] { toparty };
email["subject"] = "Team Lead" + " " + dateTime;
email["description"] = "Hi Team Lead, Today cases were resolved between 2 to 5" + " " + dateTime;
email["directioncode"] = true;
email["regardingobjectid"] = new EntityReference("contact", contactuser.Id);
//create an email activity record
Guid emailId = service.Create(email);
}
// Example: Send email to manager
else if (ACCOUNTcount > 5 && ACCOUNTcount < 9)
{
Entity email = new Entity("email");
Entity fromparty = new Entity("activityparty");
Entity toparty = new Entity("activityparty");
// tracing.Trace(context.UserId.ToString());
fromparty["partyid"] = new EntityReference("account", user.Id);
toparty["partyid"] = new EntityReference("contact", contactuser.Id);
email["from"] = new Entity[] { fromparty };
email["to"] = new Entity[] { toparty };
email["subject"] = "Manager" + " " + dateTime;
email["description"] = "Hi Manager, Today cases were resolved between 5 to 9" + " " + dateTime;
email["directioncode"] = true;
email["regardingobjectid"] = new EntityReference("contact", contactuser.Id);
//create an email activity record
Guid emailId = service.Create(email);
}
}
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
}
}
Step 2: Register the plugin in plugin Registration tool
1. Build the solution in visual studio and Register an assemble.
2. When register an assemble browse .dll file (checkCONDITIOINmail123.dll).
3. After creating the assemble inside that Register a step with create message.
checkCONDITIOINmail123.dll
Click on Updated Selected Plugin
Register a step with create message
Step 3 : Create a Case for a particular user (account) and provide contact of same account for checking the mail is sending or not
Hear check if cases are more than 2 and less than 5 then send a mail to Team lead
Hear check if cases are more than 5 and less than 9 then send a mail to Manager.





Comments
Post a Comment