public bool UpdateRegistration(new_registration registration) { try { // Creating new registration object for update _log.Info($"Updating registration {registration.Id}..."); var registrationToUpdate = new new_registration { Id = registration.Id, new_RegistrationStatus = new EntityReference(new_registrationstatus.EntityLogicalName, Guid.Parse(RegistrationStatusCancelled)), new_RegistrationSubStatus = new EntityReference(new_registrationsubstatus.EntityLogicalName, Guid.Parse(RegistrationSubstatusCancelledByTask)), new_ModifiedBy = new EntityReference(SystemUser.EntityLogicalName, _userId), new_ModifiedOn = DateTime.Now }; _service.Update(registrationToUpdate); _log.Info($"Successfully updated registration {registration.Id} with the following values: " + $"Status {registrationToUpdate.new_RegistrationStatus.Name}, " + $"SubStatus {registrationToUpdate.new_RegistrationSubStatus.Name}, " + $"Modified By {registrationToUpdate.new_ModifiedBy.Name}, " + $"Modified On {DateTime.Now}"); return(true); } catch (Exception ex) { _log.Error($"Exception caught during updating registration {registration.Id} - {ex.Message}"); return(false); } }
public bool CancellByTask(string registrationId, Guid cancelStatusId, Guid canceledSubStatusId) { log.Info($"Registration to cancel Id: {registrationId}"); try { var regToUpdate = new new_registration(); regToUpdate.Id = Guid.Parse(registrationId); //Set RegistrationStatuses regToUpdate.new_registraionstatus = new EntityReference(new_registrationstatus.EntityLogicalName, cancelStatusId); regToUpdate.new_registrationsubstatus = new EntityReference(new_registrationsubstatus.EntityLogicalName, canceledSubStatusId); crmConnection.Service.Update(regToUpdate); log.Info("Updatet sucessfully"); return(true); } catch (Exception ex) { log.Error($"{nameof(CancellByTask)} throws exception: {ex.Message}"); Console.WriteLine(ex.Message); return(false); } }
public void ExecuteTask() { if (_contactList == null || _caseList == null || _registrationList == null || _template == null) { _log.Info($"Exiting {nameof(RegistrationScheduledTasks)}"); return; } foreach (var contact in _contactList) { try { // Get cases offline related to the current iteration of contact _log.Info($"Filtering cases related to contact {contact.LastName} with id {contact.Id}"); List <Incident> currentAccountCases = _caseList.Where(x => x.CustomerId.Id == contact.Id).ToList(); _log.Info($"Retrieved {currentAccountCases.Count} cases for contact id {contact.Id}"); foreach (var currentCase in currentAccountCases) { try { // Get registration with the lowest priority, if two registrations found with the same priority take the last created one _log.Info( $"Filtering registrations related to account {contact.Account.Id} with the lowest priority"); new_registration currentRegistration = _registrationList .Where(x => x.new_Account.Id == contact.Account.Id) .OrderBy(p => p.new_Priority) .ThenByDescending(c => c.CreatedOn) .FirstOrDefault(); if (currentRegistration == null) { _log.Error( $"No registration found for account {contact.Account.Name} with id {contact.Account.Id}"); continue; } _currentRegistrationName = currentRegistration.new_registrationname; _currentCaseName = currentCase.Title; _log.Info( $"Retrieved registration with name {_currentRegistrationName} for case {_currentCaseName}"); bool updateSuccessful = _registrationService.UpdateRegistration(currentRegistration); if (!updateSuccessful) { _issuesList.Add(new IssueModel(contact.Account.Name, currentRegistration.new_registrationname, "Update was not successful")); } bool caseResolved = _caseService.ResolveCase(currentCase); if (!caseResolved) { _issuesList.Add(new IssueModel(contact.Account.Name, currentRegistration.new_registrationname, "Case was not resolved successfully")); } bool emailSent = _emailService.CreateEmailFromTemplate(contact, currentCase, currentRegistration.new_registrationname, _template); if (!emailSent) { _issuesList.Add(new IssueModel(contact.Account.Name, currentRegistration.new_registrationname, "Email not created successfully")); } // Check if there was an issue with this account and if not add it to the successfully updated registrations list if (_issuesList.All(x => x.AccountName != contact.Account.Name)) { _registrationsUpdatedList.Add(new RegistrationModel(contact.Account.Name, _currentRegistrationName, _currentCaseName)); } } catch (Exception ex) { _log.Error( $"Exception caught ïn {nameof(RegistrationScheduledTasks)} for case {currentCase.Id} - {ex.Message}"); } } } catch (Exception ex) { _log.Error( $"Exception caught ïn {nameof(RegistrationScheduledTasks)} for contact {contact.Id} - {ex.Message}"); } } // Send Registration update summary mail to all task owners foreach (var taskOwner in _registrationScheduledTaskOwners) { bool emailSent = _emailService.SendEmailToRegistrationScheduledTaskOwners(taskOwner, _registrationsUpdatedList, _issuesList); if (emailSent) { _log.Info($"Registration update summary mail successfully sent to {taskOwner}"); } else { _log.Error($"Registration update summary mail was NOT successfully sent to {taskOwner}"); } } }