I'm having an issue when trying to programmatically update a DateTime field that already has a date stored in it.
The error that i'm 开发者_JS百科getting is:
System.InvalidOperationException: There was an error generating the XML document. ---> System.ArgumentException: Value of type 'CrmDateTimeProperty' is not supported. Parameter name: value
This is the code that performs the CRM update:
public bool UpdatePromptList(PromptList list)
{
DynamicEntity c = crmService.RetrieveDynamicEntity("ntup1_promptlist", list.PromptListId);
if (c != null)
{
c.UpdateBoolean("examplebooleanfield", list.booleanField);
c.UpdateDateTime("exampledatefield", list.dateField);
c.UpdateString("examplestringfield", list.stringField);
try
{
crmService.Update(c);
}
catch (SoapException)
{
return false;
}
}
}
And here is the code that validates the DateTime value to be passed into CRM:
public static CrmDateTime FromUser(DateTime userTime)
{
return new CrmDateTime(string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:s}", userTime));
}
public static void UpdateDateTime(this DynamicEntity entity, string property, DateTime? date)
{
if (date == null)
{
if (entity.Properties.Contains(property))
{
entity.Properties.Remove(property);
}
return;
}
CrmDateTime crmDate = FromUser(date.Value);
CrmDateTimeProperty crmProp = new CrmDateTimeProperty(property, crmDate);
if (entity.Properties.Contains(property))
{
entity.Properties[property] = crmProp;
}
else
{
entity.Properties.Add(crmProp);
}
}
Any help on this would be greatly appreciated.
If you want to update the date when it is null you should alter your update method to not drop the property if it is null, but instead null it out, otherwise no update will occur for that attribute -
CrmDateTime myDateTime = new CrmDateTime();
myDateTime.IsNull = true;
myDateTime.IsNullSpecified = true;
It looks like your error is occurring because you are setting the property value to a CrmDateTimeProperty when it expects a CrmDateTime.
Change this -
if (entity.Properties.Contains(property))
{
entity.Properties[property] = crmProp;
}
to this -
if (entity.Properties.Contains(property))
{
entity.Properties[property] = crmDate;
}
and that should solve the issue you are running into.
精彩评论