private void ExportEnvironmentUpdates()

in 2LCS/Forms/MainForm.cs [725:799]


        private void ExportEnvironmentUpdates(LCSEnvironments _LCSEnvironments)
        {
            notifyIcon.BalloonTipText = $"Exporting list of Environment Updates to {_LCSEnvironments} environments for current project. Please wait...";
            notifyIcon.BalloonTipTitle = $"Exporting list of Environment Updates";

            notifyIcon.ShowBalloonTip(2000); //This setting might be overruled by the OS
                       
            var previousProject = _selectedProject;
            var exportedActionDetailList = new List<ActionDetails>();
           
            _httpClientHelper.ChangeLcsProjectId(_selectedProject.Id.ToString());
            _httpClientHelper.LcsProjectTypeId = _selectedProject.ProjectTypeId;
            RefreshChe();
            RefreshSaas();

            if (_LCSEnvironments == LCSEnvironments.ALL || _LCSEnvironments == LCSEnvironments.SAAS)
                if (_saasInstancesList != null && _saasInstancesList.Count > 0)
                {
                    foreach (var _instance in _saasInstancesList)
                    {
                        List<ActionDetails> actions = _httpClientHelper.GetEnvironmentHistoryDetails(_instance);
                        if (actions != null)
                        {
                            foreach (ActionDetails _action in actions)
                            {
                                _action.EnvironmentName = _instance.DisplayName;
                                exportedActionDetailList.Add(_action);
                            }
                        }
                    }                
            }
            if (_LCSEnvironments == LCSEnvironments.ALL || _LCSEnvironments == LCSEnvironments.CHE)
                if (_cheInstancesList != null && _cheInstancesList.Count > 0)
                {
                    foreach (var _instance in _cheInstancesList)
                    {
                        List<ActionDetails> actions = _httpClientHelper.GetEnvironmentHistoryDetails(_instance);
                        if (actions != null)
                        {
                            foreach (ActionDetails _action in actions)
                            {
                                _action.EnvironmentName = _instance.DisplayName;
                                exportedActionDetailList.Add(_action);
                            }
                        }
                    }
            }

            SaveFileDialog savefile = new SaveFileDialog
            {
                FileName = $"D365FO {_LCSEnvironments} environment updates - 2LCS generated.csv",
                Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*"
            };

            if (savefile.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    using StreamWriter sw = new StreamWriter(savefile.FileName, false, Encoding.Unicode);
                    var csv = new CsvWriter(sw, CultureInfo.CurrentCulture);
                    csv.WriteRecords(exportedActionDetailList);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            _selectedProject = previousProject;
            _httpClientHelper.ChangeLcsProjectId(_selectedProject.Id.ToString());
            _httpClientHelper.LcsProjectTypeId = _selectedProject.ProjectTypeId;
            SetLcsProjectText();

            RefreshChe(false);
            RefreshSaas(false);
        }