public async Task <IActionResult> UpdateAccount([FromBody] ViewModels.Account item, string id)
        {
            if (!string.IsNullOrEmpty(id) && Guid.TryParse(id, out Guid accountId))
            {
                _logger.LogInformation(LoggingEvents.HttpPut, "Begin method " + this.GetType().Name + "." + MethodBase.GetCurrentMethod().ReflectedType.Name);
                _logger.LogDebug(LoggingEvents.HttpPut, "Account parameter: " + JsonConvert.SerializeObject(item));
                _logger.LogDebug(LoggingEvents.HttpPut, "id parameter: " + id);


                if (!UserDynamicsExtensions.CurrentUserHasAccessToAccount(accountId, _httpContextAccessor, _dynamicsClient))
                {
                    _logger.LogWarning(LoggingEvents.NotFound, "Current user has NO access to the account.");
                    return(NotFound());
                }

                MicrosoftDynamicsCRMaccount account = _dynamicsClient.GetAccountByIdWithChildren(accountId);
                if (account == null)
                {
                    _logger.LogWarning(LoggingEvents.NotFound, "Account NOT found.");
                    return(new NotFoundResult());
                }

                // handle the contacts.

                UpdateContacts(item);

                // we are doing a patch, so wipe out the record.
                MicrosoftDynamicsCRMaccount patchAccount = new MicrosoftDynamicsCRMaccount();

                // copy values over from the data provided
                patchAccount.CopyValues(item);
                if (item.primaryContact != null && item.primaryContact.id != null &&
                    (account._primarycontactidValue == null || account._primarycontactidValue != item.primaryContact.id))
                {
                    patchAccount.PrimaryContactidODataBind = _dynamicsClient.GetEntityURI("contacts", item.primaryContact.id);
                }
                else
                {
                    if (account._primarycontactidValue != null && !item.primaryContact.HasValue())
                    {
                        // remove the reference.
                        try
                        {
                            // pass null as recordId to remove the single value navigation property
                            _dynamicsClient.Accounts.RemoveReference(accountId.ToString(), "primarycontactid", null);
                        }
                        catch (OdataerrorException odee)
                        {
                            _logger.LogError(LoggingEvents.Error, "Error updating the account.");
                            _logger.LogError("Request:");
                            _logger.LogError(odee.Request.Content);
                            _logger.LogError("Response:");
                            _logger.LogError(odee.Response.Content);
                            throw new OdataerrorException("Error updating the account.");
                        }

                        // delete the contact.
                        try
                        {
                            _dynamicsClient.Contacts.Delete(account._primarycontactidValue);
                        }
                        catch (OdataerrorException odee)
                        {
                            _logger.LogError(LoggingEvents.Error, "Error removing primary contact");
                            _logger.LogError("Request:");
                            _logger.LogError(odee.Request.Content);
                            _logger.LogError("Response:");
                            _logger.LogError(odee.Response.Content);
                            throw new OdataerrorException("Error updating the account.");
                        }
                    }
                }


                if (item.additionalContact != null && item.additionalContact.id != null &&
                    (account._bcgovAdditionalcontactValue == null || account._bcgovAdditionalcontactValue != item.additionalContact.id))
                {
                    patchAccount.AdditionalContactODataBind = _dynamicsClient.GetEntityURI("contacts", item.additionalContact.id);
                }
                else
                {
                    if (account._bcgovAdditionalcontactValue != null && !item.additionalContact.HasValue())
                    {
                        // remove the reference.
                        try
                        {
                            // pass null as recordId to remove the single value navigation property
                            _dynamicsClient.Accounts.RemoveReference(accountId.ToString(), "bcgov_AdditionalContact", null);
                        }
                        catch (OdataerrorException odee)
                        {
                            _logger.LogError(LoggingEvents.Error, "Error updating the account.");
                            _logger.LogError("Request:");
                            _logger.LogError(odee.Request.Content);
                            _logger.LogError("Response:");
                            _logger.LogError(odee.Response.Content);
                            throw new OdataerrorException("Error updating the account.");
                        }

                        // delete the contact.
                        try
                        {
                            _dynamicsClient.Contacts.Delete(account._bcgovAdditionalcontactValue);
                        }
                        catch (OdataerrorException odee)
                        {
                            _logger.LogError(LoggingEvents.Error, "Error removing additional contact");
                            _logger.LogError("Request:");
                            _logger.LogError(odee.Request.Content);
                            _logger.LogError("Response:");
                            _logger.LogError(odee.Response.Content);
                            throw new OdataerrorException("Error updating the account.");
                        }
                    }
                }


                try
                {
                    await _dynamicsClient.Accounts.UpdateAsync(accountId.ToString(), patchAccount);
                }
                catch (OdataerrorException odee)
                {
                    _logger.LogError(LoggingEvents.Error, "Error updating the account.");
                    _logger.LogError("Request:");
                    _logger.LogError(odee.Request.Content);
                    _logger.LogError("Response:");
                    _logger.LogError(odee.Response.Content);
                    throw new OdataerrorException("Error updating the account.");
                }

                // purge any existing non bceid accounts.
                _dynamicsClient.DeleteNonBceidBusinessContactLinkForAccount(_logger, accountId.ToString());

                // create the business contact links.
                if (item.primaryContact != null)
                {
                    _dynamicsClient.CreateBusinessContactLink(_logger, item.primaryContact.id, accountId.ToString(), null, (int?)ContactTypeCodes.Primary, item.primaryContact.title);
                }
                if (item.additionalContact != null)
                {
                    _dynamicsClient.CreateBusinessContactLink(_logger, item.additionalContact.id, accountId.ToString(), null, (int?)ContactTypeCodes.Additional, item.additionalContact.title);
                }


                // populate child items in the account.
                patchAccount = _dynamicsClient.GetAccountByIdWithChildren(accountId);

                var updatedAccount = patchAccount.ToViewModel();
                _logger.LogDebug(LoggingEvents.HttpPut, "updatedAccount: " +
                                 JsonConvert.SerializeObject(updatedAccount, Formatting.Indented, new JsonSerializerSettings {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                }));

                return(Json(updatedAccount));
            }
            else
            {
                return(new BadRequestResult());
            }
        }