private void SetActivityCompleteTrigger(Func <string, object> getField) { if (!string.IsNullOrWhiteSpace((string)getField(Fields.jmcg_sms_.jmcg_setsentonregardingfield))) { var regardingType = XrmEntity.GetLookupType(getField(Fields.activitypointer_.regardingobjectid)); var regardingId = XrmEntity.GetLookupGuid(getField(Fields.activitypointer_.regardingobjectid)); if (regardingId.HasValue) { var fieldToSet = (string)getField(Fields.jmcg_sms_.jmcg_setsentonregardingfield); var fieldType = XrmService.GetFieldType(fieldToSet, regardingType); var completionTarget = XrmService.Retrieve(regardingType, regardingId.Value, new[] { fieldToSet }); if (fieldType == AttributeTypeCode.Boolean) { if (!completionTarget.GetBoolean(fieldToSet)) { XrmService.SetField(regardingType, regardingId.Value, fieldToSet, true); } } else if (fieldType == AttributeTypeCode.DateTime) { if (!XrmEntity.FieldsEqual(completionTarget.GetField(fieldToSet), LocalisationService.TodayUnspecifiedType)) { XrmService.SetField(regardingType, regardingId.Value, fieldToSet, LocalisationService.TodayUnspecifiedType); } } else { throw new NotImplementedException(string.Format("Setting the field type {0} of the field {1} on {2} type is not implemented", fieldType, fieldToSet, regardingType)); } } } }
private void LookupTest() { /* * Lookup field * */ //Valid values var expectedLookup = XrmEntity.CreateLookup(Entities.account, Guid.Empty); var actualLookup = XrmService.ParseField(Fields.jmcg_testentity_.jmcg_account, Entities.jmcg_testentity, expectedLookup); Assert.IsTrue(XrmEntity.GetLookupGuid(expectedLookup) == Guid.Empty); Assert.IsTrue(XrmEntity.GetLookupType(expectedLookup) == Entities.account); }
public void UpdateSql(Entity target) { var type = target.LogicalName; var table = type; // + "ExtensionBase"; var fieldsToUpdate = new List <KeyValuePair <string, string> >(); foreach (var field in target.GetFieldsInEntity()) { var fieldType = XrmService.GetFieldType(field, type); var value = target.GetField(field); if (value == null) { if (fieldType == AttributeTypeCode.Money) { fieldsToUpdate.Add(new KeyValuePair <string, string>(field + "_base", "null")); } fieldsToUpdate.Add(new KeyValuePair <string, string>(field, "null")); } else { switch (fieldType) { case AttributeTypeCode.DateTime: fieldsToUpdate.Add(new KeyValuePair <string, string>(field, SqlProvider.ToSqlDateString((DateTime)value))); break; case AttributeTypeCode.Money: fieldsToUpdate.Add(new KeyValuePair <string, string>(field, XrmEntity.GetMoneyValue(value).ToString(CultureInfo.InvariantCulture))); fieldsToUpdate.Add(new KeyValuePair <string, string>(field + "_base", XrmEntity.GetMoneyValue(value).ToString(CultureInfo.InvariantCulture))); break; case AttributeTypeCode.Lookup: var id = XrmEntity.GetLookupGuid(value); if (!id.HasValue) { throw new NullReferenceException("error no id in " + field); } fieldsToUpdate.Add(new KeyValuePair <string, string>(field, WrapSqlString(id.Value.ToString()))); break; case AttributeTypeCode.Picklist: fieldsToUpdate.Add(new KeyValuePair <string, string>(field, XrmEntity.GetOptionSetValue(value).ToString(CultureInfo.InvariantCulture))); break; case AttributeTypeCode.Status: fieldsToUpdate.Add(new KeyValuePair <string, string>(field, XrmEntity.GetOptionSetValue(value).ToString(CultureInfo.InvariantCulture))); break; case AttributeTypeCode.Integer: fieldsToUpdate.Add(new KeyValuePair <string, string>(field, value.ToString())); break; case AttributeTypeCode.Decimal: fieldsToUpdate.Add(new KeyValuePair <string, string>(field, value.ToString())); break; case AttributeTypeCode.Boolean: fieldsToUpdate.Add(new KeyValuePair <string, string>(field, XrmEntity.GetBoolean(value) ? "1" : "0")); break; case AttributeTypeCode.String: fieldsToUpdate.Add(new KeyValuePair <string, string>(field, SqlProvider.ToSqlString((string)value))); break; case AttributeTypeCode.Memo: fieldsToUpdate.Add(new KeyValuePair <string, string>(field, SqlProvider.ToSqlString((string)value))); break; case AttributeTypeCode.Uniqueidentifier: break; default: throw new NotImplementedException("No update logic implemented for field type " + fieldType); } } } var setStrings = string.Join(",", fieldsToUpdate.Select(kv => string.Format("{0} = {1}", kv.Key, kv.Value))); var primaryKey = XrmService.GetPrimaryKeyField(target.LogicalName); var primaryKeySql = SqlProvider.ToSqlString(target.Id); var sql = string.Format("update {0} set {1} where {2} = {3}", table, setStrings, primaryKey, primaryKeySql); SqlProvider.ExecuteNonQuery(sql); }