none
PlugIn : 创建回款(Collection)记录,跟新订单(Order)信息 提示"The Given key was not present in the dictionary" RRS feed

  • 问题

  • "The Given key was not present in the dictionary" 真不知道为什么,查了半天也不知道什么地方写错了为。
    帮忙查一下,谢谢

    namespace CRM4.Plugin.SetPaymentToAmount
    {
        using Microsoft.Crm.Sdk;
        using Microsoft.Crm.Sdk.Query;
        using Microsoft.Crm.SdkTypeProxy;
        using Microsoft.Win32;
        using System;
        using System.Configuration;
        using System.IO;
        using System.Net;
        using System.Web.Services.Protocols;
        using System.Xml;
    
        public class SetPaymentToAmount : IPlugin
        {
            public void Execute(IPluginExecutionContext context)
            {
                try
                {
                    Guid id;
                    int stage;
                    if (context.Depth > 1)
                    {
                        return;
                    }
                    DynamicEntity salesOrderCollection = null;
                    DynamicEntity entity = null;
                    DynamicEntity entity3 = null;
                    Moniker moniker = null;
                    CrmService crmService = GetCrmService(context);
                    ColumnSet columnSet = new ColumnSet(new string[] { "new_collectionid", "new_contractnumber", "new_ordernameid", "new_ordernumber", "new_planingrepaymentamount", "new_planingrepaymentdate", "new_repaymentamount", "new_repaymentdate" });
    
    
                    if (context.InputParameters.Properties.Contains("Target") &&
                    context.InputParameters.Properties["Target"] is DynamicEntity)
                    {
                        DynamicEntity entity0 = (DynamicEntity)context.InputParameters.Properties["Target"];
    
                        if (entity0.Name == "new_collection")
                        {
                            id = (Guid)context.OutputParameters["id"];
                            salesOrderCollection = GetSalesOrderCollection(crmService, id, columnSet);
                            entity3 = SetSalesOrderTotalAmounts(crmService, salesOrderCollection);
                            UpdateDynamicEntity(crmService, entity3);
                        }
                    }
                    string messageName = context.MessageName;
                    if (messageName != null)
                    {
                        if (!(messageName == "Create"))
                        {
                            if (messageName == "Update")
                            {
                                goto Label_024C;
                            }
                            if (messageName == "Delete")
                            {
                                goto Label_0369;
                            }
                            if (messageName == "SetStateDynamicEntity")
                            {
                                goto Label_0476;
                            }
                        }
                        else
                        {
                            if (!context.InputParameters.Properties.Contains("Target") || !(context.InputParameters["Target"] is DynamicEntity))
                            {
                                return;
                            }
                            entity = (DynamicEntity)context.InputParameters["Target"];
                            if (entity.Name != "new_collection")
                            {
                                return;
                            }
                            stage = context.Stage;
                            if ((stage != 10) && (stage == 50))
                            {
                                id = (Guid)context.OutputParameters["id"];
                                salesOrderCollection = GetSalesOrderCollection(crmService, id, columnSet);
                                entity3 = SetSalesOrderTotalAmounts(crmService, salesOrderCollection);
                                UpdateDynamicEntity(crmService, entity3);
                            }
                        }
                    }
                Label_024C:
                    if (!context.InputParameters.Properties.Contains("Target") || !(context.InputParameters["Target"] is DynamicEntity))
                    {
                        return;
                    }
                    entity = (DynamicEntity)context.InputParameters["Target"];
                    if (entity.Name != "new_collection")
                    {
                        return;
                    }
                    id = ((Key)entity["new_collectionid"]).Value;
                    salesOrderCollection = GetSalesOrderCollection(crmService, id, columnSet);
                    stage = context.Stage;
                    if ((stage != 10) && (stage == 50))
                    {
                        entity3 = SetSalesOrderTotalAmounts(crmService, salesOrderCollection);
                        UpdateDynamicEntity(crmService, entity3);
                    }
                Label_0369:
                    if (!context.InputParameters.Properties.Contains("Target") || !(context.InputParameters["Target"] is Moniker))
                    {
                        return;
                    }
                    moniker = (Moniker)context.InputParameters["Target"];
                    if (moniker.Name != "new_collection")
                    {
                        return;
                    }
                    switch (context.Stage)
                    {
                        case 10:
                            id = moniker.Id;
                            salesOrderCollection = GetSalesOrderCollection(crmService, id, columnSet);
                            context.SharedVariables.Properties.Add(new PropertyBagEntry("new_collection", salesOrderCollection));
                            return;
                        case 50:
                            if (context.SharedVariables.Contains("new_collection"))
                            {
                                salesOrderCollection = (DynamicEntity)context.SharedVariables["new_collection"];
                                entity3 = SetSalesOrderTotalAmounts(crmService, salesOrderCollection);
                                UpdateDynamicEntity(crmService, entity3);
                            }
                            return;
                        default:
                            return;
                    }
                Label_0476:
                    if (!context.InputParameters.Properties.Contains("entityMoniker") || !(context.InputParameters["entityMoniker"] is Moniker))
                    {
                        return;
                    }
                    moniker = (Moniker)context.InputParameters["entityMoniker"];
                    if (moniker.Name != "new_collection")
                    {
                        return;
                    }
                    id = moniker.Id;
                    salesOrderCollection = GetSalesOrderCollection(crmService, id, columnSet);
                    stage = context.Stage;
                    if ((stage != 10) && (stage == 50))
                    {
                        entity3 = SetSalesOrderTotalAmounts(crmService, salesOrderCollection);
                        UpdateDynamicEntity(crmService, entity3);
                    }
                   
                }
                catch (SoapException exception)
                {
                    throw new InvalidPluginExecutionException("An error occurred in the Execute plugin.", exception);
                }
            }
    
            private static DynamicEntity SetSalesOrderTotalAmounts(CrmService crmService, DynamicEntity collection)
            {
                DynamicEntity entity4;
                try
                {
                    CrmMoneyProperty property = new CrmMoneyProperty("new_repaymentamount", new CrmMoney(0M));
                    Guid salesOrderNumber = GetLookupProperty(collection, "new_ordernameid").Value.Value;
                    ColumnSet columnSet = new ColumnSet(new string[] { "new_repaymentamount" });
                    DynamicEntity entity = GetSalesOrder(crmService, salesOrderNumber, columnSet);
                    QueryByAttribute attribute = new QueryByAttribute();
                    attribute.ColumnSet = new ColumnSet(new string[] { "new_repaymentamount" });
                    attribute.EntityName = "new_collection";
                    attribute.Attributes = new string[] { "new_ordernameid" };
                    attribute.Values = new string[] { salesOrderNumber.ToString() };
                    RetrieveMultipleRequest request = new RetrieveMultipleRequest();
                    request.Query = attribute;
                    request.ReturnDynamicEntities = true;
                    RetrieveMultipleResponse response = (RetrieveMultipleResponse)crmService.Execute(request);
                    foreach (BusinessEntity entity2 in response.BusinessEntityCollection.BusinessEntities)
                    {
                        DynamicEntity entity3 = (DynamicEntity)entity2;
                        CrmMoneyProperty crmMoneyProperty = GetCrmMoneyProperty(entity3, "new_repaymentamount");
                        CrmMoney money1 = property.Value;
                        money1.Value += crmMoneyProperty.Value.Value;
                    }
                    if (entity.Properties.Contains("new_repaymentamount"))
                    {
                        ((CrmMoney)entity.Properties["new_repaymentamount"]).Value = property.Value.Value;
                    }
                    else
                    {
                        entity.Properties.Add(property);
                    }
                    entity4 = entity;
                }
                catch (SoapException exception)
                {
                    throw new InvalidPluginExecutionException("An error occurred in the SetSalesOrderTotalAmounts plugin.", exception);
                }
                return entity4;
            }
    
            public static LookupProperty GetLookupProperty(DynamicEntity entity, string propertyName)
            {
                if (entity.Properties.Contains(propertyName))
                {
                    return new LookupProperty(propertyName, (Lookup)entity[propertyName]);
                }
                return new LookupProperty();
            }
    
            public static CrmMoneyProperty GetCrmMoneyProperty(DynamicEntity entity, string propertyName)
            {
                if (entity.Properties.Contains(propertyName))
                {
                    return new CrmMoneyProperty(propertyName, (CrmMoney)entity[propertyName]);
                }
                return new CrmMoneyProperty(propertyName, new CrmMoney(0M));
            }
    
            public static DynamicEntity GetSalesOrderCollection(CrmService crmService, Guid salesOrderCollectionId, ColumnSet columnSet)
            {
                DynamicEntity entity;
                try
                {
                    QueryByAttribute attribute = new QueryByAttribute();
                    attribute.ColumnSet = columnSet;
                    attribute.EntityName = "new_Collection";
                    attribute.Attributes = new string[] { "new_ordernameid" };
                    attribute.Values = new string[] { salesOrderCollectionId.ToString() };
                    RetrieveMultipleRequest request = new RetrieveMultipleRequest();
                    request.Query = attribute;
                    request.ReturnDynamicEntities = true;
                    RetrieveMultipleResponse response = (RetrieveMultipleResponse)crmService.Execute(request);
                    entity = (DynamicEntity)response.BusinessEntityCollection.BusinessEntities[0];
                }
                catch (SoapException exception)
                {
                    throw new InvalidPluginExecutionException("An error occurred in the GetSalesOrderCollection plugin.", exception);
                }
                return entity;
            }
    
            public static DynamicEntity GetSalesOrder(CrmService crmService, Guid salesOrderNumber, ColumnSet columnSet)
            {
                DynamicEntity entity;
                try
                {
                    QueryByAttribute attribute = new QueryByAttribute();
                    attribute.ColumnSet = columnSet;
                    attribute.EntityName = "salesorder";
                    attribute.Attributes = new string[] { "salesorderid" };
                    attribute.Values = new string[] { salesOrderNumber.ToString() };
                    RetrieveMultipleRequest request = new RetrieveMultipleRequest();
                    request.Query = attribute;
                    request.ReturnDynamicEntities = true;
                    RetrieveMultipleResponse response = (RetrieveMultipleResponse)crmService.Execute(request);
                    entity = (DynamicEntity)response.BusinessEntityCollection.BusinessEntities[0];
                }
                catch (SoapException exception)
                {
                    throw new InvalidPluginExecutionException("An error occurred in the GetSalesOrder plugin.", exception);
                }
                return entity;
            }
    
            public static void UpdateDynamicEntity(CrmService crmService, DynamicEntity entity)
            {
                try
                {
                    TargetUpdateDynamic dynamic = new TargetUpdateDynamic();
                    dynamic.Entity = entity;
                    UpdateRequest request = new UpdateRequest();
                    request.Target = dynamic;
                    UpdateResponse response = (UpdateResponse)crmService.Execute(request);
                }
                catch (SoapException exception)
                {
                    throw new InvalidPluginExecutionException("An error occurred in the UpdateDynamicEntity plugin.", exception);
                }
            }
    
            public static CrmService GetCrmService(IPluginExecutionContext context)
            {
                CrmService service2;
                try
                {
                    CrmAuthenticationToken token = new CrmAuthenticationToken();
                    token.AuthenticationType = 0;
                    token.OrganizationName = "CRM";
                    token.CallerId = context.UserId;
                    CorrelationToken token2 = new CorrelationToken();
                    token2.CorrelationId = context.CorrelationId;
                    token2.Depth = context.Depth;
                    token2.CorrelationUpdatedTime = context.CorrelationUpdatedTime;
                    CrmService service = new CrmService();
                    service.Url = "http://localhost/MSCrmServices/2007/CrmService.asmx";
                    service.CrmAuthenticationTokenValue = token;
                    service.CorrelationTokenValue = token2;
                    service.UseDefaultCredentials = true;
                    service.UnsafeAuthenticatedConnectionSharing = true;
                    service2 = service;
                }
                catch (SoapException exception)
                {
                    throw new InvalidPluginExecutionException("An error occurred in the GetCrmService plugin.", exception);
                }
                return service2;
            }
        }
    }
    

    要能放下,才能提起。提放自如,是自在人。
    • 已编辑 ckumark 2009年6月26日 19:16 Fixing thread title bug
    2009年6月23日 6:31

答案

  • 查找到代码错误了,是同一GUID的account获取错误


    要能放下,才能提起。提放自如,是自在人。
    • 已标记为答案 Harlan Liu 2009年6月26日 1:20
    2009年6月26日 1:20

全部回复

  • 各位帮忙看看,现在只能用工作流将就用着,但工作流的达不到及时
    要能放下,才能提起。提放自如,是自在人。
    2009年6月23日 6:48
  • 查找到代码错误了,是同一GUID的account获取错误


    要能放下,才能提起。提放自如,是自在人。
    • 已标记为答案 Harlan Liu 2009年6月26日 1:20
    2009年6月26日 1:20