System.InvalidCastException:Microsoft Dynamics CRM遇到错误

本文关键字:CRM 遇到 错误 Dynamics Microsoft InvalidCastException System | 更新日期: 2023-09-27 18:25:48

我正尝试使用Microsoft Dynamics CRM SDK以编程方式更新记录。不幸的是,在我提取记录、更新字段并调用服务代理上的.Update()方法后,我得到了以下异常:

[System.ServiceModel.FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>]
System.InvalidCastException: Microsoft Dynamics CRM has experienced an error. 
Reference number for administrators or support: #B08B34D9"  
System.ServiceModel.FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>

由于我以前从未使用过CRM,而且公司内部目前使用CRM的资源非常有限,我不知道如何/从哪里开始调查异常,也不知道它可能来自哪里。

程序从以下方法开始。这只需查询数据以查找匹配的EIN编号。如果没有找到记录,那么我使用.Create()方法。如果找到记录,我们将更新Dynamics中当前存在的项目。

    public string UpdateDynamics(AgentTransmission agt)
    {
        ConditionExpression condition = new ConditionExpression();
        condition.AttributeName = "neu_taxid";
        condition.Operator = ConditionOperator.Equal;
        condition.Values.Add("012-3456787");
        ColumnSet columns = new ColumnSet(true);
        QueryExpression query = new QueryExpression();
        query.ColumnSet = columns;
        query.EntityName = "account";
        query.Criteria.AddCondition(condition);
        OrganizationServiceClient client = new OrganizationServiceClient();
        EntityCollection collection = client.RetrieveMultiple(query);
        if (collection.Entities.Count == 0)
        {
            _servicePoxy.Create(CreateAccount(agt));
        }
        else
        {
            foreach (Entity contact in collection.Entities)
            {
                //This throws the exception
                _servicePoxy.Update(CreateAccount(agt, contact.Id));
            }
        }
        return string.Empty;
    }

下面是我用来创建传递给Dynamics的Entity对象的方法。数据是从实体框架模型对象中提取的,并映射到相应的字段。

    private Entity CreateAccount(AgentTransmission agt, Guid Id = new Guid())
    {
        _account = new Entity();
        _account.LogicalName = "account";
        _account.Attributes.Add("name", agt.AgencyName);
        _account.Attributes.Add("telephone1", agt.BusinessPhone.Replace("(","").Replace(")", "").Replace("-", ""));
        _account.Attributes.Add("address1_line1", agt.MailingStreet1);
        _account.Attributes.Add("address1_city", agt.MailingCity);
        _account.Attributes.Add("address1_postalcode", agt.MailingZip);
        _account.Attributes.Add("neu_address1stateprovince", LookupStateCode(agt.MailingState));
        _account.Attributes.Add("address1_addresstypecode", new OptionSetValue(1)); //1 for Mailing
        _account.Attributes.Add("neu_channelid", LookupChannelId(agt.Channel));
        _account.Attributes.Add("neu_appointmentstatus", new OptionSetValue(279660000));
        _account.Attributes.Add("customertypecode", LookupCustomerCode(agt.RelationshipType));
        _account.Attributes.Add("neu_taxid", UnobfuscateRef(agt.ReferenceNumber).ToString());
        //If we're doing an update will need the GUID. Only use when Update is needed. 
        if (Id != Guid.Empty)
        {
            _account.Attributes.Add("accountid", Id);
        }
        return _account;
    }

然而,.Create()方法按预期工作,因此我认为异常与我用于更新的accountid GUID属性隔离。

System.InvalidCastException:Microsoft Dynamics CRM遇到错误

在处理现有实体时,在CreateAccount方法中,尝试更改此行:

_account.Attributes.Add("accountid", Id);

_account.Id = Id;

从理论上讲,这并不重要,但实体实例的Id属性应该始终为现有记录设置。

您可能还想考虑将一个帐户实体(您在for循环中称之为contact)而不是id作为参数传递到CreateAccount中,这样您就不必在每次调用中显式地创建一个帐户实例,还可以删除尝试分配id的逻辑(因为传入的实体已经定义了id)。

例如,您的电话将是:

_servicePoxy.Create(CreateAccount(agt), new Entity("account"));

_servicePoxy.Update(CreateAccount(agt, contact));

然后你可以从CreateAccount:中删除这些行

_account = new Entity();
_account.LogicalName = "account";

if (Id != Guid.Empty)
{
    _account.Attributes.Add("accountid", Id);
}

(我本来会把它作为评论发布的,但我没有足够的权限。)

System.ServiceModel.FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>吃掉堆栈跟踪。将此博客作为一种实际记录stacktrace的方法,以便能够找到错误发生的位置。