public static void Save(LocationAdminDetailView parent, SubEntity insurances, int orgUnitId)
        {
            var partiallySaved = false;

            try
            {
                insurances.ErrorMessage = string.Empty;

                //Existing Records Added
                foreach (var record in insurances.Records.Where(r => r.Action == RecordActions.Added))
                {
                    var request = new AddOrgUnitInsuranceRequest()
                    {
                        OrgUnitId = orgUnitId,
                        InsuranceId = int.Parse(parent.GetFieldValue(record.Fields, _insuranceIdField))
                    };

                    parent.ProcessRequest<AddOrgUnitInsuranceResponse>(request);
                    partiallySaved = true;
                }

                //New Records Created
                foreach (var record in insurances.Records.Where(r => r.Action == RecordActions.New))
                {
                    //Add New OrgUnit insurance
                    var addInsuranceRequest = new AddInsuranceRequest()
                    {
                        Name = parent.GetFieldValue(record.Fields, _nameField),
                        IsEnabled = true
                    };
                    var addedInsuranceId = parent.ProcessRequest<AddInsuranceResponse>(addInsuranceRequest).Id;

                    //Add newly created insurance to OrgUnit
                    var request = new AddOrgUnitInsuranceRequest()
                    {
                        OrgUnitId = orgUnitId,
                        InsuranceId = addedInsuranceId
                    };

                    parent.ProcessRequest<AddOrgUnitInsuranceResponse>(request);
                    partiallySaved = true;
                }

                //Deletes
                foreach (var record in insurances.Records.Where(r => r.Action == RecordActions.Deleted))
                {
                    var request = new RemoveOrgUnitInsuranceRequest()
                    {
                        Id = int.Parse(parent.GetFieldValue(record.Fields, _idField))
                    };
                    parent.ProcessRequest<RemoveOrgUnitInsuranceResponse>(request);
                    partiallySaved = true;
                }
            }
            catch (Exception ex)
            {
                insurances.ErrorMessage = ex.Message;

                if(!partiallySaved)
                    throw;
            }

            insurances = BuildLocationInsurancesSubEntity(parent, orgUnitId);
        }
        private static void SaveInsurances(ProviderAdminDetailView parent, SubEntity subentity, int providerOrgUnitId)
        {
            try
            {
                //New Records
                foreach (var record in subentity.Records.Where(r => r.Action == RecordActions.New))
                {
                    var addServiceRequest = new AddInsuranceRequest()
                    {
                        Name = parent.GetFieldValue(record.Fields, "InsuranceName"),
                        IsEnabled = true
                    };
                    var serviceResponseId = parent.ProcessRequest<AddInsuranceResponse>(addServiceRequest).Id;

                    var request = new AddProviderOrgUnitInsuranceRequest()
                    {
                        ProviderOrgUnitId = providerOrgUnitId,
                        InsuranceId = serviceResponseId
                    };
                    parent.ProcessRequest<AddProviderOrgUnitInsuranceResponse>(request);
                }

                //Added Records
                foreach (var record in subentity.Records.Where(r => r.Action == RecordActions.Added))
                {
                    var request = new AddProviderOrgUnitInsuranceRequest()
                    {
                        ProviderOrgUnitId = providerOrgUnitId,
                        InsuranceId = int.Parse(parent.GetFieldValue(record.Fields, _insuranceId))
                    };
                    parent.ProcessRequest<AddProviderOrgUnitInsuranceResponse>(request);
                }
                foreach (var record in subentity.Records.Where(r => r.Action == RecordActions.Deleted))
                {
                    var request = new DeleteProviderOrgUnitInsuranceRequest()
                    {
                        ProviderOrgUnitId = providerOrgUnitId,
                        InsuranceId = int.Parse(parent.GetFieldValue(record.Fields, _insuranceId))
                    };
                    parent.ProcessRequest<DeleteProviderOrgUnitInsuranceResponse>(request);
                }
            }
            catch (Exception ex)
            {
                subentity.ErrorMessage = ex.Message;
                throw;
            }
        }
        private void Save()
        {
            _insurancesId = int.Parse(CommonUtils.GetFieldValue(_viewModel.Fields, _id), CultureInfo.InvariantCulture);

            if (_insurancesId == 0)
            {
                var addRequest = new AddInsuranceRequest();

                var nameValue = GetFieldValue(_viewModel.Fields, _name);
                addRequest.Name = nameValue;
                var isEnabledValue = GetFieldValue(_viewModel.Fields, _isEnabled);
                addRequest.IsEnabled = bool.Parse(isEnabledValue);

                _insurancesId = ProcessRequest<AddInsuranceResponse>(addRequest).Id;
            }
            else
            {
                if (_viewModel.Action == RecordActions.Edited)
                {
                    var updateRequest = new UpdateInsuranceRequest();

                    var idValue = GetFieldValue(_viewModel.Fields, _id);
                    updateRequest.Id = int.Parse(idValue, CultureInfo.InvariantCulture);
                    var nameValue = GetFieldValue(_viewModel.Fields, _name);
                    updateRequest.Name = nameValue;
                    var isEnabledValue = GetFieldValue(_viewModel.Fields, _isEnabled);
                    updateRequest.IsEnabled = bool.Parse(isEnabledValue);

                    ProcessRequest<UpdateInsuranceResponse>(updateRequest);
                }
            }
        }