在没有早期绑定实体的CRM中创建ActivityParty

时间:2014-04-30 13:09:49

标签: c# dynamics-crm

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

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

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

4 个答案:

答案 0 :(得分:18)

如果我说得对,你不需要使用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);

答案 1 :(得分:1)

即使我赞成了答案,但我对ActivityParty的序列化有类似的问题。我找到了解决方案,并不要求你放弃早期绑定的实体。

你需要做的是这样的事情:

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

(我没有测试代码并从空中写下来,但你应该感受到这个想法)

答案 2 :(得分:0)

我为TrN投票,因为我正在寻找任何一种例子,而且它是我能找到的唯一早期约束的例子。

他的例子实际帮助我创建了一个具有属性&#34; From&#34;的PhoneCall实体。指向实际拨打电话的主管。我从未完全理解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<ActivityParty>如何工作,这可能有助于他们创建正确的PhoneCall活动。

答案 3 :(得分:-1)

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

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;
}