public object GetAggregate(Guid id, XrmService service) { object newValue = null; switch (AggregateType) { case AggregateType.Exists: { //if the aggregate returns a result > 0 then one exists var fetch = GetLookupFetch(id); var result = service.RetrieveAllFetch(fetch); newValue = result.Any() && XrmEntity.GetInt(result.ElementAt(0).GetFieldValue(FetchAlias)) > 0; break; } case AggregateType.Count: { var result = service.RetrieveAllFetch(GetLookupFetch(id)); if (result.Any()) { newValue = result.ElementAt(0).GetFieldValue(FetchAlias); } break; } case AggregateType.Sum: { var result = service.RetrieveAllFetch(GetLookupFetch(id)); if (result.Any()) { newValue = result.ElementAt(0).GetFieldValue(FetchAlias); } break; } case AggregateType.Min: { var query = GetAggregatedRecordQueryForLookup(id); query.AddOrder(AggregatedField, OrderType.Ascending); var minRecord = service.RetrieveFirst(query); newValue = minRecord.GetField(AggregatedField); break; } case AggregateType.CSV: case AggregateType.PSV: { var query = GetAggregatedRecordQueryForLookup(id); query.AddOrder(AggregatedField, OrderType.Ascending); var records = service.RetrieveAll(query); var labels = records.Select( delegate(Entity item) { return(item.GetField(AggregatedField)); }). ToArray(); if (AggregateType == AggregateType.CSV) { newValue = string.Join(", ", labels); } else { newValue = string.Join("|", labels); } newValue = ((string)newValue).Left(1000); break; } } if (newValue == null && NullAmount != null) { newValue = NullAmount; } return(newValue); }
public object GetRollup(LookupRollup rollup, Guid id) { object newValue = null; switch (rollup.RollupType) { case RollupType.Exists: { //if the Rollup returns a result > 0 then one exists var fetch = GetLookupFetch(rollup, id); var result = XrmService.Fetch(fetch); newValue = result.Any() && XrmEntity.GetInt(result.First().GetField(FetchAlias)) > 0; break; } case RollupType.Count: { var result = XrmService.Fetch(GetLookupFetch(rollup, id)); if (result.Any()) { newValue = result.ElementAt(0).GetField(FetchAlias); } break; } case RollupType.Sum: { var result = XrmService.Fetch(GetLookupFetch(rollup, id)); if (result.Any()) { newValue = result.ElementAt(0).GetField(FetchAlias); } break; } case RollupType.Min: { var query = GetRollupQueryForLookup(rollup, id); query.AddOrder(rollup.FieldRolledup, OrderType.Ascending); var minRecord = XrmService.RetrieveFirst(query); newValue = minRecord.GetField(rollup.FieldRolledup); break; } case RollupType.Max: { var query = GetRollupQueryForLookup(rollup, id); query.AddOrder(rollup.FieldRolledup, OrderType.Descending); var maxRecord = XrmService.RetrieveFirst(query); newValue = maxRecord.GetField(rollup.FieldRolledup); break; } case RollupType.Mean: { var result = XrmService.Fetch(GetLookupFetch(rollup, id)); if (result.Any()) { newValue = result.ElementAt(0).GetField(FetchAlias); } break; } case RollupType.SeparatedStrings: { var query = GetRollupQueryForLookup(rollup, id); query.AddOrder(rollup.FieldRolledup, OrderType.Ascending); var records = XrmService.RetrieveAll(query); var labels = records.Select(e => e.GetField(rollup.FieldRolledup)). ToArray(); newValue = string.Join(rollup.SeparatorString, labels); break; } case RollupType.First: { var query = GetRollupQueryForLookup(rollup, id); query.AddOrder(rollup.OrderByField, rollup.OrderType); if (rollup.OrderByField != "createdon") { query.AddOrder("createdon", OrderType.Descending); } var record = XrmService.RetrieveFirst(query); newValue = record.GetField(rollup.FieldRolledup); if (newValue is Guid g && rollup.ObjectType == typeof(EntityReference)) { newValue = new EntityReference(rollup.RecordTypeRolledup, g); } break; } } if (newValue == null && rollup.NullAmount != null) { newValue = rollup.NullAmount; } if (newValue != null && rollup.ObjectType != null) { if (rollup.ObjectType == typeof(decimal)) { newValue = Convert.ToDecimal(newValue.ToString()); } } return(newValue); }
public object GetRollup(LookupRollup rollup, Guid id) { object newValue = null; switch (rollup.RollupType) { case RollupType.Exists: { //if the Rollup returns a result > 0 then one exists var fetch = GetLookupFetch(rollup, id); var result = XrmService.Fetch(fetch); newValue = result.Any() && XrmEntity.GetInt(result.First().GetField(FetchAlias)) > 0; break; } case RollupType.Count: { var result = XrmService.Fetch(GetLookupFetch(rollup, id)); if (result.Any()) { newValue = result.ElementAt(0).GetField(FetchAlias); } break; } case RollupType.Sum: { var result = XrmService.Fetch(GetLookupFetch(rollup, id)); if (result.Any()) { newValue = result.ElementAt(0).GetField(FetchAlias); } break; } case RollupType.Min: { var query = GetRollupQueryForLookup(rollup, id); query.AddOrder(rollup.FieldRolledup, OrderType.Ascending); var minRecord = XrmService.RetrieveFirst(query); newValue = minRecord.GetField(rollup.FieldRolledup); break; } case RollupType.CSV: case RollupType.PSV: { var query = GetRollupQueryForLookup(rollup, id); query.AddOrder(rollup.FieldRolledup, OrderType.Ascending); var records = XrmService.RetrieveAll(query); var labels = records.Select(e => e.GetField(rollup.FieldRolledup)). ToArray(); if (rollup.RollupType == RollupType.CSV) { newValue = string.Join(", ", labels); } else { newValue = string.Join("|", labels); } newValue = ((string)newValue).Left(1000); break; } } if (newValue == null && rollup.NullAmount != null) { newValue = rollup.NullAmount; } if (newValue != null && rollup.ObjectType != null) { if (rollup.ObjectType == typeof(decimal)) { newValue = Convert.ToDecimal(newValue.ToString()); } } return(newValue); }