CRM Dynamics插件代码问题2015

时间:2016-07-25 18:53:44

标签: c# visual-studio-2013 dynamics-crm-2015

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

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

修改

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

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

2 个答案:

答案 0 :(得分:0)

“ContractId”分配错误。

if (contract != null)
{
  incident.ContractId = contract.ToEntityReference();
  service.Update(incident);
}

答案 1 :(得分:0)

当您通过account.contract_customer_accounts获取实体时,

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

您可以单独调用服务以使用RetrieveMultiple获取相关合同,或者您可以查看使用OrganizationServiceContext