[英]Create ActivityParty in CRM without early bound Entities
作为要求,我不能使用用“CrmSvcUtil”创建的早期绑定上下文。 问题是新的电话呼叫活动需要两个字段('from'和'to'),这些字段是activityparty
类型的Entities
。 标准XRM / CRM名称空间不包含类似于使用Utility创建的ActivityParty
的类。
我尝试用EntityCollection填充它,但该字段将为空。 接下来,我尝试重新创建一个有效的电话呼叫活动的结构。 EntityCollection
“ activityparty ” - >具有一个Entity
“ activityparty ” - >具有EntityReference
属性“ partyid ” - >实体引用(例如“联系人”和联系人的ID)。 但它根本行不通。
如何使用“普通” Entitiy
类创建ActivityParty(或更好的电话Entitiy
活动)?
如果我是对的,你不需要使用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活动。
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 Activity。
这是相同的工作代码。 如果有人遇到任何问题,请随时联系。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.