/// <summary>
        /// Populate SR ComboBox based on Customer Name Entered
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void SR_Combobox_DropDown(object sender, EventArgs e)
        {
            if (_showDropDownList == true)
            {
                SR_Combobox.Items.Clear();
                string customerName = CustomerName_txtbx.Text;
                if (customerName != string.Empty && customerName.Contains("*"))
                {
                    customerName = customerName.Replace('*', '%');
                }
                string[] srList = RightNowConnectService.GetService().GetSR(customerName);
                if (srList != null)
                {
                    SR_Combobox.Items.Add(new KeyValuePair <int, string>(0, "[No Value]"));

                    foreach (string sr in srList)
                    {
                        int    key   = Convert.ToInt32(sr.Split('~')[0]);
                        string value = sr.Split('~')[1];
                        SR_Combobox.Items.Add(new KeyValuePair <int, string>(key, value));
                    }
                    SR_Combobox.DisplayMember = "Value";
                    SR_Combobox.ValueMember   = "Key";
                }
                else
                {
                    MessageBox.Show("No Sales Release Record Found");
                }
            }
            _showDropDownList = false;
        }
 /// <summary>
 /// Method called by data load event. It does the following:
 /// 1> Get Onload FSAR value
 /// 2> Get all affected VIN mapped to currently opened FSAR
 /// </summary>
 private void _recordContext_DataLoaded(object sender, System.EventArgs e)
 {
     _affectedBusId = new List <string>();
     _fsarRecord    = (IGenericObject)_recordContext.GetWorkspaceRecord("CO$FSAR");
     if (_fsarRecord != null)
     {
         string[] response = RightNowConnectService.GetService().GetAffectedBus(_fsarRecord.Id);
         if (response != null)
         {
             _affectedBusId = response.ToList();
         }
     }
 }
        /// <summary>
        /// Method which is called when any Workspace Rule Action is invoked.
        /// </summary>
        /// <param name="ActionName">The name of the Workspace Rule Action that was invoked.</param>
        public void RuleActionInvoked(string ActionName)
        {
            if (ActionName == "selectVIN")
            {
                _fsarRecord = (IGenericObject)_recordContext.GetWorkspaceRecord("CO$FSAR");

                //Get the List of Unique Model Names from Sales Release CO
                string[] modelList = RightNowConnectService.GetService().GetModel();


                SalesReleaseVINSelection form = new SalesReleaseVINSelection(modelList, _recordContext, _fsarRecord.Id, _affectedBusId);
                form.ShowDialog();
            }
        }
        public static RightNowConnectService GetService()
        {
            if (_rightnowConnectService != null)
            {
                return(_rightnowConnectService);
            }

            try
            {
                lock (_sync)
                {
                    if (_rightnowConnectService == null)
                    {
                        // Initialize client with current interface soap url
                        string          url      = WorkspaceAddIn._globalContext.GetInterfaceServiceUrl(ConnectServiceType.Soap);
                        EndpointAddress endpoint = new EndpointAddress(url);

                        BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);
                        binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;

                        // Optional depending upon use cases
                        binding.MaxReceivedMessageSize = 1024 * 1024;
                        binding.MaxBufferSize          = 1024 * 1024;
                        binding.MessageEncoding        = WSMessageEncoding.Mtom;

                        _rightNowClient = new RightNowSyncPortClient(binding, endpoint);

                        BindingElementCollection elements = _rightNowClient.Endpoint.Binding.CreateBindingElements();
                        elements.Find <SecurityBindingElement>().IncludeTimestamp = false;
                        _rightNowClient.Endpoint.Binding = new CustomBinding(elements);
                        WorkspaceAddIn._globalContext.PrepareConnectSession(_rightNowClient.ChannelFactory);

                        _rightnowConnectService = new RightNowConnectService();
                    }
                }
            }
            catch (Exception e)
            {
                _rightnowConnectService = null;
                WorkspaceAddIn.InfoLog(e.Message);
            }
            return(_rightnowConnectService);
        }
        /// <summary>
        /// Search for VIN records based on SR No or VIN No
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Search_Button_Click(object sender, EventArgs e)
        {
            SelectAll_Checkbox.Checked = false;
            ClearAll_CheckBox.Checked  = false;

            var filters = new List <KeyValuePair <string, string> >();

            #region SR and VIN null
            if ((SR_Combobox.SelectedItem == null || ((KeyValuePair <int, string>)SR_Combobox.SelectedItem).Key == 0) &&
                VIN_Textbox.Text == String.Empty && CustomerName_txtbx.Text == String.Empty && Model_ComboBox.SelectedItem == null)
            {
                if (DataGridView != null)
                {
                    clearDataGrid();//clear old view
                }
                MessageBox.Show("Select at least one Filter", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            #endregion

            #region SR not null

            if ((SR_Combobox.SelectedItem != null && ((KeyValuePair <int, string>)SR_Combobox.SelectedItem).Key != 0))
            {
                filters.Add(new KeyValuePair <string, string>("Sales Release", ((KeyValuePair <int, string>)SR_Combobox.SelectedItem).Value));
            }
            #endregion

            #region VIN not null
            if (VIN_Textbox.Text != String.Empty)
            {
                filters.Add(new KeyValuePair <string, string>("VIN", VIN_Textbox.Text));
            }
            #endregion

            #region if Customer Name is not null
            if (CustomerName_txtbx.Text != String.Empty)
            {
                filters.Add(new KeyValuePair <string, string>("Customer Name", CustomerName_txtbx.Text));
            }
            #endregion

            #region if Model Name is not null
            if (Model_ComboBox.SelectedItem != null && ((KeyValuePair <int, string>)Model_ComboBox.SelectedItem).Key != 0)
            {
                filters.Add(new KeyValuePair <string, string>("Model Name", ((KeyValuePair <int, string>)Model_ComboBox.SelectedItem).Value));
            }
            #endregion
            CSVTable resulttable = RightNowConnectService.GetService().GetReportDetails(filters);

            if (resulttable.Rows.Length > 0)
            {
                PopulateGrid(resulttable);
                SelectExistingVins();
            }
            else
            {
                if (DataGridView != null)
                {
                    clearDataGrid();//clear old view
                }
                MessageBox.Show("No data found");
            }
        }
        /// <summary>
        /// Fucntion to Create Records
        /// </summary>
        private void CreateRecords()
        {
            try
            {
                int           srOrganizationID   = 0;
                int           srID               = 0;
                int           primaryContact     = 0;
                int           internalIncidentID = 0;
                int           reportingIncID     = 0;
                List <string> srOrgAndContact    = new List <string>();
                //Get Unselected and Selected Vins
                foreach (DataGridViewRow _row in DataGridView.Rows)
                {
                    if (Convert.ToBoolean(_row.Cells["Select_CheckBox"].Value) == false)
                    {
                        _unselectedIDs.Add(_row.Cells["Bus ID"].Value.ToString());
                    }
                    if (Convert.ToBoolean(_row.Cells["Select_CheckBox"].Value) == true)
                    {
                        _selectedIDs.Add(_row.Cells["Bus ID"].Value.ToString());
                    }
                }

                #region Unselected Vins
                if (_unselectedIDs != null && _unselectedIDs.Count > 0)
                {
                    List <int> deleteVins = new List <int>();

                    if (_existingAffectedVins != null && _existingAffectedVins.Count > 0)
                    {
                        foreach (string uvin in _unselectedIDs)
                        {
                            foreach (string evin in _existingAffectedVins)
                            {
                                int busId = Convert.ToInt32(evin.Split('~')[0]);// First element is bus id and second is Incident_VIN ID

                                if (Convert.ToInt32(uvin) == busId)
                                {
                                    // First element is bus id and second is Incident_VIN ID
                                    deleteVins.Add(Convert.ToInt32(evin.Split('~')[1]));
                                }
                            }
                        }
                    }

                    if (deleteVins.Count > 0)
                    {
                        _buildCount++;
                        RightNowConnectService.GetService().DeleteIncidentVIN(deleteVins);
                    }
                }
                #endregion

                #region Selected Vins
                if (_selectedIDs != null && _selectedIDs.Count > 0)
                {
                    KeyValuePair <int, string> selectedItem = (KeyValuePair <int, string>)SR_Combobox.SelectedItem;
                    srID = selectedItem.Key;

                    string response = RightNowConnectService.GetService().GetOrgAndPrimaryContact(srID);
                    if (response != null)
                    {
                        srOrganizationID = Convert.ToInt32(response.Split('~')[1]);
                        primaryContact   = Convert.ToInt32(response.Split('~')[0]);
                    }
                    if (primaryContact != 0)
                    {
                        reportingIncID = RightNowConnectService.GetService().CreateReportingIncident(primaryContact, _fsarID,
                                                                                                     srOrganizationID);

                        if (srID != 0 && reportingIncID != 0)
                        {
                            internalIncidentID = RightNowConnectService.GetService().CreateInternalIncident(primaryContact, srID,
                                                                                                            reportingIncID, _fsarID,
                                                                                                            srOrganizationID);
                        }

                        if (internalIncidentID != 0)
                        {
                            List <int> addVins = new List <int>();

                            for (int i = 0; i < _selectedIDs.Count; i++)
                            {
                                if (CheckIfVinExist(_selectedIDs[i]) == false) //to make sure duplicate records are not created
                                {
                                    addVins.Add(Convert.ToInt32(_selectedIDs[i]));
                                    _existingAffectedVins.Add(_selectedIDs[i]);
                                }
                            }
                            if (addVins.Count > 0)
                            {
                                RightNowConnectService.GetService().createIncidentVIN(addVins, internalIncidentID);
                            }

                            if (this.IsHandleCreated)
                            {
                                this.BeginInvoke(new Action(() =>
                                {
                                    form.Hide();
                                }));
                            }
                            _buildCount++;
                            MessageBox.Show(this, "Build Complete");
                        }
                        else
                        {
                            if (this.IsHandleCreated)
                            {
                                this.BeginInvoke(new Action(() =>
                                {
                                    form.Hide();
                                }));
                            }

                            MessageBox.Show(this, "Internal Incident is not created", "Attention");
                        }
                    }
                    else
                    {
                        if (this.IsHandleCreated)
                        {
                            this.BeginInvoke(new Action(() =>
                            {
                                form.Hide();
                            }));
                        }
                        MessageBox.Show(this, "No Primary contact found for sale release :: " + selectedItem.Value, "Attention");
                    }
                    #endregion
                }
                if (this.IsHandleCreated)
                {
                    this.BeginInvoke(new Action(() =>
                    {
                        form.Hide();
                    }));
                }
            }
            catch (Exception ex)
            {
                if (this.IsHandleCreated)
                {
                    this.BeginInvoke(new Action(() =>
                    {
                        form.Hide();
                    }));
                }
                WorkspaceAddIn.InfoLog("Exception in CreateRecords: " + ex.Message);
            }
        }