【问题标题】:Create ActivityParty in CRM without early bound Entities在没有早期绑定实体的 CRM 中创建 ActivityParty
【发布时间】:2014-06-16 19:54:26
【问题描述】:

作为一项要求,我不能使用使用“CrmSvcUtil”创建的早期绑定上下文。问题是一个新的电话活动需要两个字段('from'和'to'),它们是Entities,类型为activityparty。标准 XRM/CRM 命名空间不包含类似于使用实用程序创建的ActivityParty 的类。

我尝试用 EntityCollection 填充它,但该字段将为空。接下来,我尝试重新创建有效的电话活动的结构。 EntityCollection "activityparty" -> 带有一个 Entity "activityparty" -> 带有 EntityReference 属性 "partyid" ->实体参考(例如“联系人”和联系人的 ID)。但它根本不起作用。

如何使用“普通”Entitiy 类创建 ActivityParty(或更好的电话活动)?

【问题讨论】:

    标签: c# dynamics-crm


    【解决方案1】:

    我投票给 TrN 是因为我正在寻找任何类型的示例,这是我能找到的唯一早期绑定示例。

    他的示例实际上帮助我创建了一个 PhoneCall 实体,该实体的属性“From”指向实际拨打电话的潜在客户。我从来没有完全理解IEnumerable<ActivityParty> 枚举器。多亏了 TrN,我理解它足以使用它。

    这是我测试过的关于 PhoneCall 活动 的代码,它可以工作。每次现有潜在客户来电。 PhoneCall 活动将使用链接到正确潜在客户的正确属性值保存。

    IEnumerable<ActivityParty> party = new[] { new ActivityParty { LogicalName = ActivityParty.EntityLogicalName , PartyId = eref2  } };
    
    
                        Console.WriteLine("Logging activity to {0}", firstName);
                        Console.WriteLine("... \n" );
                        PhoneCall newCall = new PhoneCall { Description = "Missed phone call from this lead", DirectionCode = true, RegardingObjectId = eref2,
                            Subject = "Missed Call", PhoneNumber = MissedCall, OwnerId = User, From = party };
    
    
    
                        Guid newCallId = service.Create(newCall);
    
                    Console.WriteLine("Log successfully created \n \n ");
    

    正如我所说,对于 Kirschi 来说,考虑到他没有任何上下文的要求,这不是真正的解决方案。但是任何想要/可以使用提供的上下文并且好奇IEnumerable&lt;ActivityParty&gt; 是如何工作的人,这可能会帮助他们创建一个适当的电话呼叫活动。

    【讨论】:

      【解决方案2】:

      这是相同的工作代码。如果有人遇到任何问题,请随时与我们联系。

      private static void fetchRelatedPhoneCalls(IPluginExecutionContext context, IOrganizationService service, Guid yourGuid, Entity opp)
      {
          string strFetchPhoneCalls = string.Format(FetchQuery.bringFetchQueryForPhoneCalls(),yourGuid);
          EntityCollection entPhoneCalls = (EntityCollection)service.RetrieveMultiple(new FetchExpression(strFetchPhoneCalls));
      
          if (entPhoneCalls != null && entPhoneCalls.Entities.Count > 0)
          {
              for (int i = 0; i < entPhoneCalls.Entities.Count; i++)
              {
                  Entity entPhoneCall = (Entity)entPhoneCalls.Entities[i];
      
                  string[] strAttributesPCtoRemove = new string[] { "createdon", "createdbyname", "createdby"
                                  ,"modifiedon", "modifiedby" ,"regardingobjectid","owninguser"
                                  ,"activityid", "instancetypecode", "activitytypecode" // PhoneCall Skip
                  };
      
                  Entity entNewPhoneCall = this.CloneRecordForEntity("phonecall", entPhoneCall, strAttributesPCtoRemove);
                  entNewPhoneCall["regardingobjectid"] = new EntityReference(context.PrimaryEntityName, context.PrimaryEntityId);
                  entNewPhoneCall["to"] = this.getActivityObject(entNewPhoneCall, "to");
                  entNewPhoneCall["from"] = this.getActivityObject(entNewPhoneCall, "from");
      
                  service.Create(entNewPhoneCall);
              }
          }
      }
      
      private static Entity CloneRecordForEntity(string targetEntityName, Entity sourceEntity, string[] strAttributestoRemove)
      {
          Entity clonedEntity = new Entity(targetEntityName);
          AttributeCollection attributeKeys = sourceEntity.Attributes;
          foreach (string key in attributeKeys.Keys)
          {
              if (Array.IndexOf(strAttributestoRemove, key) == -1)
              {
                  if (!clonedEntity.Contains(key))
                  {
                      clonedEntity[key] = sourceEntity[key];
                  }
              }
          }
          return clonedEntity;
      }
      
      private static EntityCollection getActivityObject(Entity entNewActivity, string activityFieldName)
      {
          Entity partyToFrom = new Entity("activityparty");
          partyToFrom["partyid"] = ((EntityReference)((EntityCollection)entNewActivity[activityFieldName]).Entities[0].Attributes["partyid"]);
      
          EntityCollection toFrom = new EntityCollection();
          toFrom.Entities.Add(partyToFrom);
      
          return toFrom;
      }
      

      【讨论】:

        【解决方案3】:

        即使我赞成答案,但我在 ActivityParty 的序列化方面遇到了类似的问题。我找到了不需要您放弃早期绑定实体的解决方案。

        你需要做的是这样的:

        IEnumerable<ActivityParty> party = new [] { new ActivityParty { PartyId="", EntityLogicalName="..." } }; 
        phonecall["to"] = new EntityCollection(party.Select(x => x.ToEntity<Entity>).ToList());
        

        (我没有测试代码,是从空中写的,但你应该感觉到这个想法)

        【讨论】:

        • 感谢您的支持 :) 在您的代码中,您混合了早期和晚期绑定。有关两种样式未混合的完整示例,您可以查看我的博客:crmanswers.net/2014/08/dynamics-crm-special-data-types.html
        • 是的,因为 phonecall.To 属于 IEnumerable 类型,这会导致序列化问题(我有一些错误:内部异常:System.Runtime.Serialization.SerializationException:类型'MyNamespace. ActivityParty 的数据合同名称为 'ActivityParty:schemas.datacontract.org/2004/07/MyNameSpace' 不是预期的),这让我很头疼。有趣的是,它在我有 .net 4.5 的开发机器上工作,并且错误只发生在他们仍然有 .net 4.0 的测试服务器上)。我这样做的方式解决了这个问题。但我知道它看起来有点丑。
        • phonecall.To 是一个 ActivityParty 数组(如果您愿意,也可以是一个 Entity 数组)
        • umm.. 再次抱歉,您是对的,我搞砸了——当我第二次阅读时才意识到
        【解决方案4】:

        如果我是对的,您不需要使用 EntityCollection,而是使用 Entity 的数组

        使用后期绑定语法创建电话呼叫将是:

        Entity from1 = new Entity("activityparty");
        Entity to1 = new Entity("activityparty");
        Entity to2 = new Entity("activityparty"); // two contacts inside the to field
        
        from1["partyid"]= new EntityReference("systemuser", userId);
        to1["partyid"]= new EntityReference("contact", contact1Id);
        to2["partyid"]= new EntityReference("contact", contact2Id);
        
        Entity phonecall = new Entity("phonecall");
        
        phonecall["from"] = new Entity[] { from1 };
        phonecall["to"] = new Entity[] { to1, to2 };
        // other phonecall fields
        
        Guid phonecallId = service.Create(phonecall);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-07
          • 2015-09-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多