我创建了新实体。
从该实体中,我将其称为创建机会的自定义工作流事件实体。 它有效,但此外我还必须根据创造的机会更改一些字段。 (我必须添加机会产品,并且必须更改每个机会的价目表)。
作为测试,我尝试在创建后更新帐户字段,但字段失败。当我在创建之前填充此帐户字段时,它会起作用,因此与此无关。 这是部分代码:
Entity entity = null;
if (context.InputParameters != null && context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
entity = (Entity)context.InputParameters["Target"];
}
else
{
entity = service.Retrieve(context.PrimaryEntityName, ((Guid)context.PrimaryEntityId), new ColumnSet(true));
}
Entity opportunity = new Entity("opportunity");
string name = entity.GetAttributeValue<string>("subject");
opportunity["name"] = name;
opportunityId = service.Create(opportunity);
EntityReference accountlookup = (EntityReference)entity.Attributes["ad_sendto"];
Guid accountId = accountlookup.Id;
opportunity["parentaccountid"] = new EntityReference("account", accountId);
service.Update(opportunity);
再说一遍,它创造了机会,但它不适用于更新,有没有其他方法可以做到这一点,或者我这里有一些错误吗?
最佳答案
失败是因为您尝试更新没有设置主键 (opportunityid) 的 opportunity
实体。
为什么不在创建操作期间分配parentaccountid
,而不是在创建机会后更新机会?
var opportunity = new Entity("opportunity");
opportunity["name"] = entity.GetAttributeValue<string>("subject"); ;
opportunity["parentaccountid"] = entity.Attributes["ad_sendto"];
opportunityId = service.Create(opportunity);
为了将来的引用,如果您必须更新刚刚创建的实体或任何与此相关的实体:
var opportunityToUpdate = new Entity("opportunity")
{
Id = opportunityId
};
opportunityToUpdate["parentaccountid"] = entity.Attributes["ad_sendto"];
service.Update(opportunityToUpdate);
关于c# - 更新自定义工作流程事件中创建的记录 -CRM -C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39879477/