我正在尝试使用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属性我使用的更新。
有一点需要注意的是,您明显从别处复制了一些代码,因为您检索了帐户记录,但是您的foreach将其称为“contact”foreach(实体在collection.Entities中的联系人)''。虽然这会起作用,但它可能暗示您复制了其他部分,并导致您遇到的问题。我怀疑你的一个Lookup *辅助函数返回一个不匹配的数据类型。 – Filburt 2014-10-07 22:32:24
InvalidCastException是一个很大的提示,一个或多个属性设置为错误的类型 – 2014-10-08 04:09:54
虽然它可能是一个很好的提示...没有关于什么属性设置不正确的上下文,但它实际上是无用的(并且非常令人沮丧)的消息。 – 2016-02-23 04:18:50