private void SaveToSiebel(bool isCreate)
        {
            bool sr_saved;
            ServiceRequest sr = null;
            string logMessage, logNote;

            // Make sure we actually have an asset and a saved incident
            if (Contact != null)
            {
                //Compose Service Request
                sr = new ServiceRequest();

                // Set Service Request attributes - severity, status, request type, contact party id, etc. 
                KeyValuePair<String, String> severityKeyPair = sr.rnSeverityToServerSeverity(selected_rn_severity_id);
                selected_server_severity = severityKeyPair.Key;
                sr.Severity = severityKeyPair.Key;

                KeyValuePair<String, String> statusKeyPair = sr.rnStatusToServerStatus(selected_rn_request_status_id);
                selected_server_request_status = statusKeyPair.Key;
                sr.Status = statusKeyPair.Key;

                KeyValuePair<String, String> requestTypeKeyPair = sr.rnRequestTypeToServerRequestType(selected_rn_request_type_id);
                selected_server_request_type = requestTypeKeyPair.Key;
                sr.RequestType = requestTypeKeyPair.Key;

                sr.ContactID = selectedContactPartyId;
                if (String.IsNullOrEmpty(selectedContactOrgId) || selectedContactOrgId == "0")
                {
                    sr.ContactOrgID = null;
                }
                else
                {
                    sr.ContactOrgID = selectedContactOrgId;
                }
                
                sr.Summary = Incident.Subject;
                sr.RequestDate = (Incident.Created.HasValue) ? Incident.Created.Value : DateTime.Now;
                sr.IncidentOccurredDate = (Incident.Created.HasValue) ? Incident.Created.Value : DateTime.Now;

                sr.IncidentRef = Incident.RefNo;
                sr.IncidentID = Incident.ID.ToString();

                sr.SerialNumber = current_serial_num;

                if(!String.IsNullOrEmpty(currentProductID)){
                    sr.ProductID = currentProductID;
                }
                else
                {
                    sr.ProductID = null;
                }

                if (_siebelDefaultSrOwnerId != null)
                {
                    sr.OwnerID = _siebelDefaultSrOwnerId;
                }


                try
                {
                    if (isCreate)
                    {
                        //Create Service Request
                        sr.RnowHost = ConfigurationSetting.rnt_host;
                        logMessage = "Ready to create Service Request";
                        _log.DebugLog(incidentId: _logIncidentId, logMessage: logMessage);
                        sr_saved = sr.Create(_logIncidentId, 0);
                    }
                    else
                    {
                        //Update Service Request
                        //Set custom attribute fields for update
                        var sr_id = "";
                        var sr_num = "";
                        var sr_owner_id = "";
                        IList<ICustomAttribute> incCustomAttributes = Incident.CustomAttributes;
                        string[] incCustomAttrs = { "Accelerator$siebel_sr_id", "Accelerator$siebel_sr_num", "Accelerator$siebel_sr_owner_id"};
                        Dictionary<String, Object> incCustomAttrsResults = CustomAttrHelper.fetchCustomAttrValue(incCustomAttributes, incCustomAttrs, this._logIncidentId, 0);
                        sr_id = incCustomAttrsResults["Accelerator$siebel_sr_id"] != null ? incCustomAttrsResults["Accelerator$siebel_sr_id"].ToString() : "";
                        sr_num = incCustomAttrsResults["Accelerator$siebel_sr_num"] != null ? incCustomAttrsResults["Accelerator$siebel_sr_num"].ToString() : "";
                        sr_owner_id = incCustomAttrsResults["Accelerator$siebel_sr_owner_id"] != null ? incCustomAttrsResults["Accelerator$siebel_sr_owner_id"].ToString() : "";

                        sr.RequestID = sr_id;
                        sr.RequestNumber = sr_num;
                        sr.RnowHost = ConfigurationSetting.rnt_host;
                        if (sr_owner_id != "")
                        {
                            sr.OwnerID = sr_owner_id;
                        }

                        logMessage = "Ready to update Service Request. SR ID = " + sr_id;
                        logNote = "";
                        _log.DebugLog(incidentId: _logIncidentId, logMessage: logMessage, logNote: logNote);

                        sr_saved = sr.Update(_logIncidentId, 0);
                    }
                }
                catch (Exception ex)
                {
                    logMessage = "Error in creating/updating Service Request.Error Message: " + ex.Message;
                    logNote = "";
                    _log.ErrorLog(incidentId: _logIncidentId, logMessage: logMessage, logNote: logNote);

                    string message = ex.Message;
                    MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (!sr_saved)
                {
                    // If Service Request is not saved successfully, show error.
                    string message = "There has been an error communicating with Siebel. Please check log for detail.";
                    MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    logMessage = "Error in creating/updating Service Request.";
                    logNote = "Response shows error code. Response's error message: " + sr.ErrorMessage;
                    _log.ErrorLog(incidentId: _logIncidentId, logMessage: logMessage, logNote: logNote);

                    return;
                }
                else
                {
                    logMessage = "Created/Updated Service Request successfully. SR ID = " + sr.RequestID;
                    logNote = "";
                    _log.DebugLog(incidentId: _logIncidentId, logMessage: logMessage, logNote: logNote);

                    // After update successfully, update the values which are stored information from Siebel side
                    try
                    {
                        storedRequestType = selected_server_request_type;
                        storedRequestStatus = selected_server_request_status;
                        storedSeverity = selected_server_severity;
                        siebelStoredSerialNum = current_serial_num;
                    }
                    catch (Exception ex)
                    {
                        logMessage = "Error in updating incident fields with Service Request information, after created/updated SR. Error Message: " + ex.Message;
                        _log.ErrorLog(incidentId: _logIncidentId, logMessage: logMessage);
                        return;
                    }

                    if (isCreate)
                    {
                        //Check the interaction, we need created, after created a new service request
                        string sr_id = sr.RequestID;
                        int currentThreadCount = Incident.Threads.Count == 0 ? 0 : Incident.Threads[0].ID;
                        int storedThreadsCount = 0;
                        IList<ICustomAttribute> incCustomAttributes = Incident.CustomAttributes;
                        string[] incCustomAttrThread = { "Accelerator$siebel_max_thread_id" };
                        Dictionary<String, Object> incCustomAttrResultThread = CustomAttrHelper.fetchCustomAttrValue(incCustomAttributes, incCustomAttrThread, this._logIncidentId, 0);
                        storedThreadsCount = incCustomAttrResultThread["Accelerator$siebel_max_thread_id"] != null ? (int)incCustomAttrResultThread["Accelerator$siebel_max_thread_id"] : 0;
                        

                        // If have new thread, then call function SaveInteractionToSiebel to create interaction
                        if (currentThreadCount != storedThreadsCount && !String.IsNullOrWhiteSpace(sr_id))
                        {
                            logMessage = "After created new SR, need to store new threads to Siebel System. Stored Thread Count = " + storedThreadsCount + "; Current Thread Count = " + currentThreadCount;
                            logNote = "";
                            _log.DebugLog(incidentId: _logIncidentId, logMessage: logMessage, logNote: logNote);

                            SaveNoteToSiebel(sr_id, currentThreadCount, storedThreadsCount, true);
                        }

                        // Iterate through the incident custom attributes and set values
                        try
                        {
                            bool sr_id_found = false;
                            bool sr_num_found = false;
                            bool sr_owner_id_found = false;
                            foreach (ICustomAttribute cusAttr in incCustomAttributes)
                            {
                                if (cusAttr.PackageName == "Accelerator" && cusAttr.GenericField.Name == "Accelerator$siebel_sr_id")
                                {
                                    sr_id_found = true;
                                    cusAttr.GenericField.DataValue.Value = sr.RequestID;
                                    UpdateIncCustomAttr(Incident.ID, "siebel_sr_id", sr.RequestID);
                                }
                                if (cusAttr.PackageName == "Accelerator" && cusAttr.GenericField.Name == "Accelerator$siebel_sr_num")
                                {
                                    ServiceRequest createdSR = sr.Lookup(sr.RequestID, _logIncidentId);
                                    sr_num_found = true;
                                    if (createdSR != null)
                                    {
                                        cusAttr.GenericField.DataValue.Value = createdSR.RequestNumber;
                                        UpdateIncCustomAttr(Incident.ID, "siebel_sr_num", createdSR.RequestNumber);
                                    }
                                }
                                if (cusAttr.PackageName == "Accelerator" && cusAttr.GenericField.Name == "Accelerator$siebel_sr_owner_id")
                                {
                                    sr_owner_id_found = true;
                                    cusAttr.GenericField.DataValue.Value = sr.OwnerID;
                                    UpdateIncCustomAttr(Incident.ID, "siebel_sr_owner_id", sr.OwnerID);
                                }
                            }

                            if (sr_id_found == false)
                            {
                                logMessage = "Custom attribute is not defined. Cannot get Accelerator$siebel_sr_id.";
                                _log.ErrorLog(incidentId:_logIncidentId, logMessage: logMessage);
                            }

                            if (sr_num_found == false)
                            {
                                logMessage = "Custom attribute is not defined. Cannot get Accelerator$siebel_sr_num.";
                                _log.ErrorLog(incidentId: _logIncidentId, logMessage: logMessage);
                            }

                            if (sr_owner_id_found == false)
                            {
                                logMessage = "Custom attribute is not defined. Cannot get Accelerator$siebel_sr_owner_id.";
                                _log.ErrorLog(incidentId: _logIncidentId, logMessage: logMessage);
                            }

                            if (sr_id_found == false || sr_num_found == false || sr_owner_id_found == false)
                            {
                                MessageBox.Show("Custom Attribute configuration missing. Please check log for detail.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                        catch (Exception ex)
                        {
                            logMessage = "Error in updating incident fields with Service Request information, after created new SR.Error Message: " + ex.Message;
                            _log.ErrorLog(incidentId: _logIncidentId, logMessage: logMessage);
                            return;
                        }

                        logMessage = " Updating incident fields with Service Request information, after created new SR successfully.siebel_sr_id = " + sr.RequestID.ToString() + "; siebel_sr_num = " + sr.RequestNumber + "; siebel_sr_owner_id = " + sr.OwnerID.ToString() + ".";
                        _log.DebugLog(incidentId: _logIncidentId, logMessage: logMessage);
                    }
                }
            }
        }
        private Boolean checkSRStatus(string sr_id)
        {

            string logMessage, logNote;

            if (String.IsNullOrWhiteSpace(sr_id))
                return false;

            // load SR
            ServiceRequest sr = new ServiceRequest();
            try
            {
                sr = sr.Lookup(sr_id, _logIncidentId, 0);
            }
            catch (Exception ex)
            {
                string message = ex.Message;
                MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                logMessage = "Error in loading Service Request. Error: " + ex.Message;
                logNote = "";
                _log.ErrorLog(incidentId: _logIncidentId, logMessage: logMessage, logNote: logNote);
                return false;
            }

            if (sr.ErrorMessage != null)
            {
                logMessage = "Loading Service Request is failed. SR ID = " + sr_id;
                logNote = "Response shows error code when loading service request. Response's error message: " + sr.ErrorMessage;
                _log.ErrorLog(incidentId: _logIncidentId, logMessage: logMessage, logNote: logNote);

                return false;
            }

            if (sr.Status == "Closed")
            {
                String message = "Activity cannot be saved in Siebel because the Service Request is closed.";
                MessageBoxButtons buttons = MessageBoxButtons.OK;
                DialogResult result;

                result = System.Windows.Forms.MessageBox.Show(this, message, "Warning", buttons, MessageBoxIcon.Warning);
                logMessage = "Cannot propagate the activity to Siebel.";
                logNote = message;
                _log.ErrorLog(logMessage: logMessage, logNote: logNote, incidentId: _logIncidentId);

                return false;
            }
            else
            {
                return true;
            }
        }
        private void bw_LoadSRDetails(object sender, DoWorkEventArgs e)
        {
            var sr_id = "";
            var sr_num = "";
            string logMessage, logNote;
            IList<ICustomAttribute> customAttributes = incident.CustomAttributes;
            string[] incCustomAttrs = { "Accelerator$siebel_sr_id", "Accelerator$siebel_sr_num", "Accelerator$siebel_serial_number" };
            Dictionary<String, Object> incCustomAttrsResults = CustomAttrHelper.fetchCustomAttrValue(customAttributes, incCustomAttrs, this._logIncidentId, 0);
            sr_id = incCustomAttrsResults["Accelerator$siebel_sr_id"] != null ? incCustomAttrsResults["Accelerator$siebel_sr_id"].ToString() : "";
            sr_num = incCustomAttrsResults["Accelerator$siebel_sr_num"] != null ? incCustomAttrsResults["Accelerator$siebel_sr_num"].ToString() : "";

            e.Result = null;
            if (!String.IsNullOrWhiteSpace(sr_id))
            {
                // Call to SR Lookup and Display SR Details
                ServiceRequest sr = new ServiceRequest();
                try
                {
                    sr = sr.Lookup(sr_id,  _logIncidentId, 0);
                }
                catch (Exception ex)
                {
                    string message = ex.Message;
                    MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    e.Cancel = true;
                    logMessage = "Error in loading Service Request. Error: " + ex.Message;
                    logNote = "";
                    _log.ErrorLog(incidentId: _logIncidentId, logMessage: logMessage, logNote: logNote);
                    return;
                }

                if (sr.ErrorMessage != null)
                {
                    e.Cancel = true;
                    logMessage = "Loading Service Request is failed. SR ID = " + sr_id;
                    logNote = "Response shows error code when loading service request. Response's error message: " + sr.ErrorMessage;
                    _log.ErrorLog(incidentId: _logIncidentId, logMessage: logMessage, logNote: logNote);

                    return;
                }

                e.Result = sr;

                //Set stored value (need them to decide whether field is changed)
                storedContactPartyId = (sr.ContactID == "0") ? "" : sr.ContactID;
                storedSeverity = sr.Severity;
                storedRequestStatus = sr.Status;
                storedRequestType =  sr.RequestType;
                storedSubject = sr.Summary;
                siebelStoredSerialNum = (sr.SerialNumber == null ? "" : sr.SerialNumber);
                siebelStoredProductID = (sr.ProductID == null ? "" : sr.ProductID);

                bool sr_owner_id = false;
                bool sr_num_ca = false;
                foreach (ICustomAttribute cusAttr in customAttributes)
                {
                    if (cusAttr.PackageName == "Accelerator" && cusAttr.GenericField.Name == "Accelerator$siebel_sr_owner_id")
                    {
                        sr_owner_id = true;
                        cusAttr.GenericField.DataValue.Value = !String.IsNullOrEmpty(sr.OwnerID)? sr.OwnerID : "";
                    }
                    if (cusAttr.PackageName == "Accelerator" && cusAttr.GenericField.Name == "Accelerator$siebel_sr_num")
                    {
                        sr_num_ca = true;
                        cusAttr.GenericField.DataValue.Value = !String.IsNullOrEmpty(sr.RequestNumber) ? sr.RequestNumber : "";
                    }

                }
                if (sr_num_ca == false)
                {
                    logMessage = "Custom attribute is not defined. Cannot get Accelerator$siebel_sr_num.";
                    ConfigurationSetting.logWrap.ErrorLog(incidentId: _logIncidentId, logMessage: logMessage);
                }

                if (sr_owner_id == false)
                {
                    logMessage = "Custom attribute is not defined. Cannot get Accelerator$siebel_sr_owner_id.";
                    ConfigurationSetting.logWrap.ErrorLog(incidentId: _logIncidentId, logMessage: logMessage);
                }

                if (sr_owner_id == false||sr_num_ca == false)
                {
                    MessageBox.Show("Custom Attribute configuration missing. Please check log for detail.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                logMessage = "Loaded Service Request. SR ID = " + sr_id;
                _log.DebugLog(incidentId: _logIncidentId, logMessage: logMessage);

            }
            else
            {
                //Display Empty page if no Service Request associated
                logMessage = "No Service Request associated. Show empty form.";
                logNote = "";
                _log.DebugLog(incidentId: _logIncidentId, logMessage: logMessage, logNote: logNote);
            }
        }