Thanks Alex. I tried that option as well. I was getting "The record type 1 is not defined for use with the connection role" error but when i was debugging both the plug-ins it doesn't show error once and doesn't create the record that it needs to create. Here is the generic code that i am using and registered on Lead Create and Connection Create plug-ins. Can you suggest any other alternatives or some thing wrong with this approach that i followed?
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;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.Web;
using System.Xml;
using Microsoft.Crm.Sdk.Messages;
namespace LeadConnection
{
public class LeadConnection : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
Entity entity = (Entity)context.InputParameters["Target"];
string connectionCheck = string.Empty;
if (entity.Attributes.Contains("sm_createconnectioncheck"))
{
connectionCheck = entity.Attributes["sm_createconnectioncheck"].ToString();
}
if (entity.LogicalName == "lead")
{
CreateDefaultConnection(service, tracingService, context, entity);
}
else if (entity.LogicalName == "connection" && connectionCheck != "No")
{
CreateConnection(service, tracingService, context, entity);
}
}
public void CreateDefaultConnection(IOrganizationService service, ITracingService tracingService, IPluginExecutionContext context, Entity entity)
{
try
{
string message = context.MessageName.ToLower();
string amrAccountUser = string.Empty;
if (entity.LogicalName == "lead")
amrAccountUser = entity.Attributes["sm_clientgroup"].ToString();
else if (entity.LogicalName == "opportunity")
amrAccountUser = entity.Attributes["sm_clientgroup_opp"].ToString();
Guid contactid = ((EntityReference)entity.Attributes["parentcontactid"]).Id;
if (contactid == null)
return;
if (amrAccountUser == "BGT" && (message == "create"))
{
Guid id = Guid.NewGuid();
if (entity.LogicalName == "lead")
id = (Guid)entity.Attributes["leadid"];
else if (entity.LogicalName == "opportunity")
id = (Guid)entity.Attributes["opportunityid"];
QueryExpression query = new QueryExpression("contact");
EntityCollection Result = null;
var columnNames = new[] { "fullname", "emailaddress1" };
FilterExpression filterExpression = new FilterExpression(LogicalOperator.And);
filterExpression.AddCondition("contactid", ConditionOperator.Equal, contactid);
query.ColumnSet.AddColumns(columnNames);
query.Criteria = filterExpression;
query.LinkEntities.Add(new LinkEntity("contact", "account", "parentcustomerid", "accountid", JoinOperator.LeftOuter));
query.LinkEntities[0].Columns.AddColumn("psm_accounttype");
query.LinkEntities[0].Columns.AddColumn("accountid");
query.LinkEntities[0].Columns.AddColumn("accountnumber");
Result = service.RetrieveMultiple(query);
string accountType = Result.Entities[0].FormattedValues["account1.psm_accounttype"].ToString();
string associatedAccount = "";
if (Result != null && Result.Entities.Any())
associatedAccount = ((AliasedValue)Result.Entities[0]["account1.accountid"]).Value.ToString();
else
return;
Guid accountid = new Guid(associatedAccount);
string connectionRoleValue = "";
if (accountType == "Test Broker")
{
connectionRoleValue = "Test Broker 1111";
}
else if (accountType == "Test Broker2")
{
connectionRoleValue = "Test Broker 22222";
}
else
{
connectionRoleValue = "Default";
}
EntityCollection connectionResult = GetConnectionRole(service, connectionRoleValue);
if (connectionResult == null || !connectionResult.Entities.Any())
return;
Entity connection = new Entity();
connection.LogicalName = "connection";
if (entity.LogicalName == "lead")
connection.Attributes.Add("record1id", new EntityReference("lead", id));
else if (entity.LogicalName == "opportunity")
connection.Attributes.Add("record1id", new EntityReference("opportunity", id));
connection.Attributes.Add("record2id", new EntityReference("account", accountid));
connection.Attributes.Add("record2roleid", new EntityReference("connectionrole", new Guid(connectionResult.Entities[0].Attributes["connectionroleid"].ToString())));
connection.Attributes.Add("sm_createconnectioncheck", "No");
service.Create(connection);
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
tracingService.Trace("Error Occurred in Create Lead Connection Plugin: " + ex.Message);
throw new InvalidPluginExecutionException("An error occurred in the Lead Connection Plugin.", ex);
}
catch (Exception ex)
{
tracingService.Trace("Error Occurred in Create Lead Connection :", ex);
throw;
}
}
private EntityCollection GetConnectionRole(IOrganizationService service, string connectionRoleValue)
{
//Retrieve Connection Role Guids...
QueryExpression queryConnectionRole = new QueryExpression("connectionrole")
{
ColumnSet = new ColumnSet("connectionroleid", "name"),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression
{
AttributeName = "name",
Operator = ConditionOperator.Equal,
Values = { connectionRoleValue }
}
}
}
};
EntityCollection connectionResult = service.RetrieveMultiple(queryConnectionRole);
return connectionResult;
}
public void CreateConnection(IOrganizationService service, ITracingService tracingService, IPluginExecutionContext context, Entity entity)
{
try
{
string message = context.MessageName.ToLower();
string amrAccountUser = string.Empty;
if (message == "create")
{
Guid record2Id = ((Microsoft.Xrm.Sdk.EntityReference)(entity.Attributes["record2id"])).Id;
Guid record1Id = ((Microsoft.Xrm.Sdk.EntityReference)(entity.Attributes["record1id"])).Id;
QueryExpression query = new QueryExpression("contact");
EntityCollection Result = null;
var columnNames = new[] { "fullname", "emailaddress1" };
FilterExpression filterExpression = new FilterExpression(LogicalOperator.And);
filterExpression.AddCondition("contactid", ConditionOperator.Equal, record1Id);//record1Id Contact Id that is selected in lookup.
query.ColumnSet.AddColumns(columnNames);
query.Criteria = filterExpression;
query.LinkEntities.Add(new LinkEntity("contact", "account", "parentcustomerid", "accountid", JoinOperator.LeftOuter));
query.LinkEntities[0].Columns.AddColumn("accountid");
query.LinkEntities[0].Columns.AddColumn("accountnumber");
Result = service.RetrieveMultiple(query);
//string associatedAccount = ((AliasedValue)Result.Entities[0]["account1.accountid"]).Value.ToString();//retrive the associated account to the selected contact.
string associatedAccount = "";
if (Result != null && Result.Entities.Any())
{
associatedAccount = ((AliasedValue)Result.Entities[0]["account1.accountid"]).Value.ToString();
}
else
return;
Guid accountid = new Guid(associatedAccount);
//Retrieve the selected connection role..
Entity connectionEntity = new Entity();
var conRoleColumns = new[] { "name" };
ColumnSet connColumns = new ColumnSet(conRoleColumns);
connectionEntity = service.Retrieve("connectionrole", ((Microsoft.Xrm.Sdk.EntityReference)(entity.Attributes["record1roleid"])).Id, connColumns);
string connectionRoleValue = connectionEntity.Attributes["name"].ToString(); //Connection role referenced entity. Role value doesn't exist in Connection so retrieve with additional call.
if (connectionRoleValue == "Testing Locatoin1")
{
connectionRoleValue = "Testing Main Location1";
}
else if (connectionRoleValue == "Test Locatio2")
{
connectionRoleValue = "Testing Main Location2";
}
EntityCollection connectionResult = GetConnectionRole(service, connectionRoleValue);
if (connectionResult == null || !connectionResult.Entities.Any())
return;
Entity connection = new Entity();
connection.LogicalName = "connection";
//Dynamics check to create the connection for Opportunity or Lead based on where the connection is selectd.
if (((Microsoft.Xrm.Sdk.EntityReference)(entity.Attributes["record2id"])).LogicalName == "opportunity")
connection.Attributes.Add("record1id", new EntityReference("opportunity", record2Id));
else if (((Microsoft.Xrm.Sdk.EntityReference)(entity.Attributes["record2id"])).LogicalName == "lead")
connection.Attributes.Add("record1id", new EntityReference("lead", record2Id));
connection.Attributes.Add("record2id", new EntityReference("account", accountid));
connection.Attributes.Add("record2roleid", new EntityReference("connectionrole", new Guid(connectionResult.Entities[0].Attributes["connectionroleid"].ToString())));
service.Create(connection);
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
tracingService.Trace("Error Occurred in Create Connection Plugin: " + ex.Message);
throw new InvalidPluginExecutionException("An error occurred in the Lead Connection Plugin.", ex);
}
catch (Exception ex)
{
tracingService.Trace("Error Occurred in Create Connection Connection :", ex);
throw;
}
}
public static void EnablePlugin(IOrganizationService orgService, bool enable)
{
var messapgeProcessQE = new QueryExpression("sdkmessageprocessingstep");
messapgeProcessQE.ColumnSet.AddColumns("sdkmessageprocessingstepid", "name");
EntityCollection obj = orgService.RetrieveMultiple(messapgeProcessQE);
int pluginStateCode = enable ? 0 : 1;
int pluginStatusCode = enable ? 1 : 2;
//Connection Step
Guid stepId = new Guid("cdcd5b74-ea0c-e811-80f1-3863bb34ecf0");
orgService.Execute(new SetStateRequest
{
EntityMoniker = new EntityReference("sdkmessageprocessingstep", stepId),
State = new OptionSetValue(pluginStateCode),
Status = new OptionSetValue(pluginStatusCode)
});
}
}
}