我正在努力使用CrmServiceClient将属性值更新为null。
我的代码可以很好地处理非null值,但是失败并出现异常:
你调用的对象是空的。在
Microsoft.Xrm.Tooling.Connector.CrmServiceClient.AddValueToPropertyList(KeyValuePair 2 Field, AttributeCollection PropertyList) at Microsoft.Xrm.Tooling.Connector.CrmServiceClient.UpdateEntity(String entityName, String keyFieldName, Guid id, Dictionary
2 fieldList,
字符串applyToSolution,启用布尔值DuplicateDetection,Guid
batchId)
每当我尝试设置空值时。
到目前为止,我已经尝试了以下方法:
// create Crm service client object
CrmServiceClient svc = new CrmServiceClient(
new System.Net.NetworkCredential("username", "password", "domain"),
"crmhost",
"443",
"crmorganization",
false,
true
);
// check the connection to CRM was successful
if (!svc.IsReady)
{
throw new Exception("Could not connect to CRM Organization.", svc.LastCrmException);
}
// create a Dictionary to store the attributes to be added to the entity
Dictionary<string, CrmDataTypeWrapper> attributes = new Dictionary<string, CrmDataTypeWrapper>();
//this doesn't work
attributes.Add("new_somedatefield", null);
// and this doesn't work
attributes.Add("new_somedatefield", new CrmDataTypeWrapper(null, CrmFieldType.CrmDateTime));
// this also doesn't work
DateTime? nullDate = new DateTime();
nullDate = null;
attributes.Add("new_somedatefield", new CrmDataTypeWrapper(nullDate, CrmFieldType.CrmDateTime));
svc.UpdateEntity("new_entityname", "new_entitynameid", (Guid)data[metaData.PrimaryIdAttribute], attributes));
在文档中没有特别提到设置空值。
有人成功地做到了吗?
编辑-为了澄清我需要针对Dynamics CRM 2015,CrmServiceClient上的Update方法要到2016年才可用。
最佳答案
这应该工作。
Entity ent = new Entity("entityname");
ent.Attributes["datefieldname"] = null;
ent.id = Id;
service.Update(ent);