给定的密​​钥没有出现在字典插件CRM 2011在线

时间:2012-06-06 11:41:06

标签: dynamics-crm-2011

有人能告诉我我做错了什么,我现在已经尝试了一个多星期。

按照代码。

  

插件出现意外异常(执行):   Microsoft.Crm.Sdk.Samples.ProjectTotalAmount:   System.Collections.Generic.KeyNotFoundException:给定的键不是   出现在字典中。

namespace Microsoft.Crm.Sdk.Samples
{
    public class ProjectTotalAmount : IPlugin
    {

        public void Execute(IServiceProvider serviceProvider)
        {

            Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext) serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {

                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                //create a service context
                var ServiceContext = new OrganizationServiceContext(service);
                //ITracingService tracingService = localContext.TracingService;

                Entity entity = (Entity)context.InputParameters["Target"];


                if (entity.LogicalName == "new_project")
                {


                    Guid projectGUID = ((EntityReference)entity["new_project"]).Id;
                    Entity a = service.Retrieve("new_project", ((EntityReference)entity["new_project"]).Id, new ColumnSet(true));

                    decimal totalAmount = 0;

                    try
                    {
                        //fetchxml to get the sum total of estimatedvalue
                        string new_amount_sum = string.Format(@" 
                        <fetch distinct='false' mapping='logical' aggregate='true'> 
                            <entity name='new_projectitem'> 
                                <attribute name='new_amount' alias='new_amount' aggregate='sum' /> 
                                <filter type='and'>
                                    <condition attribute='new_projectid' operator='eq' value='{0}' uiname='' uitype='' />
                                </filter>
                            </entity>
                        </fetch>", a.Id);

                        EntityCollection new_amount_sum_result = service.RetrieveMultiple(new FetchExpression(new_amount_sum));

                        foreach (var c in new_amount_sum_result.Entities)
                        {
                            totalAmount = ((Money)((AliasedValue)c["new_amount_sum"]).Value).Value;
                        }

                        //updating the field on the account
                        Entity acc = new Entity("new_project");
                        acc.Id = a.Id;
                        acc.Attributes.Add("new_amount", new Money(totalAmount));
                        service.Update(acc);


                    }
                    catch (FaultException ex)
                    {
                        throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                    }
                }
            }

        }
    }   
}

插件的设置:

后验证 同步执行模式 服务器部署

1 个答案:

答案 0 :(得分:3)

在我们开始查看您的代码之前,有几点可以帮助您...

  • 此错误通常表示您的代码指的是不存在的属性(或没有值)
  • 您尚未说明您的插件注册了哪条消息。这可能会影响运行时的可用参数
  • 您已经注释掉了tracingService变量,但这可以帮助您至少了解您的代码到底有多远。恢复它并添加一些这样的行来跟踪失败前的进度。此信息将写入客户端异常对话框中提供给您的错误日志中:

    tracingService.Trace("Project Id is {0}", projectGUID);` 
    

    tracingService.Trace("Number of returned records: {0}", new_amount_sum_result.Entities.Count);`
  • 以下行似乎完全是多余的,因为您只使用Id中的a属性,而且这个属性已经存在为entity.Id

    Entity a = service.Retrieve("new_project", ((EntityReference)entity["new_project"]).Id, new ColumnSet(true));