//Edit name property by replacing string with another string public string ReplaceInName(AccountWithNameModel account, string wordToReplace, string replaceWith) { log.Info($"Account id: {account.Id} name: {account.FullName} will be edited"); try { using (XrmServiceContext context = new XrmServiceContext(this.service)) { var newName = Regex.Replace(account.FullName, TO_REPLACE, REPLACE_WITH, RegexOptions.IgnoreCase); var accountToChange = new Account { Id = account.Id, Name = newName }; this.service.Update(accountToChange); log.Info($"AccountId: {accountToChange.Id} new name is: {newName}"); return(newName); } } catch (Exception ex) { log.Error($"Exception throw during name chaneg: {ex.Message}"); Console.WriteLine($"Exception throw during name chaneg: {ex.Message}"); return(null); }; }
//Buisness logic goes here public void Run() { log.Info("Engine will run...."); //Retrieve Accounts that satisfy condition var accounts = accountService.AllContainingInName(TO_REPLACE); foreach (var account in accounts) { var accountToModify = new AccountWithNameModel { Id = account.Id, FullName = account.FullName }; //Replace in name var changedName = accountService.ReplaceInName(accountToModify, TO_REPLACE, REPLACE_WITH); //If name was not changed new contact will not be created if (string.IsNullOrEmpty(changedName)) { //LOG ERROR log.Info("Name was not edited successfully!"); continue; } //From full name split Last name and all othe to be First name SplitName(changedName, out string firstName, out string lastName); //Creat new contact var newContactId = contactService.CreateContact(account.Email, firstName, lastName, account.Address); if (newContactId == Guid.Empty) { log.Error($"New contact with was NOT created!"); } else { log.Info($"New contact with Id: {newContactId} was created!"); } } }