Hi,
I want to update all fields of an entity. I check if the field property "IsValidForUpdate = true" and want to insert the new values.
Contact con = xrm.ContactSet.Where(c => c.ContactId == id).First();
foreach (FieldItem field in listItem.FieldItems)
{
if (field.ToUpdate)
{
if (field.Value != null)
{
con[field.InternalName] = field.Value;
}
xrm.UpdateObject(con);
}
}
xrm.SaveChanges();
I got an "Incorrect attribute value type System.String" exception. So I tried to cast the field.Value:
RetrieveEntityResponse res = (RetrieveEntityResponse)xrm.Execute(req);
//...
foreach (var s in res.EntityMetadata.Attributes)
{
//...
TypeAsString = s.AttributeType.ToString();
//...
}
//update entity
con[field.InternalName] = Convert.ChangeType(field.Value, Type.GetType(field.TypeAsString));
But it won't work, because the Types that I get from CRM 2011 are not C# conform.
I hope that someone can help me.
Best regards, Markus