We have upgraded our CRM Dynamics system from 2011 - 2016 and getting issues as we have had to change from the SOAP web reference approach, to the SDK.
Types have changed now and we are wondering the best way to update this. We are wondering if the following function can be updated? And the best approach forward. We have updated the GetValue() function but are struggling on the others.
Main code
public Property SetProperty(Field field, object value) { if (field.Type == typeof(LookupProperty)) { Guid id = Guid.Empty; if (value.GetType() == typeof(Guid)) { id = (Guid)value; } else id = GetRecordGuid(field.LookupEntity, field.SearchField, value); if (id != Guid.Empty) { LookupProperty lp = new LookupProperty(); lp.Name = field.Name; lp.Value = new Lookup(); lp.Value.Value = id; lp.Value.type = field.LookupEntity; return lp; } } else if (field.Type == typeof(KeyProperty)) { KeyProperty kp = new KeyProperty(); kp.Name = field.Name; kp.Value = new Key(); kp.Value.Value = new Guid(Convert.ToString(value)); return kp; } else if (field.Type == typeof(StringProperty)) { StringProperty sp = new StringProperty(); sp.Name = field.Name; sp.Value = value.ToString(); return sp; } else if (field.Type == typeof(CrmBooleanProperty)) { bool v = new bool(); bool b = Boolean.TryParse(Convert.ToString(value), out v); if (b) { CrmBooleanProperty bp = new CrmBooleanProperty(); bp.Name = field.Name; bp.Value = new CrmBoolean(); bp.Value.Value = v; return bp; } } else if (field.Type == typeof(CrmDateTimeProperty)) { string dval = Convert.ToString(value); string fdate = dval.Substring(6, 4) "-" dval.Substring(3, 2) "-" dval.Substring(0, 2) "T21:00:00"; DateTime dt = new DateTime(); bool b = DateTime.TryParse(fdate, out dt); if (b) { CrmDateTimeProperty dp = new CrmDateTimeProperty(); dp.Name = field.Name; dp.Value = new CrmDateTime(); dp.Value.Value = Convert.ToString(fdate); return dp; } } else if (field.Type == typeof(CrmDecimalProperty)) { decimal d = new decimal(); bool b = Decimal.TryParse(Convert.ToString(value), out d); if (b) { CrmDecimalProperty dp = new CrmDecimalProperty(); dp.Name = field.Name; dp.Value = new CrmDecimal(); dp.Value.Value = d; return dp; } } else if (field.Type == typeof(CrmMoneyProperty)) { decimal d = new decimal(); bool b = Decimal.TryParse(Convert.ToString(value), out d); if (b) { CrmMoneyProperty mp = new CrmMoneyProperty(); mp.Name = field.Name; mp.Value = new CrmMoney(); mp.Value.Value = d; return mp; } } else if (field.Type == typeof(PicklistProperty)) { int i = -1; bool b = Int32.TryParse(Convert.ToString(value), out i); if (b) { PicklistProperty pp = new PicklistProperty(); pp.Name = field.Name; pp.Value = new Picklist(); pp.Value.Value = i; return pp; } } return null; } }
GetRecordGuid
public Guid GetRecordGuid(string entity, string searchField, object searchValue)
{
ColumnSet cs = ColumnSet(new string[] { entity "id" });
ConditionExpression ce = ConditionExpression(searchField, searchValue);
QueryExpression qe = QueryExpression(entity, cs, new ConditionExpression[] { ce });
Entity de = RetrieveSingleDynamic(qe);
if (de != null)
{
object key = GetValue(de, entity "id");
if (key != null)
{
return (Guid)key;
}
}
return Guid.Empty;
}
GetRecordGuid
public Guid GetRecordGuid(string entity, string searchField, object searchValue, string searchField1, object searchValue1) { ColumnSet cs = ColumnSet(new string[] { entity "id" }); ConditionExpression ce = ConditionExpression(searchField, searchValue); ConditionExpression ce1 = ConditionExpression(searchField1, searchValue1); QueryExpression qe = QueryExpression(entity, cs, new ConditionExpression[] { ce, ce1 }); Entity de = RetrieveSingleDynamic(qe); if (de != null) { object key = GetValue(de, entity "id"); if (key != null) { return (Guid)key; } } return Guid.Empty; }
public object GetValue(Entity properties, string name) { return properties[name]; }
These functions are being called the following way (this is a sample):
private XmlDocument Create(Crm crm, Action action) { //if (!String.IsNullOrEmpty(action.JobXml)) //{ // XmlDocument jobXml = new XmlDocument(); // jobXml.LoadXml(action.JobXml); // List ps = new List (); // DynamicEntity job = new DynamicEntity...Code
We have looked at the following article on how to get values, but wondering the best approach to convert this code...
Dynamics forum article
Let me know if you need any more info, thanks