Id =“xxxxxx”的错误帐户不存在

问题描述 投票:0回答:2

我有一个自定义工作流程,可以创建帐户和机会。

有时我有这个错误:Id =“xxxxxx”的帐户不存在。

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

  1. 按num查找帐户(如果不存在,我创建它们)
  2. 获取帐户=帐户
  3. 用机会创造机会[“parentaccountid”] =帐户;
  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
2个回答
1
投票

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

不要跨线程共享您的IOrganziationService!


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.