c# - 更新自定义工作流程事件中创建的记录 -CRM -C#

标签 c# dynamics-crm crm

我创建了新实体。

从该实体中,我将其称为创建机会的自定义工作流事件实体。 它有效,但此外我还必须根据创造的机会更改一些字段。 (我必须添加机会产品,并且必须更改每个机会的价目表)。

作为测试,我尝试在创建后更新帐户字段,但字段失败。当我在创建之前填充此帐户字段时,它会起作用,因此与此无关。 这是部分代码:

          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/

相关文章:

dynamics-crm - 在开发时将单个文件部署到 Dynamics 365(例如 Web 资源)

javascript - 如何使用 Dynamic CRM 端点从列表中删除联系人

c# - 如何在 C# 中加载我的测试数据?

c# - 使用 JSON.NET 将对象序列化为 HttpClient 的响应流

c# - 我如何最小起订量 System.IO.FileInfo 类...或任何其他没有接口(interface)的类?

c# - 如何根据Incident Guid获取注解

javascript - 在 CRM 2011 的 Web 资源中出现错误对象不支持属性或方法 'setSrc'

c# - IEnumerable<T> 与 T[]

dynamics-crm-2011 - Microsoft Dynamics CRM 始终是移动 express

azure - 如何使用azure逻辑应用程序内的文本检索选项集的整数值