Hi
Please see an example of how you could check dups and create contact/activity.
using System;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace CrmVSSolution.Plugins
{
/// <summary>
/// PostOperationincidentCreate Plugin.
/// </summary>
public class PostOperationincidentCreate: PluginBase
{
public PostOperationincidentCreate(string unsecure, string secure): base(typeof(PostOperationincidentCreate)){}
protected override void ExecuteCrmPlugin(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new InvalidPluginExecutionException("localContext");
}
Entity entCase = (Entity)localContext.PluginExecutionContext.InputParameters["Target"];
// if FN or LN is not set on the case, dont continue
if (!entCase.Attributes.Contains("new_firstname") || !entCase.Attributes.Contains("new_lastname"))
return;
if (!DuplicatesExist(entCase, localContext.OrganizationService))
{
// No Duplicate found
// Create contact
Entity contact = new Entity();
contact.LogicalName = "contact";
contact["firstname"] = entCase["new_firstname"];
contact["lastname"] = entCase["new_lastname"];
localContext.OrganizationService.Create(contact);
// Create activity
Entity task = new Entity("task");
task["subject"] = "Test Task";
task["description"] = "Testing";
task["regardingobjectid"] =entCase.ToEntityReference();
localContext.OrganizationService.Create(task);
}
else
{
// Duplicates Found
// Create activity
Entity task = new Entity("task");
task["subject"] = "Test Task";
task["description"] = "Testing";
task["regardingobjectid"] = entCase.ToEntityReference();
localContext.OrganizationService.Create(task);
}
}
private bool DuplicatesExist(Entity entCase, IOrganizationService CrmService)
{
var QEincident = new QueryExpression("incident");
QEincident.ColumnSet.AddColumns("title", "ticketnumber", "createdon", "incidentid");
QEincident.Criteria.AddCondition("new_firstname", ConditionOperator.Equal, entCase["new_firstname"]);
QEincident.Criteria.AddCondition("new_lastname", ConditionOperator.Equal, entCase["new_lastname"]);
return CrmService.RetrieveMultiple(QEincident).Entities.Count > 1;
}
}
}