public void SetFieldIfChanging(string recordType, Guid id, string fieldName, object fieldValue) { var record = Retrieve(recordType, id, new[] { fieldName }); var currentValue = record.GetField(fieldName); if (!XrmEntity.FieldsEqual(currentValue, fieldValue)) { record.SetField(fieldName, fieldValue); Update(record); } }
/// <summary> /// MAYBE TRUE FOR DELETE! FIELD MUST BE IN PREIMAGE FOR UPDATE STEP! Returns if the fields value is logically changing /// by inspecting the Target and Preimage /// </summary> public bool FieldChanging(string fieldName) { if (MessageName == PluginMessage.Create || MessageName == PluginMessage.Update) { return(TargetEntity.Contains(fieldName) && !XrmEntity.FieldsEqual(PreImageEntity.GetField(fieldName), TargetEntity.GetField(fieldName))); } else if (MessageName == PluginMessage.Delete) { return(GetFieldFromPreImage(fieldName) != null); } else { //not sure how to get status if a setstate message throw new InvalidPluginExecutionException("FieldChanging Not Implemented for plugin message " + MessageName); } }
/// <summary> /// Basic script for verifying configured Rollups - not only uses active filter in scenarios /// </summary> /// <summary> /// Basic script for verifying configured Rollups - not only uses active filter in scenarios /// </summary> public void VerifyRollupScenarios(string parentType, string childType, string referenceField) { var Rollups = $ext_jmobjprefix$RollupService .GetRollupsForRolledupType(childType) .Where(a => a.RecordTypeWithRollup == parentType) .ToArray(); var contactFieldsRollupd = Rollups.Select(a => a.FieldRolledup).ToArray(); var parent1 = CreateTestRecord(parentType); foreach (var rollup in Rollups) { if (rollup.NullAmount != null) { Assert.IsTrue(XrmEntity.FieldsEqual(rollup.NullAmount, parent1.GetField(rollup.RollupField))); } } var parent2 = CreateTestRecord(parentType); //create new child linked to parent and verify the rollup fields populated var child1 = new Entity(childType); PopulateRollupFields(child1); child1.SetLookupField(referenceField, parent1); child1 = CreateAndRetrieve(child1); parent1 = Refresh(parent1); foreach (var rollup in Rollups) { var expectedValue1 = $ext_jmobjprefix$RollupService.GetRollup(rollup, parent1.Id); Assert.IsNotNull(child1.GetField(rollup.FieldRolledup)); Assert.IsTrue(XrmEntity.FieldsEqual(expectedValue1, parent1.GetField(rollup.RollupField))); } //create second child linked to parent and verify the rollup fields added var child2 = new Entity(childType); PopulateRollupFields(child2); child2.SetLookupField(referenceField, parent1); child2 = CreateAndRetrieve(child2); parent1 = Refresh(parent1); foreach (var rollup in Rollups) { Assert.IsNotNull(child1.GetField(rollup.FieldRolledup)); Assert.IsNotNull(child2.GetField(rollup.FieldRolledup)); var expectedValue1 = $ext_jmobjprefix$RollupService.GetRollup(rollup, parent1.Id); Assert.IsTrue(XrmEntity.FieldsEqual(expectedValue1, parent1.GetField(rollup.RollupField))); } //change second child to second parent and verify both parents updated child2.SetLookupField(referenceField, parent2); child2 = UpdateFieldsAndRetreive(child2, referenceField); parent1 = Refresh(parent1); foreach (var rollup in Rollups) { Assert.IsNotNull(child1.GetField(rollup.FieldRolledup)); var expectedValue1 = $ext_jmobjprefix$RollupService.GetRollup(rollup, parent1.Id); Assert.IsTrue(XrmEntity.FieldsEqual(expectedValue1, parent1.GetField(rollup.RollupField))); } parent2 = Refresh(parent2); foreach (var rollup in Rollups) { Assert.IsNotNull(child2.GetField(rollup.FieldRolledup)); var expectedValue2 = $ext_jmobjprefix$RollupService.GetRollup(rollup, parent1.Id); Assert.IsTrue(XrmEntity.FieldsEqual(expectedValue2, parent2.GetField(rollup.RollupField))); } //triple each value in child 1 and verify updated in parent PopulateRollupFields(child1, multiplier: 3); child1 = UpdateFieldsAndRetreive(child1, contactFieldsRollupd); parent1 = Refresh(parent1); foreach (var rollup in Rollups) { Assert.IsNotNull(child1.GetField(rollup.FieldRolledup)); var expectedValue1 = $ext_jmobjprefix$RollupService.GetRollup(rollup, parent1.Id); Assert.IsTrue(XrmEntity.FieldsEqual(expectedValue1, parent1.GetField(rollup.RollupField))); } //negate value in child 1 and verify updated in parent PopulateRollupFields(child1, multiplier: 2); child1 = UpdateFieldsAndRetreive(child1, contactFieldsRollupd); parent1 = Refresh(parent1); foreach (var rollup in Rollups) { Assert.IsNotNull(child1.GetField(rollup.FieldRolledup)); var expectedValue1 = $ext_jmobjprefix$RollupService.GetRollup(rollup, parent1.Id); Assert.IsTrue(XrmEntity.FieldsEqual(expectedValue1, parent1.GetField(rollup.RollupField))); } //create child in account 1 and verify updated var child3 = new Entity(childType); PopulateRollupFields(child3); child3.SetLookupField(referenceField, parent1); child3 = CreateAndRetrieve(child3); parent1 = Refresh(parent1); foreach (var rollup in Rollups) { Assert.IsNotNull(child3.GetField(rollup.FieldRolledup)); var expectedValue1 = $ext_jmobjprefix$RollupService.GetRollup(rollup, parent1.Id); Assert.IsTrue(XrmEntity.FieldsEqual(expectedValue1, parent1.GetField(rollup.RollupField))); } //deactivate and verify updated XrmService.SetState(child3.LogicalName, child3.Id, 1); parent1 = Refresh(parent1); foreach (var rollup in Rollups) { var expectedValue1 = $ext_jmobjprefix$RollupService.GetRollup(rollup, parent1.Id); Assert.IsTrue(XrmEntity.FieldsEqual(expectedValue1, parent1.GetField(rollup.RollupField))); } //activate and verify updated XrmService.SetState(child3.LogicalName, child3.Id, 0); parent1 = Refresh(parent1); foreach (var rollup in Rollups) { var expectedValue1 = $ext_jmobjprefix$RollupService.GetRollup(rollup, parent1.Id); Assert.IsTrue(XrmEntity.FieldsEqual(expectedValue1, parent1.GetField(rollup.RollupField))); } //delete and verify updated Delete(child3); parent1 = Refresh(parent1); foreach (var rollup in Rollups) { Assert.IsNotNull(child1.GetField(rollup.FieldRolledup)); var expectedValue1 = $ext_jmobjprefix$RollupService.GetRollup(rollup, parent1.Id); Assert.IsTrue(XrmEntity.FieldsEqual(expectedValue1, parent1.GetField(rollup.RollupField))); } //lets create one without the fields populated just to verify no error child3 = new Entity(childType); child3.SetLookupField(referenceField, parent1); child3 = CreateAndRetrieve(child3); parent1 = Refresh(parent1); foreach (var rollup in Rollups) { var expectedValue1 = $ext_jmobjprefix$RollupService.GetRollup(rollup, parent1.Id); Assert.IsTrue(XrmEntity.FieldsEqual(expectedValue1, parent1.GetField(rollup.RollupField))); } //same activate / deactivate / delete for the parent 2 //this includes changing to not exists //deactivate and verify updated XrmService.SetState(child2.LogicalName, child2.Id, 1); parent2 = Refresh(parent2); foreach (var rollup in Rollups) { var expectedValue2 = $ext_jmobjprefix$RollupService.GetRollup(rollup, parent2.Id); Assert.IsTrue(XrmEntity.FieldsEqual(expectedValue2, parent2.GetField(rollup.RollupField))); } //activate and verify updated XrmService.SetState(child2.LogicalName, child2.Id, 0); parent2 = Refresh(parent2); foreach (var rollup in Rollups) { var expectedValue2 = $ext_jmobjprefix$RollupService.GetRollup(rollup, parent2.Id); Assert.IsTrue(XrmEntity.FieldsEqual(expectedValue2, parent2.GetField(rollup.RollupField))); } //delete and verify updated Delete(child2); parent2 = Refresh(parent2); foreach (var rollup in Rollups) { var expectedValue2 = $ext_jmobjprefix$RollupService.GetRollup(rollup, parent2.Id); Assert.IsTrue(XrmEntity.FieldsEqual(expectedValue2, parent2.GetField(rollup.RollupField))); } DeleteMyToday(); }
private void CheckAggregation(XrmEntityPlugin plugin) { if (plugin.TargetType != RecordTypeAggregated) { return; } if ( (plugin.MessageName == PluginMessage.Create || plugin.MessageName == PluginMessage.Update || plugin.MessageName == PluginMessage.Delete) && plugin.Stage == PluginStage.PostEvent && plugin.Mode == PluginMode.Synchronous ) { var isDependencyChanging = false; switch (plugin.MessageName) { case PluginMessage.Delete: { isDependencyChanging = plugin.PreImageEntity.Contains(LookupName) && plugin.MeetsConditionsChanging(Filters); break; } case PluginMessage.Update: { if (plugin.FieldChanging(LookupName) || (AggregatedField != null && plugin.FieldChanging(AggregatedField)) || (LinkEntity != null && plugin.FieldChanging(LinkEntity.LinkFromAttributeName))) { isDependencyChanging = true; } else { isDependencyChanging = plugin.MeetsConditionsChanging(Filters); } break; } case PluginMessage.Create: { isDependencyChanging = plugin.TargetEntity.Contains(LookupName) && (AggregatedField == null || plugin.TargetEntity.Contains(AggregatedField)) && plugin.MeetsConditionsChanging(Filters); break; } } if (isDependencyChanging) { object preImageLookup = plugin.PreImageEntity.GetLookupGuid(LookupName); object contextLookup = null; if (plugin.MessageName == PluginMessage.Create || plugin.MessageName == PluginMessage.Update) { contextLookup = plugin.TargetEntity.GetLookupGuid(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) { RefreshAggregate((Guid)preImageLookup, plugin.XrmService, plugin.Controller); } if (processContextGuid && contextLookup != null) { RefreshAggregate((Guid)contextLookup, plugin.XrmService, plugin.Controller); } } } }
public IEnumerable <Guid> GetIdsRequiringRefresh(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); } } } return(idsRequireRefresh); }