在没有早期绑定实体的CRM中创建ActivityParty
本文关键字:CRM 创建 ActivityParty 实体 绑定 | 更新日期: 2023-09-27 18:00:52
根据要求,我不能使用使用"CrmSvcUtil"创建的早期绑定上下文。问题是,新的电话呼叫活动需要两个字段("from"answers"to"(,它们是activityparty
类型的Entities
。标准XRM/CRM命名空间不包含类似于使用实用工具创建的ActivityParty
的类。
我尝试用EntityCollection填充它,但随后该字段将为空。接下来,我尝试重新创建一个工作电话活动的结构。EntityCollection
">activityparty"->带有一个Entity
">activityparty"->带有EntityReference
属性">partitionid>"->实体引用(例如"contact"和联系人的id(。但它根本不起作用。
如何使用"普通"Entitiy
类创建ActivityParty(或更好的电话活动(?
如果我是对的,您不需要使用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);
尽管我对答案投了赞成票,但我对ActivityParty的序列化也有类似的问题。我找到了一个不需要您放弃早期绑定实体的解决方案。
你需要做的是这样的事情:
IEnumerable<ActivityParty> party = new [] { new ActivityParty { PartyId="", EntityLogicalName="..." } };
phonecall["to"] = new EntityCollection(party.Select(x => x.ToEntity<Entity>).ToList());
(我没有测试代码,而是在空中编写的,但你应该能感受到这个想法(
我投票给TrN,因为我在寻找任何类型的例子,这是我能找到的唯一早期绑定的例子。
他的例子实际上帮助我创建了一个PhoneCall实体,该实体的属性"From"指向实际发出呼叫的Lead。我从未完全理解IEnumerable<ActivityParty>
枚举器。多亏了TrN,我对它的理解足以使用它。
这是我关于PhoneCall活动的代码,我已经测试过了,它是有效的。每次现有潜在客户打来电话。保存PhoneCall活动时,会将正确的Attribute值链接到正确的Lead。
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活动。
以下是相同的工作代码。如果有人遇到任何问题,请随时联系。
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;
}