gpt4 book ai didi

c# - 如何使用 CrmServiceClient 设置空属性值

转载 作者:太空宇宙 更新时间:2023-11-03 12:14:23 25 4
gpt4 key购买 nike

我正在努力使用 CrmServiceClient 将属性值更新为 null。

我的代码对于非 null 值工作正常但失败并出现异常:

Object reference not set to an instance of an object. at Microsoft.Xrm.Tooling.Connector.CrmServiceClient.AddValueToPropertyList(KeyValuePair2
Field, AttributeCollection PropertyList) at
Microsoft.Xrm.Tooling.Connector.CrmServiceClient.UpdateEntity(String
entityName, String keyFieldName, Guid id, Dictionary
2 fieldList, String applyToSolution, Boolean enabledDuplicateDetection, 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 上的更新方法要到 2016 年才可用。

最佳答案

这应该有效。

Entity ent = new Entity("entityname");
ent.Attributes["datefieldname"] = null;
ent.id = Id;
service.Update(ent);

关于c# - 如何使用 CrmServiceClient 设置空属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50477789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com