internal void UpdatePOAMask(Guid poaId, int accessRightsMask) { var existing = db.GetEntity("principalobjectaccess", poaId); existing["accessrightsmask"] = existing.GetAttributeValue <int>("accessrightsmask") | accessRightsMask; db.Update(existing); }
internal static object ConvertToDbValue(object value, IXrmDb db) { if (value is OptionSetValue osv) { return(osv.Value); } if (value is Money money) { return(money.Value); } if (value is EntityReference reference && db.IsValidEntity(reference.LogicalName)) { //return db.GetEntity(reference); return(value); } if (value is IEnumerable <Entity> entities) { return(entities .Where(e => db.IsValidEntity(e.LogicalName)) .Select(e => db.GetEntity(e.ToEntityReference())) .ToArray()); } return(value); }
private static void HandleBaseCurrencies(MetadataSkeleton metadata, IXrmDb db, Entity entity) { if (entity.LogicalName == LogicalNames.TransactionCurrency) { return; } var transAttr = "transactioncurrencyid"; if (!entity.Attributes.ContainsKey(transAttr)) { return; } var currency = db.GetEntity(LogicalNames.TransactionCurrency, entity.GetAttributeValue <EntityReference>(transAttr).Id); var attributesMetadata = metadata.EntityMetadata.GetMetadata(entity.LogicalName).Attributes.Where(a => a is MoneyAttributeMetadata); if (!currency.GetAttributeValue <decimal?>("exchangerate").HasValue) { throw new FaultException($"No exchangerate specified for transactioncurrency '{entity.GetAttributeValue<EntityReference>(transAttr)}'"); } var baseCurrency = GetBaseCurrency(metadata); foreach (var attr in entity.Attributes.ToList()) { if (attributesMetadata.Any(a => a.LogicalName == attr.Key) && !attr.Key.EndsWith("_base")) { if (entity.GetAttributeValue <EntityReference>(transAttr) == baseCurrency) { entity[attr.Key + "_base"] = attr.Value; } else { if (attr.Value is Money money) { var value = money.Value / currency.GetAttributeValue <decimal?>("exchangerate").Value; entity[attr.Key + "_base"] = new Money(value); } } } } }
internal static void PopulateEntityReferenceNames(Entity entity, IXrmDb db, EntityMetadata metaData, Dictionary <string, IEnumerable <Entity> > lookups = null) { foreach (var attr in entity.Attributes) { if (attr.Value is EntityReference eRef) { Entity row; if (lookups == null) { row = db.GetEntity(eRef); } else { row = lookups[eRef.LogicalName].SingleOrDefault(x => x.Id == eRef.Id); } if (row != null) { var nameAttr = metaData.PrimaryNameAttribute; eRef.Name = row.GetAttributeValue <string>(nameAttr); } } } }
//internal DbRow GetDbRow(EntityReference entityReference) //{ // return db.GetDbRow(entityReference); //} internal Entity GetEntity(EntityReference entityReference) { return(db.GetEntity(entityReference)); }
internal static void HandlePrecision(MetadataSkeleton metadata, IXrmDb db, Entity entity) { if (entity.LogicalName == LogicalNames.TransactionCurrency) { return; } var transAttr = "transactioncurrencyid"; if (!entity.Attributes.ContainsKey(transAttr)) { return; } var currency = db.GetEntity(LogicalNames.TransactionCurrency, entity.GetAttributeValue <EntityReference>(transAttr).Id); var attributesMetadata = metadata.EntityMetadata.GetMetadata(entity.LogicalName).Attributes.Where(a => a is MoneyAttributeMetadata); var baseCurrencyPrecision = GetBaseCurrencyPrecision(metadata); foreach (var attr in entity.Attributes.ToList()) { if (attributesMetadata.Any(a => a.LogicalName == attr.Key) && attr.Value != null) { var attributeMetadata = attributesMetadata.First(m => m.LogicalName == attr.Key) as MoneyAttributeMetadata; int?precision = null; switch (attributeMetadata.PrecisionSource) { case 0: precision = attributeMetadata.Precision; break; case 1: precision = baseCurrencyPrecision; break; case 2: precision = currency.GetAttributeValue <int?>("currencyprecision"); break; } if (!precision.HasValue) { switch (attributeMetadata.PrecisionSource) { case 0: throw new MockupException($"No precision set for field '{attr.Key}' on entity '{entity.LogicalName}'"); case 1: throw new MockupException($"No precision set for organization. Please check you have the correct metadata"); case 2: throw new MockupException($"No precision set for currency. Make sure you set the precision for your own currencies"); } } var rounded = Math.Round(((Money)attr.Value).Value, precision.Value); if (rounded < (decimal)attributeMetadata.MinValue.Value || rounded > (decimal)attributeMetadata.MaxValue.Value) { throw new FaultException($"'{attr.Key}' was outside the ranges '{attributeMetadata.MinValue}','{attributeMetadata.MaxValue}' with value '{rounded}' "); } entity[attr.Key] = new Money(rounded); } } }