System.InvalidCastException:Microsoft Dynamics CRM遇到错误

时间:2014-10-07 21:50:30

标签: c# dynamics-crm-2011 dynamics-crm dynamics-crm-4 dynamics-crm-2013

我尝试使用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,并且我们目前在公司内部使用过恐吓资源,因此我不知道如何/从何处开始调查异常,或者知道它可能来自何处从。

程序以以下方法开头。这只是在数据中查询匹配的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对象的方法。数据从Entity Framework模型对象中提取并映射到相应的字段。

    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属性隔离开来。

2 个答案:

答案 0 :(得分:0)

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

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

_account.Id = Id;

理论上,它应该无关紧要,但应始终为现有记录设置实体实例的Id属性。

您可能还想考虑将帐户实体(您在for循环中调用contact)而不是id作为参数传入CreateAccount,然后您就不必显式创建帐户实例在每次调用中,也可以删除尝试分配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);
}

(我会发布此更多评论,但我没有足够的权限。)

答案 1 :(得分:0)

System.ServiceModel.FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>吃堆栈跟踪。请参考this blog作为实际记录堆栈跟踪的方法,以便能够找到错误发生的位置。