【问题标题】:Error Account with Id = "xxxxxx" does not existID =“xxxxxx”的错误帐户不存在
【发布时间】:2026-01-19 03:25:01
【问题描述】:

我有一个创建帐户和机会的托管工作流。

有时我会遇到此错误:Id = "xxxxxx" 的帐户不存在。

知道我在 CRM 中找到了帐户,但我不知道我的代码有什么问题。
以下是我的插件代码的步骤:

  1. 按 num 查找帐户(如果不存在,我创建它们)
  2. 获取帐号 = 帐号
  3. 使用 Opportunity["parentaccountid"] = Account 创建机会;
  4. 错误信息!

代码:

//Get opportunity
Guid id = retrieveOpportunity<string>("opportunity", "new_numero", numero, service);
Entity eOpportunity;
if (id != Guid.Empty)
{
    eOpportunity = new Entity("opportunity", id);
}
else
{
    eOpportunity = new Entity("opportunity");
}

//Get account
EntityReference eAccount = retrieveAccount<string>(accountCode, "account", "new_code", service);
if (eAccount == null)
{
    eAccount = new Entity("account", "new_code", accountCode);
    eAccount["name"] = "name";
    UpsertRequest usMessage = new UpsertRequest()
    {
        Target = eAccount
    };
    //create account
    UpsertResponse usResponse = (UpsertResponse)this._service.Execute(usMessage);
    eOpportunity["parentaccountid"] = usResponse.Target;
}
else
{
    eOpportunity["parentaccountid"] = eAccount;
}

UpsertRequest req = new UpsertRequest()
{
    Target = eOpportunity
}; 
//upsert opportunity
UpsertResponse resp = (UpsertResponse)service.Execute(req);

if (resp.RecordCreated)
    tracer.Trace("New opportunity");
else
    tracer.Trace("Opportunity updated");

有时会同时启动多个工作流并执行相同的操作(创造其他机会)

【问题讨论】:

  • 向我们展示实际代码而不是伪代码..:)

标签: c# dynamics-crm dynamics-crm-online dynamics-crm-365


【解决方案1】:

您还没有向我们展示整个插件,所以这只是一个猜测,但您可能在类级别共享您的 IOrganizationService,这会导致您的代码出现竞争条件,并且一个线程在一个不同的上下文,然后它的服务被另一个线程覆盖,该线程在另一个没有新创建的帐户的不同数据库事务中并且出错。

不要跨线程共享您的 IOrganziationService!

【讨论】:

    【解决方案2】:

    每当您尝试在同一事务中使用创建的记录时,请将插件转换为异步模式 - 这将起作用。

    【讨论】: