【问题标题】:System.InvalidCastException Dynamics CRMSystem.InvalidCastException 动态 CRM
【发布时间】:2018-02-07 01:22:22
【问题描述】:

我有一个创建或更新帐户的代码。此代码块调用另一种方法,该方法将 accountid 保存在联系人记录中,一旦它被选为帐户的主要联系人。

但是,当我尝试运行此程序时,我收到此错误:

Microsoft.Xrm.Sdk.dll 中出现“System.ServiceModel.FaultException`1”类型的异常,但未在用户代码中处理 附加信息:System.InvalidCastException:Microsoft Dynamics CRM 遇到错误。管理员或支持的参考号:#06B9DDED

它发生在我对 UpdateAccountNameContactEntity 方法所做的更新周围。我已经阅读了几个网站,说它可能是我正在使用的 Guid,但我对此很陌生,我不知道我应该如何解决它。

我现在正在使用 ASP.NET MVC 和 Dynamics 365。我使用this 作为我的指南来创建它。

这里是代码。

public void SaveAccount(AccountEntityModels objAccountModel)
    {
        using (OrganizationService service = new OrganizationService("CRM"))
        {
            Entity AccountEntity = new Entity("account");

            if (objAccountModel.AccountID != Guid.Empty)
            {
                AccountEntity["accountid"] = objAccountModel.AccountID;
            }

            AccountEntity["name"] = objAccountModel.AccountName;
            AccountEntity["telephone1"] = objAccountModel.Phone;
            AccountEntity["fax"] = objAccountModel.Fax;
            AccountEntity["websiteurl"] = objAccountModel.Website;
            AccountEntity["primarycontactid"] = new Microsoft.Xrm.Sdk.EntityReference { Id = objAccountModel.PrimaryContact.Id, LogicalName = "account" };

            if (objAccountModel.AccountID == Guid.Empty)
            {
                objAccountModel.AccountID = service.Create(AccountEntity);
                UpdateAccountNameContactEntity(objAccountModel.AccountID, objAccountModel.PrimaryContact.Id);
            }
            else
            {
                service.Update(AccountEntity);
                UpdateAccountNameContactEntity(objAccountModel.AccountID, objAccountModel.PrimaryContact.Id);
            }
        }
    }

    public void UpdateAccountNameContactEntity(Guid accountId, Guid contactId)
    {
        using (OrganizationService service = new OrganizationService("CRM"))
        {
            try
            {
                Entity contactEntity = new Entity("contact");
                contactEntity["contactid"] = contactId;
                contactEntity["parentcustomerid"] = accountId;
                service.Update(contactEntity); //THIS IS WHERE I GET THE ERROR
            }
            catch (Exception ex)
            {
            }
        }
    }

【问题讨论】:

  • 您至少应该:1-向我们提供完整的错误消息 2-告诉我们您在哪一行收到错误以及 3-您使用什么数据导致错误。跨度>
  • 你能显示objAccountModel.AccountID的数据类型是什么吗?请创建一个断点并将光标悬停在该行
  • @DavidG 是的,对不起。我没有注意到我没有指出我收到错误的代码部分。我现在已经修好了,希望对你有用。
  • @JericCruz objAccountModel.AccountID 说它是 System.Guid。

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


【解决方案1】:

您正在尝试在 EntityReference 中分配 GUID。

改变这一行

contactEntity["parentcustomerid"] = accountId;

如下:

contactEntity["parentcustomerid"] = new Microsoft.Xrm.Sdk.EntityReference { Id = accountId, LogicalName = "account" };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 2011-05-13
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多