更新自定义工作流中创建的记录 Activity -CRM -C#

Update created record in Custom Workflow Activity -CRM -C#

我创建了新实体。

我从该实体调用自定义工作流程 Activity 创造机会的实体。 它有效,但另外我必须根据创造的机会改变一些领域。 (我必须添加机会产品,并且必须为每个机会更改价目表)。

作为测试,我尝试在创建后更新帐户字段,但失败了。当我在创建之前填充此帐户字段时,它起作用了,所以它不是关于那个的。 这是代码的一部分:

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

重复一下,它创造了机会,但它对更新不起作用,有没有其他方法可以做到这一点,或者我这里有一些错误?

它失败了,因为您正在尝试更新 opportunity 没有设置主键 (opportunityid) 的实体。

与其在创建机会后更新机会,不如在创建操作期间分配 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);