0

我正在尝试使用Microsoft Dynamics CRM SDK以编程方式更新记录。 Unfortunatey,我拉纪录后,更新的领域,并调用.Update()方法对我的服务代理我得到以下异常:System.InvalidCastException:Microsoft Dynamics CRM遇到错误

[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; 
    } 

下面是我用它来创建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属性我使用的更新。

+0

有一点需要注意的是,您明​​显从别处复制了一些代码,因为您检索了帐户记录,但是您的foreach将其称为“contact”foreach(实体在collection.Entities中的联系人)''。虽然这会起作用,但它可能暗示您复制了其他部分,并导致您遇到的问题。我怀疑你的一个Lookup *辅助函数返回一个不匹配的数据类型。 – Filburt 2014-10-07 22:32:24

+2

InvalidCastException是一个很大的提示,一个或多个属性设置为错误的类型 – 2014-10-08 04:09:54

+0

虽然它可能是一个很好的提示...没有关于什么属性设置不正确的上下文,但它实际上是无用的(并且非常令人沮丧)的消息。 – 2016-02-23 04:18:50

回答

0

在你CreateAccount方法,当你正在处理现有的实体,尝试改变这一行:

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

_account.Id = Id; 

从理论上讲,它不应该的问题,但实体实例的Id财产应该始终为现有记录设置。

您可能还需要考虑将作为参数的账户实体(您在for循环中调用联系人)而不是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); 
} 

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

0

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