简体   繁体   English

CRM Dynamics插件代码问题2015

[英]CRM Dynamics plugin code issue 2015

I'm trying to create a plugin for Microsoft CRM Dynamics 2015 (version 7.0.2), but it's not working. 我正在尝试为Microsoft CRM Dynamics 2015(版本7.0.2)创建一个插件,但无法正常工作。 The plugin will run when a case is created and will attemp to recover the Contract information from the account. 该插件将在创建案例后运行,并将尝试从帐户中恢复合同信息。

I have the following code, it's not working(contract information is not saved on the case/incident). 我有以下代码,它不起作用(合同信息未保存在案例/事件中)。

Edit 编辑

I update my code, now it fails, but it doesn't give a clear error on the system job or when i debug using the Plugin Registration Tool. 我更新了代码,但现在失败了,但是在系统作业上或使用插件注册工具进行调试时,并没有给出明确的错误。

System job error message: System.ServiceModel.QuotaExceededException: Microsoft Dynamics CRM has experienced an error. 系统作业错误消息:System.ServiceModel.QuotaExceededException:Microsoft Dynamics CRM遇到错误。 Reference number for administrators or support: #1C10449A 管理员或支持者的参考编号:#1C10449A

Debuging with Visual Studio and the Plugin Registration Tool it goes well until it tries to call the Update method, where it says that the incident does not exist, but since it was a syncrhonous plugin the incident was not created(only way to get the log to be able to debug that i found). 使用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" is being incorrectly assigned. 错误分配了“ ContractId”。

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

account.contract_customer_accounts will be empty when you fetch the entity via IOrganizationService.Retrieve , just as all other relationships will be. 当您通过IOrganizationService.Retrieve获取实体时, account.contract_customer_accounts将为空,就像所有其他关系一样。

You could make a separate call to the service to fetch the related contracts with RetrieveMultiple, or you could look into using the OrganizationServiceContext . 您可以单独调用该服务以使用RetrieveMultiple获取相关合同,也可以考虑使用OrganizationServiceContext

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM