【问题标题】:Update created record in Custom Workflow Activity -CRM -C#更新自定义工作流活动中创建的记录 -CRM -C#
【发布时间】:2016-10-05 16:38:39
【问题描述】:

我创建了新实体。

我从该实体中调用创建机会的自定义工作流活动实体。 它有效,但另外我必须更改一些已创建机会的字段。 (我必须添加机会产品,并且必须为每个机会更改价目表)。

作为测试,我尝试在创建后更新帐户字段,但字段失败。当我在创建之前填充此帐户字段时,它可以工作,所以不是这样。 以下是部分代码:

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

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

【问题讨论】:

    标签: c# dynamics-crm crm


    【解决方案1】:

    失败是因为您尝试更新没有设置主键 (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);
    

    【讨论】:

    • opportunity["parentaccountid"] = entity.Attributes["ad_sendto"]; service.Update(opportunityToUpdate); 最后两行,你的意思是opportunity["parentaccountid"] opportunityToUpdate["parenaccountid"],更新前?
    • 你是对的,复制粘贴错误,更新答案。
    猜你喜欢
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多