RE: Plugin is not getting triggered when creating automatic Case from Incoming Emails
Hi Alex,
I have tried it and it worked but without context.Depth property,
Code:
public class ExtAPICallOnCrete :IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var serviceb = serviceFactory.CreateOrganizationService(context.UserId);
if (context.Depth > 2)
return;
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity caseRecord = (Entity)context.InputParameters["Target"] ;
try
{
if (caseRecord.LogicalName == "incident")
{
StringBuilder builder = new StringBuilder();
builder.Append(RandomString(4, true));
builder.Append(RandomNumber(1000, 9999));
builder.Append(RandomString(2, false));
caseRecord.Attributes["new_caseid"] = builder.ToString();
caseRecord.Attributes["new_duplicate"] = true;
//return builder.ToString();
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException("Plugin has encountered erro", ex);
tracingService.Trace(ex.ToString());
}
}
}
private string RandomString(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
}