public string GetLookupFetch(LookupRollup rollup, Guid id) { string RollupFieldNode; switch (rollup.RollupType) { case RollupType.Exists: { RollupFieldNode = "<attribute name='" + rollup.FieldRolledup + "' aggregate='count' distinct = 'true' alias='" + FetchAlias + "'/>"; break; } case RollupType.Count: { RollupFieldNode = "<attribute name='" + rollup.FieldRolledup + "' aggregate='count' alias='" + FetchAlias + "'/>"; break; } case RollupType.Sum: { RollupFieldNode = "<attribute name='" + rollup.FieldRolledup + "' aggregate='sum' alias='" + FetchAlias + "'/>"; break; } default: { throw new ArgumentOutOfRangeException("Fetch Rollup not implemented for " + rollup.RollupType); } } return ("<fetch version='1.0' aggregate='true' output-format='xml-platform' mapping='logical' distinct='false'>" + "<entity name='" + rollup.RecordTypeRolledup + "'>" + RollupFieldNode + "<filter type='and'>" + "<condition attribute='" + rollup.LookupName + "' operator='eq' uiname='' uitype='" + rollup.RecordTypeWithRollup + "' value='" + id + "' />" + (rollup.FieldRolledup != null ? GetConditionFetchNode(new ConditionExpression(rollup.FieldRolledup, ConditionOperator.NotNull)) : null) + String.Join("", rollup.Filters.Select(GetConditionFetchNode)) + "</filter>" + String.Join("", GetLinkedEntityFetchNode(rollup.LinkEntity)) + "</entity>" + "</fetch>"); }
private QueryExpression GetRollupQueryForLookup(LookupRollup rollup, Guid id) { var query = new QueryExpression(rollup.RecordTypeRolledup); query.Criteria.AddCondition(rollup.LookupName, ConditionOperator.Equal, id); foreach (var condition in rollup.Filters) { query.Criteria.AddCondition(condition); } query.ColumnSet.AddColumn(rollup.LookupName); if (rollup.FieldRolledup != null) { query.ColumnSet.AddColumn(rollup.FieldRolledup); query.Criteria.AddCondition(new ConditionExpression(rollup.FieldRolledup, ConditionOperator.NotNull)); } if (rollup.LinkEntity != null) { query.LinkEntities.Add(rollup.LinkEntity); } return(query); }
private void ExecuteDependencyPluginRefresh(XrmEntityPlugin plugin, LookupRollup rollup) { var idsRequireRefresh = new List <Guid>(); var isDependencyChanging = false; if ( (plugin.MessageName == PluginMessage.Create || plugin.MessageName == PluginMessage.Update || plugin.MessageName == PluginMessage.Delete) && plugin.Stage == PluginStage.PostEvent && plugin.Mode == PluginMode.Synchronous ) { switch (plugin.MessageName) { case PluginMessage.Delete: { isDependencyChanging = plugin.PreImageEntity.Contains(rollup.LookupName) && plugin.MeetsConditionsChanging(rollup.Filters); break; } case PluginMessage.Update: { if (plugin.FieldChanging(rollup.LookupName) || (rollup.FieldRolledup != null && plugin.FieldChanging(rollup.FieldRolledup)) || (rollup.LinkEntity != null && plugin.FieldChanging(rollup.LinkEntity.LinkFromAttributeName))) { isDependencyChanging = true; } else { isDependencyChanging = plugin.MeetsConditionsChanging(rollup.Filters); } break; } case PluginMessage.Create: { isDependencyChanging = plugin.TargetEntity.Contains(rollup.LookupName) && (rollup.FieldRolledup == null || plugin.TargetEntity.Contains(rollup.FieldRolledup)) && plugin.MeetsConditionsChanging(rollup.Filters); break; } } if (isDependencyChanging) { object preImageLookup = plugin.PreImageEntity.GetLookupGuid(rollup.LookupName); object contextLookup = null; if (plugin.MessageName == PluginMessage.Create || plugin.MessageName == PluginMessage.Update) { contextLookup = plugin.TargetEntity.GetLookupGuid(rollup.LookupName); } var processPreImage = false; var processContextGuid = false; //If they aren't the same do both if (!XrmEntity.FieldsEqual(preImageLookup, contextLookup)) { processPreImage = true; processContextGuid = true; } //else just do the first not null one else { if (preImageLookup != null) { processPreImage = true; } else { processContextGuid = true; } } if (processPreImage && preImageLookup != null) { idsRequireRefresh.Add((Guid)preImageLookup); } if (processContextGuid && contextLookup != null) { idsRequireRefresh.Add((Guid)contextLookup); } } } foreach (var id in idsRequireRefresh) { RefreshRollup(id, rollup); } }
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); }
public void RefreshRollup(Guid id, LookupRollup rollup) { var newValue = GetRollup(rollup, id); XrmService.SetFieldIfChanging(rollup.RecordTypeWithRollup, id, rollup.RollupField, newValue); }