i have written the code which is check test case:
Led to lead working.
lead to contact working
contact to contact also working
but contact to lead not working.
i am sharing the code in that contact to contact and contact to lead code please help me for this:
public class OnCreateContact : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
try
{
ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
tracer.Trace("Execution started.");
tracer.Trace("Message Name: " + context.MessageName);
tracer.Trace("Stage: " + context.Stage);
tracer.Trace("Depth: " + context.Depth);
Entity target = null;
tracer.Trace("1");
string message_stage = context.MessageName + "_" + context.Stage;
switch (message_stage)
{
case "Create_20":
if (context.InputParameters.Contains("Target"))
{
target = context.InputParameters.Contains(Model.Common.TARGET) ? (Entity)context.InputParameters[Model.Common.TARGET] : null;
tracer.Trace("2");
CreateLead(service, tracer, target);
tracer.Trace("3");
return;
}
break;
default:
break;
}
}
catch (InvalidPluginExecutionException ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
private void CreateLead(IOrganizationService service, ITracingService tracer, Entity target)
{
tracer.Trace("4");
string Business_email = target.Contains(Model.Contact.ATTR_BUSINESSEMAIL) ? target.GetAttributeValue<string>(Model.Contact.ATTR_BUSINESSEMAIL) : string.Empty;
string FirstName = target.Contains(Model.Contact.ATTR_FIRSTNAME) ? target.GetAttributeValue<string>(Model.Contact.ATTR_FIRSTNAME) : string.Empty;
string LastName = target.Contains(Model.Contact.ATTR_LASTNAME) ? target.GetAttributeValue<string>(Model.Contact.ATTR_LASTNAME) : string.Empty;
EntityReference companyname = target.Contains(Model.Contact.ATTR_ACCOUNT) ? target.GetAttributeValue<EntityReference>(Model.Contact.ATTR_ACCOUNT) : null;
EntityCollection contact = GetContact(service, tracer, Business_email, FirstName, LastName,);
EntityCollection lead = GetLead(service, tracer, Business_email, FirstName, LastName,target.GetAttributeValue<EntityReference>(Model.Contact.ATTR_ACCOUNT).Name.ToString());
if (contact != null || lead!=null)
{
throw new InvalidPluginExecutionException("A record with the same values already exists. Please check your input and try again.");
}
}
//Retive the contact
private EntityCollection GetContact(IOrganizationService service, ITracingService tracer, string Business_email, string FirstName, string LastName, EntityReference companyname)
{
string Query = @"<fetch>
<entity name='contact'>
<attribute name='emailaddress1' />
<attribute name='firstname' />
<attribute name='lastname' />
<attribute name='parentcustomerid' />
<filter type='and'>
<condition attribute='firstname' operator='eq' value='" + FirstName + @"' />
<condition attribute='lastname' operator='eq' value='" + LastName + @"' />
<condition attribute='emailaddress1' operator='eq' value='" + Business_email + @"' />
<condition attribute='parentcustomerid' operator='eq' value='" + companyname.Id + @"' />
</filter>
</entity>
</fetch>";
EntityCollection result = service.RetrieveMultiple(new FetchExpression(Query));
if (result.Entities.Count > 0)
{
return result;
}
return null;
}
Retrive the lead
private EntityCollection GetLead(IOrganizationService service, ITracingService tracer, string Business_email, string FirstName, string LastName, string Companyname)
{
string Query = @"<fetch>" +
"<entity name='lead'>" +
"<attribute name='emailaddress1' />" +
"<attribute name='firstname' />" +
"<attribute name='lastname' />" +
"<attribute name='companyname' />" +
"<filter type='and'>" +
"<condition attribute='firstname' operator='eq' value='" + FirstName + "' />" +
"<condition attribute='lastname' operator='eq' value='" + LastName + "' />" +
"<condition attribute='emailaddress1' operator='eq' value='" + Business_email + "' />" +
"<condition attribute='companyname' operator='eq' value='" + Companyname + "' />" +
"</filter>" +
"</entity>" +
"</fetch>";
EntityCollection result = service.RetrieveMultiple(new FetchExpression(Query));
if (result.Entities.Count > 0)
{
return result;
}
return null;
}
}
}
Error:-