Как использовать CrmServiceClient, чтобы установить нулевое значение атрибута
Я борюсь с использованием CrmServiceClient, чтобы обновить значения атрибута до нуля.
Мой код работает нормально для ненулевых значений, но не работает, за исключением:
В экземпляре объекта не задана ссылка на объект. в 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, 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 года.
1 ответ
Это должно работать.
Entity ent = new Entity("entityname");
ent.Attributes["datefieldname"] = null;
ent.id = Id;
service.Update(ent);