CRM Dynamics 插件代码问题 2015
CRM Dynamics plugin code issue 2015
我正在尝试为 Microsoft CRM Dynamics 2015(版本 7.0.2)创建一个插件,但它不起作用。
该插件将 运行 在创建案例时尝试从帐户中恢复合同信息。
我有以下代码,它不起作用(合同信息未保存在 case/incident 上)。
编辑
我更新了我的代码,现在它失败了,但它没有在系统作业或当我使用插件注册工具进行调试时给出明确的错误。
系统作业错误消息:System.ServiceModel.QuotaExceededException:Microsoft Dynamics CRM 遇到错误。管理员或支持参考号:#1C10449A
使用 Visual Studio 和插件注册工具进行调试,一切顺利,直到它尝试调用 Update 方法,它说事件不存在,但由于它是一个同步插件,事件不存在已创建(获取日志以便能够调试我发现的唯一方法)。
public class CaseContractFill : IPlugin
{
Contract checkForContract(Guid accountId, IOrganizationService service)
{
QueryExpression accountContractQuery = new QueryExpression
{
EntityName = Contract.EntityLogicalName,
ColumnSet = new ColumnSet(true),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression
{
AttributeName = "accountid",
Operator = ConditionOperator.Equal,
Values = { accountId }
}
}
}
};
DataCollection<Entity> accountContracts = service.RetrieveMultiple(accountContractQuery).Entities;
//Check if account has a contract and return it
if (accountContracts.Count > 0)
{
return (Contract)accountContracts[0];
}
//Retrieves account
Account account = (Account)service.Retrieve(Account.EntityLogicalName, accountId, new ColumnSet(true));
//Check if account has a parent and call this method again with that parent
if (account.ParentAccountId != null)
{
checkForContract(account.ParentAccountId.Id, service);
}
//If no Contract and/or no Parent returns null
return null;
}
ContractDetail checkForContractLine(Guid contractId, IOrganizationService service)
{
QueryExpression accountContractQuery = new QueryExpression
{
EntityName = ContractDetail.EntityLogicalName,
ColumnSet = new ColumnSet(true),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression
{
AttributeName = "contractid",
Operator = ConditionOperator.Equal,
Values = { contractId }
}
}
}
};
DataCollection<Entity> contractLines = service.RetrieveMultiple(accountContractQuery).Entities;
return (ContractDetail)contractLines[0];
}
public void Execute(IServiceProvider serviceProvider)
{
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);
try
{
Entity entity = (Entity)context.InputParameters["Target"];
Incident incident = (Incident)entity.ToEntity<Incident>();
Contract contract = checkForContract(incident.CustomerId.Id, service);
if (contract != null)
{
incident.ContractId = contract.ToEntityReference();
ContractDetail contractLine = checkForContractLine(contract.Id, service);
incident.ContractDetailId = contractLine.ToEntityReference();
service.Update(incident);
}
}
catch (Exception e)
{
throw new InvalidPluginExecutionException(e.Message);
}
}
}
"ContractId" 分配不正确。
if (contract != null)
{
incident.ContractId = contract.ToEntityReference();
service.Update(incident);
}
当您通过 IOrganizationService.Retrieve
获取实体时,account.contract_customer_accounts
将为空,就像所有其他关系一样。
您可以单独调用该服务以使用 RetrieveMultiple 获取相关合同,或者您可以考虑使用 OrganizationServiceContext。
我正在尝试为 Microsoft CRM Dynamics 2015(版本 7.0.2)创建一个插件,但它不起作用。 该插件将 运行 在创建案例时尝试从帐户中恢复合同信息。
我有以下代码,它不起作用(合同信息未保存在 case/incident 上)。
编辑
我更新了我的代码,现在它失败了,但它没有在系统作业或当我使用插件注册工具进行调试时给出明确的错误。
系统作业错误消息:System.ServiceModel.QuotaExceededException:Microsoft Dynamics CRM 遇到错误。管理员或支持参考号:#1C10449A
使用 Visual Studio 和插件注册工具进行调试,一切顺利,直到它尝试调用 Update 方法,它说事件不存在,但由于它是一个同步插件,事件不存在已创建(获取日志以便能够调试我发现的唯一方法)。
public class CaseContractFill : IPlugin
{
Contract checkForContract(Guid accountId, IOrganizationService service)
{
QueryExpression accountContractQuery = new QueryExpression
{
EntityName = Contract.EntityLogicalName,
ColumnSet = new ColumnSet(true),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression
{
AttributeName = "accountid",
Operator = ConditionOperator.Equal,
Values = { accountId }
}
}
}
};
DataCollection<Entity> accountContracts = service.RetrieveMultiple(accountContractQuery).Entities;
//Check if account has a contract and return it
if (accountContracts.Count > 0)
{
return (Contract)accountContracts[0];
}
//Retrieves account
Account account = (Account)service.Retrieve(Account.EntityLogicalName, accountId, new ColumnSet(true));
//Check if account has a parent and call this method again with that parent
if (account.ParentAccountId != null)
{
checkForContract(account.ParentAccountId.Id, service);
}
//If no Contract and/or no Parent returns null
return null;
}
ContractDetail checkForContractLine(Guid contractId, IOrganizationService service)
{
QueryExpression accountContractQuery = new QueryExpression
{
EntityName = ContractDetail.EntityLogicalName,
ColumnSet = new ColumnSet(true),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression
{
AttributeName = "contractid",
Operator = ConditionOperator.Equal,
Values = { contractId }
}
}
}
};
DataCollection<Entity> contractLines = service.RetrieveMultiple(accountContractQuery).Entities;
return (ContractDetail)contractLines[0];
}
public void Execute(IServiceProvider serviceProvider)
{
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);
try
{
Entity entity = (Entity)context.InputParameters["Target"];
Incident incident = (Incident)entity.ToEntity<Incident>();
Contract contract = checkForContract(incident.CustomerId.Id, service);
if (contract != null)
{
incident.ContractId = contract.ToEntityReference();
ContractDetail contractLine = checkForContractLine(contract.Id, service);
incident.ContractDetailId = contractLine.ToEntityReference();
service.Update(incident);
}
}
catch (Exception e)
{
throw new InvalidPluginExecutionException(e.Message);
}
}
}
"ContractId" 分配不正确。
if (contract != null)
{
incident.ContractId = contract.ToEntityReference();
service.Update(incident);
}
IOrganizationService.Retrieve
获取实体时,account.contract_customer_accounts
将为空,就像所有其他关系一样。
您可以单独调用该服务以使用 RetrieveMultiple 获取相关合同,或者您可以考虑使用 OrganizationServiceContext。