public void Exception_If_Pre_Image_Is_Missing() { var fakedContext = new XrmFakedContext { ProxyTypesAssembly = Assembly.GetAssembly(typeof(CrmObject.Account)) }; var ctx = fakedContext.GetDefaultPluginContext(); ctx.MessageName = "Create"; ctx.Mode = (int)ThinkCrm.Core.PluginCore.Helper.ExecutionMode.Synchronous; ctx.IsolationMode = (int)ThinkCrm.Core.PluginCore.Helper.IsolationMode.None; ctx.Stage = (int)ThinkCrm.Core.PluginCore.Helper.PipelineStage.Postoperation; var targetGuid = Guid.NewGuid(); var target = new CrmObject.Account() { Id = targetGuid, think_frozenEnum = CrmObject.Account_think_frozen.Yes }; ctx.InputParameters = new ParameterCollection { new KeyValuePair <string, object>("Target", target) }; ctx.PrimaryEntityName = target.LogicalName; ctx.PrimaryEntityId = target.Id; Assert.Throws <InvalidPluginExecutionException>(() => fakedContext.ExecutePluginWith <FrozenFlagChangedTestHarness>(ctx)); }
private static List <Entity> GetAccountList() { var act1 = new CrmObject.Account() { Id = Guid.Parse("{FCFD0F00-B18A-45E8-9CF5-2A4C6BB6923D}"), Name = "Account 1" }; var act1a = new CrmObject.Account() { Id = Guid.Parse("{E063D3AF-13E3-4A89-9E26-3FE9E88D491B}"), Name = "Account 1-A", ParentAccountId = act1.ToEntityReference() }; var act1ai = new CrmObject.Account() { Id = Guid.Parse("{66F541B7-2A0C-4668-B2D6-4E6C6F556946}"), Name = "Account 1-A-i", ParentAccountId = act1a.ToEntityReference() }; var act1aii = new CrmObject.Account() { Id = Guid.Parse("{7C4DB64C-0D80-4092-8F31-D6EB7558ED39}"), Name = "Account 1-A-ii", ParentAccountId = act1a.ToEntityReference() }; var act1c = new CrmObject.Account() { Id = Guid.Parse("{70446EE5-8591-4141-9429-632F231B02F6}"), Name = "Account 1-C", ParentAccountId = act1.ToEntityReference() }; var act2 = new CrmObject.Account() { Id = Guid.Parse("{433AE6D5-3329-4167-9B98-A9E10A1D2F40}"), Name = "Account 2" }; var act3 = new CrmObject.Account() { Id = Guid.Parse("{985F7DBF-F8D7-41AF-A3FE-BE6B8912C931}"), Name = "Account 3" }; var act3a = new CrmObject.Account() { Id = Guid.Parse("{D0FF2AEA-A87D-4A08-90FD-523E4B7B52F3}"), Name = "Account 3-A", ParentAccountId = act3.ToEntityReference() }; var act4 = new CrmObject.Account() { Id = Guid.Parse("{184D539D-B869-41BB-AFE5-1BC8FA4EDAD0}"), Name = "Account 4" }; var accounts = new List <Entity>() { act1, act1a, act1ai, act1aii, act1c, act2, act3, act3a, act4 }; return(accounts); }
public bool IsAccountFrozen(CrmObject.Account accountEntity) { if (accountEntity == null) { throw new ArgumentNullException(nameof(accountEntity)); } return(accountEntity.think_frozenEnum.HasValue && accountEntity.think_frozenEnum.Value == CrmObject.Account_think_frozen.Yes); }
public void ProcessChangedFrozenFlag(ICrmService service, ILogging logging, CrmObject.Account account, bool forceChanges = false) { var l = new LocalLogger(logging, this.GetType().Name); if (service == null) { throw new ArgumentNullException(nameof(service)); } if (logging == null) { throw new ArgumentNullException(nameof(logging)); } if (account == null) { throw new ArgumentNullException(nameof(account)); } var isFrozen = this.IsAccountFrozen(account); l.Write($"IsFrozen={isFrozen}"); var accountQuery = new AccountQueries(service); List <CrmObject.Account> childAccounts; try { childAccounts = accountQuery.GetChildAccountsForAnAccount(account.ToEntityReference()); l.Write($"Retrieved {childAccounts.Count} child records."); } catch (Exception ex) { l.Write($"Exception retrieving accounts to update for account id: {account.Id}"); logging.Write(ex); throw; } var accountsToUpdate = this.UpdateAccountsFrozenFlag(childAccounts, isFrozen, forceChanges); l.Write($"Updating {accountsToUpdate.Count} records."); try { accountsToUpdate.ForEach(service.Update); l.Write("Completed CRM Updates."); } catch (Exception ex) { l.Write("Exception while updating records."); l.Write(ex); throw; } }
public void Does_Not_Call_Frozen_Account_Management_When_Frozen_Attribute_Did_Not_Change() { var pluginSetup = A.Fake <IPluginSetup>(); var ctx = A.Fake <IPluginExecutionContext>(); A.CallTo(() => pluginSetup.Context).Returns(ctx); A.CallTo(() => pluginSetup.Helper.GetTargetEntity <CrmObject.Account>()).ReturnsLazily(() => ((Entity)pluginSetup.Context.InputParameters["Target"]).ToEntity <CrmObject.Account>()); A.CallTo(() => ctx.ParentContext).Returns(null); var targetGuid = Guid.NewGuid(); var target = new CrmObject.Account() { Id = targetGuid, think_frozenEnum = CrmObject.Account_think_frozen.No }; var preImage = new CrmObject.Account() { Id = targetGuid, think_frozenEnum = CrmObject.Account_think_frozen.No }; A.CallTo(() => ctx.InputParameters) .Returns(new ParameterCollection { new KeyValuePair <string, object>("Target", target) }); A.CallTo(() => ctx.PreEntityImages).Returns(new EntityImageCollection { { "PreImage", preImage } }); var fakeFam = A.Fake <IFrozenAccountManagement>(); var pluginObj = new FrozenFlagChangedTestHarness(fakeFam); pluginObj.ExecutePostOpSync(pluginSetup); A.CallTo( () => fakeFam.ProcessChangedFrozenFlag(A <ICrmService> ._, A <ILogging> ._, A <CrmObject.Account> ._, A <bool> ._)) .MustHaveHappened(Repeated.Never); }
public void Runs_When_Given_An_Account_Target_And_Pre_Image() { var fakedContext = new XrmFakedContext { ProxyTypesAssembly = Assembly.GetAssembly(typeof(CrmObject.Account)) }; var ctx = fakedContext.GetDefaultPluginContext(); ctx.MessageName = "Create"; ctx.Mode = (int)ThinkCrm.Core.PluginCore.Helper.ExecutionMode.Synchronous; ctx.IsolationMode = (int)ThinkCrm.Core.PluginCore.Helper.IsolationMode.None; ctx.Stage = (int)ThinkCrm.Core.PluginCore.Helper.PipelineStage.Postoperation; var targetGuid = Guid.NewGuid(); var target = new CrmObject.Account() { Id = targetGuid, think_frozenEnum = CrmObject.Account_think_frozen.Yes }; var preImage = new CrmObject.Account() { Id = targetGuid, think_frozenEnum = CrmObject.Account_think_frozen.No }; ctx.InputParameters = new ParameterCollection { new KeyValuePair <string, object>("Target", target) }; ctx.PreEntityImages = new EntityImageCollection { { "PreImage", preImage } }; ctx.PrimaryEntityName = target.LogicalName; ctx.PrimaryEntityId = target.Id; fakedContext.ExecutePluginWith <FrozenFlagChangedTestHarness>(ctx); }