private void UpdateEntity(IOrganizationService service, Entity entity, string entityKey, int recordNumber, EntityReference ownerid, OptionSetValue statecode, OptionSetValue statuscode, Dictionary <string, ResolvedEntity[]> resolvedEntities) { if (!Constants.UpdateModes.Contains(Configuration.ImportMode)) { return; } bool validForUpdate = resolvedEntities[entityKey].Length == 1 || (resolvedEntities[entityKey].Length > 1 && Configuration.MultipleFoundMode == MultipleFoundMode.All); if (!validForUpdate) { return; } for (int i = 0; i < resolvedEntities[entityKey].Length; i++) { var resolvedEntity = resolvedEntities[entityKey][i].Value; entityUpdateHandler.BuildEntityForUpdate(entity, resolvedEntity, Configuration.ImportMode); try { if (entity.Attributes.Count > 0) { service.Update(entity); foreach (var attribute in entity.Attributes) { resolvedEntity.Attributes.Add(attribute.Key, attribute.Value); } } else { // Can optionally implement - log that an empty was not updated because it was equal } } catch (FaultException <OrganizationServiceFault> ex) { logger.SetWriteFault(recordNumber, ex.Detail.Message); } var resolvedEntityOwnerId = resolvedEntity.Contains("ownerid") ? (EntityReference)resolvedEntity["ownerid"] : null; bool ownerMustBeSet = EntityUpdateHandler.OwnerMustBeSet(ownerid, resolvedEntityOwnerId, Configuration.SetOwnerMode); if (ownerMustBeSet) { service.SetOwnerOfEntity(entity.LogicalName, entity.Id, ownerid.LogicalName, ownerid.Id); resolvedEntity.Attributes.Add("ownerid", ownerid); } var resolvedEntityStatuscode = resolvedEntity.Contains("statuscode") ? (OptionSetValue)resolvedEntity["statuscode"] : null; bool statusMustBeSet = EntityUpdateHandler.StatusMustBeSet(statuscode, resolvedEntityStatuscode, Configuration.SetStateMode); if (statusMustBeSet) { service.SetStateOfEntity(entity.LogicalName, entity.Id, statecode, statuscode); resolvedEntity.Attributes.Add("statecode", statecode); resolvedEntity.Attributes.Add("statuscode", statuscode); } } }
public void Test_EntityUpdateHandler_SetStateMode() { var value3 = new OptionSetValue(3); var value4 = new OptionSetValue(4); bool test1 = EntityUpdateHandler.StatusMustBeSet(null, value3, ImportMode.All); Assert.IsFalse(test1); bool test2 = EntityUpdateHandler.StatusMustBeSet(value3, value3, ImportMode.Create); Assert.IsFalse(test2); bool test3 = EntityUpdateHandler.StatusMustBeSet(value3, value3, ImportMode.UpdateChangedValuesOnly); Assert.IsFalse(test3); bool test4 = EntityUpdateHandler.StatusMustBeSet(value3, value3, ImportMode.AllChangedValuesOnly); Assert.IsFalse(test4); bool test5 = EntityUpdateHandler.StatusMustBeSet(value4, value3, ImportMode.UpdateChangedValuesOnly); Assert.IsTrue(test5); bool test6 = EntityUpdateHandler.StatusMustBeSet(value4, value3, ImportMode.AllChangedValuesOnly); Assert.IsTrue(test6); bool test7 = EntityUpdateHandler.StatusMustBeSet(value3, value3, ImportMode.Update); Assert.IsTrue(test7); bool test8 = EntityUpdateHandler.StatusMustBeSet(value3, value3, ImportMode.All); Assert.IsTrue(test8); bool test9 = EntityUpdateHandler.StatusMustBeSet(value4, value3, ImportMode.Update); Assert.IsTrue(test9); bool test10 = EntityUpdateHandler.StatusMustBeSet(value4, value3, ImportMode.All); Assert.IsTrue(test10); bool test11 = EntityUpdateHandler.StatusMustBeSet(value4, null, ImportMode.UpdateChangedValuesOnly); Assert.IsTrue(test11); bool test12 = EntityUpdateHandler.StatusMustBeSet(value4, null, ImportMode.AllChangedValuesOnly); Assert.IsTrue(test12); bool test13 = EntityUpdateHandler.StatusMustBeSet(value4, null, ImportMode.Update); Assert.IsTrue(test13); bool test14 = EntityUpdateHandler.StatusMustBeSet(value4, null, ImportMode.All); Assert.IsTrue(test14); }