CRM Dynamics插件代码发布2015

本文关键字:2015 代码 Dynamics 插件 CRM | 更新日期: 2023-09-27 17:58:36

我正在尝试为Microsoft CRM Dynamics 2015(版本7.0.2)创建一个插件,但它不起作用。该插件将在创建案例时运行,并尝试从帐户中恢复合同信息。

我有以下代码,它不起作用(合同信息没有保存在案例/事件中)。

编辑

我更新了我的代码,现在它失败了,但在系统作业或使用插件注册工具调试时,它没有给出明显的错误。

系统作业错误消息:System.ServiceModel.OQuotaExceededException: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);
        }
    }
}

CRM Dynamics插件代码发布2015

"ContractId"的分配不正确。
if (contract != null)
{
  incident.ContractId = contract.ToEntityReference();
  service.Update(incident);
}
当您通过IOrganizationService.Retrieve获取实体时,

account.contract_customer_accounts将为空,就像所有其他关系一样。

您可以使用RetrieveMultiple对服务进行单独调用以获取相关约定,也可以使用OrganizationServiceContext进行研究。