/// <summary>
        /// Delete Internal Incident Records
        /// </summary>
        /// <param name="deleteIncIDs"></param>
        /// <returns></returns>
        public void DeleteInternalIncident(List <int> deleteIncIDs)
        {
            try
            {
                List <RNObject> deleteObject = new List <RNObject>();
                for (int i = 0; i < deleteIncIDs.Count; i++)
                {
                    Incident incObj = new Incident();
                    incObj.ID = new ID
                    {
                        id          = deleteIncIDs[i],
                        idSpecified = true
                    };

                    deleteObject.Add(incObj);
                }

                //BatchResponseItem[] batchRes = rspc.Batch(clientInfoHeader, requestItems);
                callBatchJob(getDestroyMsg(deleteObject));
            }
            catch (Exception ex)
            {
                //SR mean internal incident
                WorkspaceAddIn.InfoLog("Exception in Deleting Internal Incident record: " + ex.Message);
            }
            return;
        }
        /// <summary>
        /// Batch Operation to create Incident_VIN records
        /// </summary>
        /// <param name="busIDs">List of Bus Ids</param>
        /// <param name="internalIncID">Internal Incident ID</param>
        /// <returns></returns>
        public BatchRequestItem createIncidentVIN(List <int> busIDs, int internalIncID)
        {
            try
            {
                List <RNObject> createObject = new List <RNObject>();
                foreach (int busID in busIDs)
                {
                    GenericObject genObj = new GenericObject
                    {
                        ObjectType = new RNObjectType
                        {
                            Namespace = "CO",
                            TypeName  = "Incident_VIN"
                        }
                    };
                    List <GenericField> gfs = new List <GenericField>();
                    gfs.Add(createGenericField("Bus", createNamedIDDataValue(busID), DataTypeEnum.NAMED_ID));
                    gfs.Add(createGenericField("Incident", createNamedIDDataValue(internalIncID), DataTypeEnum.NAMED_ID));
                    genObj.GenericFields = gfs.ToArray();

                    createObject.Add(genObj);
                }
                callBatchJob(getCreateMsg(createObject));
            }
            catch (Exception ex)
            {
                WorkspaceAddIn.InfoLog("Exception in creating FSAR_VIN records: " + ex.Message);
            }
            return(null);
        }
        /// <summary>
        /// Get Result of Report based on Filter's passed
        /// </summary>
        /// <param name="filters">List of filter name and Values</param>
        /// <returns>CSVTable</returns>
        public CSVTable GetReportDetails(List <KeyValuePair <string, string> > filters)
        {
            try
            {
                AnalyticsReport analyticsreport = new AnalyticsReport {
                    ID = new ID {
                        id = WorkspaceAddIn._reportID, idSpecified = true
                    }
                };
                List <AnalyticsReportFilter> analyticsfilter = new List <AnalyticsReportFilter>();

                foreach (KeyValuePair <string, string> filter in filters)
                {
                    string filterName = filter.Key;
                    if (filterName == "Customer Name")
                    {
                        analyticsfilter.Add(new AnalyticsReportFilter
                        {
                            Name     = filter.Key,
                            Operator = new NamedID
                            {
                                ID = new ID
                                {
                                    id          = 7,
                                    idSpecified = true
                                }
                            },
                            Values = new string[] { filter.Value }
                        });
                    }
                    else
                    {
                        analyticsfilter.Add(new AnalyticsReportFilter
                        {
                            Name     = filter.Key,
                            Operator = new NamedID
                            {
                                ID = new ID
                                {
                                    id          = 1,
                                    idSpecified = true
                                }
                            },
                            Values = new string[] { filter.Value }
                        });
                    }
                }
                analyticsreport.Filters = analyticsfilter.ToArray();
                CSVTableSet tableset = RunReport(analyticsreport);
                if (tableset.CSVTables.Length > 0)
                {
                    return(tableset.CSVTables[0]);
                }
            }
            catch (Exception ex)
            {
                WorkspaceAddIn.InfoLog("Exception in GetReportDetails: " + ex.Message);
            }
            return(null);
        }
 /// <summary>
 /// Get List of all the SR
 /// </summary>
 /// <param name=""></param>
 /// <returns> string[] </returns>
 public string[] GetSR(string orgName)
 {
     try
     {
         string query = "";
         if (orgName != string.Empty)
         {
             query = "select S.ID, S.sr_nmbr from CO.SalesRelease S where S.organization.Name LIKE" + "'" + orgName + "'" + "ORDER BY S.sr_nmbr";
         }
         else
         {
             query = "select S.ID, S.sr_nmbr from CO.SalesRelease S";
         }
         string[] resultSet = GetRNData("Get All SR", query);
         if (resultSet != null && resultSet.Length > 0)
         {
             return(resultSet);
         }
     }
     catch (Exception ex)
     {
         WorkspaceAddIn.InfoLog("Exception in getting SR List " + ex.Message);
     }
     return(null);
 }
        /// <summary>
        /// Return individual fields as per query
        /// </summary>
        /// <param name="ApplicationID"></param>
        /// <param name="Query"></param>
        /// <returns> array of string delimited by '~'</returns>
        private string[] GetRNData(string ApplicationID, string Query)
        {
            string[]         rnData = null;
            ClientInfoHeader hdr    = new ClientInfoHeader()
            {
                AppID = ApplicationID
            };

            byte[]      output = null;
            CSVTableSet data   = null;

            try
            {
                data = _rightNowClient.QueryCSV(hdr, Query, 1000, "~", false, false, out output);
                string dataRow = String.Empty;
                if (data != null && data.CSVTables.Length > 0 && data.CSVTables[0].Rows.Length > 0)
                {
                    return(data.CSVTables[0].Rows);
                }
            }
            catch (Exception ex)
            {
                WorkspaceAddIn.InfoLog(ex.Message);
            }
            return(rnData);
        }
        /// <summary>
        /// Batch Request to Delete Incident_VIN records
        /// </summary>
        /// <param name="delete_id">List of Vins need to be deleted</param>
        /// <returns></returns>
        public void DeleteIncidentVIN(List <int> deleteVins)
        {
            try
            {
                List <RNObject> deleteObject   = new List <RNObject>();
                List <int>      internalIncIDs = new List <int>(); // Didi - Nov 2018 - neeed to delete inc vin
                String          query;
                String[]        resultset;

                for (int i = 0; i < deleteVins.Count; i++)
                {
                    GenericObject genObj = new GenericObject
                    {
                        ObjectType = new RNObjectType
                        {
                            Namespace = "CO",
                            TypeName  = "Incident_VIN"
                        }
                    };
                    genObj.ID = new ID
                    {
                        id          = deleteVins[i],
                        idSpecified = true
                    };

                    query     = "SELECT IV.Incident.ID as iiID FROM CO.Incident_VIN IV WHERE ID = " + deleteVins[i];
                    resultset = GetRNData("Get interntal inc info", query);
                    if (resultset != null && resultset.Length > 0)
                    {
                        internalIncIDs.Add(Convert.ToInt32(resultset[0]));//store the internal inc ID
                    }

                    deleteObject.Add(genObj);
                }
                //BatchResponseItem[] batchRes = rspc.Batch(clientInfoHeader, requestItems);
                callBatchJob(getDestroyMsg(deleteObject));

                // Delete internal inc
                internalIncIDs = internalIncIDs.Distinct().ToList();

                foreach (int internalIncID in internalIncIDs)
                {
                    IncidentVinCountForInternalInc(internalIncID);//check if Internal incident is empty, if so then delete that
                }
                //*end delete intenal inc
            }
            catch (Exception ex)
            {
                WorkspaceAddIn.InfoLog("Excpetion in Deleting FSAR_VIN record: " + ex.Message);
            }
            return;
        }
 /// <summary>
 /// Get List of all the Model
 /// </summary>
 /// <param name=""></param>
 /// <returns> string[] </returns>
 public string[] GetModel()
 {
     try
     {
         string   query     = "select S.model from CO.SalesRelease S GROUP BY S.model ORDER BY S.model";
         string[] resultSet = GetRNData("Get All Model", query);
         if (resultSet != null && resultSet.Length > 0)
         {
             return(resultSet);
         }
     }
     catch (Exception ex)
     {
         WorkspaceAddIn.InfoLog("Exception in getting Model List " + ex.Message);
     }
     return(null);
 }
 /// <summary>
 /// Check if reporting incident exist for the FSAR
 /// </summary>
 /// <param name="fsarID"></param>
 /// <returns></returns>
 public string checkIfReportingIncidentExistforFSAR(int fsarID, int contactID)
 {
     try
     {
         string query = "select I.ID from Incident I where I.CustomFields.c.incident_type.LookupName = 'Reported Incident'" +
                        " AND I.CustomFields.CO.FSAR = " + fsarID + " AND I.PrimaryContact.ParentContact.ID = " + contactID;
         string[] resultset = GetRNData("Get Reported Incident", query);
         if (resultset != null && resultset.Length > 0)
         {
             return(resultset[0]);
         }
     }
     catch (Exception ex)
     {
         WorkspaceAddIn.InfoLog("Exception in Checking exisitng reported incident: " + ex.Message);
     }
     return(null);
 }
 /// <summary>
 /// Check if Internal Incident exist for the selected SR
 /// </summary>
 /// <param name="repIncID">Reported Incident ID</param>
 /// <param name="srID"></param>
 /// <returns></returns>
 public string checkIfInternalIncidentExistForSR(int repIncID, int srID)
 {
     try
     {
         string query = "Select ID from Incident where CustomFields.CO.reporting_incident = " + repIncID +
                        " AND CustomFields.CO.SalesRelease = " + srID;
         string[] resultset = GetRNData("Get Internal Incident", query);
         if (resultset != null && resultset.Length > 0)
         {
             return(resultset[0]);
         }
     }
     catch (Exception ex)
     {
         WorkspaceAddIn.InfoLog("Exception in getting exisitng internal incident for SR: " + ex.Message);
     }
     return(null);
 }
 /// <summary>
 /// Get all affected Bus for all the Internal Incidents associated with FSAR
 /// </summary>
 /// <param name="fsarID"></param>
 /// <returns> array of bus id in string format</returns>
 public string[] GetAffectedBus(int fsarID)
 {
     try
     {
         string query = "select V.Bus, V.ID from CO.Incident_VIN V where " +
                        "V.Incident.CustomFields.CO.reporting_incident.CustomFields.CO.FSAR = " + fsarID;
         string[] resultSet = GetRNData("Get Affected Bus", query);
         if (resultSet != null && resultSet.Length > 0)
         {
             return(resultSet);
         }
     }
     catch (Exception ex)
     {
         WorkspaceAddIn.InfoLog("Exception in getting exisitng affected Bus IDs: " + ex.Message);
     }
     return(null);
 }
 /// <summary>
 /// Get Organization and Primary Contact for the selected SR
 /// </summary>
 /// <param name="salesreleaseID"></param>
 /// <returns></returns>
 public string GetOrgAndPrimaryContact(int salesReleaseID)
 {
     try
     {
         string query = "select O.Contacts.ID as c_id, O.ID as org_id" +
                        " from CO.SalesRelease SR INNER JOIN SR.organization O" +
                        " where O.Contacts.CustomFields.CO.primary_contact = 1 AND SR.ID =" + salesReleaseID;
         string[] resultset = GetRNData("Get SR Org/Contact", query);
         if (resultset != null && resultset.Length > 0)
         {
             return(resultset[0]);
         }
     }
     catch (Exception ex)
     {
         WorkspaceAddIn.InfoLog("Exception in getting exisitng affected Bus IDs: " + ex.Message);
     }
     return(null);
 }
        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>
        /// Perform Batch operation
        /// </summary>
        /// <param name="msg">BatchRequestItem Item</param>
        public void callBatchJob(Object msg)
        {
            try
            {
                /*** Form BatchRequestItem structure ********************/

                BatchRequestItem[] requestItems = new BatchRequestItem[1];

                BatchRequestItem requestItem = new BatchRequestItem();
                requestItem.Item = msg;

                requestItems[0]                      = requestItem;
                requestItems[0].CommitAfter          = true;
                requestItems[0].CommitAfterSpecified = true;
                /*********************************************************/

                ClientInfoHeader clientInfoHeader = new ClientInfoHeader();
                clientInfoHeader.AppID = "Batcher";

                BatchResponseItem[] batchRes = _rightNowClient.Batch(clientInfoHeader, requestItems);

                if (batchRes[0].Item.GetType().Name == "RequestErrorFaultType")
                {
                    RequestErrorFaultType requestErrorFault = (RequestErrorFaultType)batchRes[0].Item;
                    WorkspaceAddIn.InfoLog("There is an error with batch job :: " + requestErrorFault.exceptionMessage);
                }
            }
            catch (FaultException ex)
            {
                WorkspaceAddIn.InfoLog(ex.Message);
                return;
            }
            catch (Exception ex)
            {
                WorkspaceAddIn.InfoLog(ex.Message);
                return;
            }
        }
        /// <summary>
        /// Funtion to Run the report
        /// </summary>
        /// <param name="report">AnalyticsReport info like report ID and Filter detail</param>
        /// <returns>CSVTableSet</returns>
        public CSVTableSet RunReport(AnalyticsReport report)
        {
            CSVTableSet reportdata = null;

            byte[] bytearray = null;
            try
            {
                ClientInfoHeader hdr = new ClientInfoHeader()
                {
                    AppID = "Get Report Data"
                };
                reportdata = _rightNowClient.RunAnalyticsReport(hdr, report, 10000, 0, "~", false, true, out bytearray);
                if (reportdata != null && reportdata.CSVTables.Length > 0)
                {
                    return(reportdata);
                }
            }
            catch (Exception ex)
            {
                WorkspaceAddIn.InfoLog("Exception in RunReport: " + ex.Message);
            }
            return(null);
        }
        /// <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);
            }
        }
        /// <summary>
        /// Create Internal Incident Records
        /// </summary>
        /// <param name="contactID"></param>
        /// <param name="srID"></param>
        /// <param name="reportingIncID"></param>
        /// <param name="fsarID"></param>
        /// <param name="orgID"></param>
        /// <returns></returns>
        public int CreateInternalIncident(int contactID, int srID, int reportingIncID, int fsarID, int orgID)
        {
            try
            {
                //Check if it exist in order to avoid duplicate record
                Incident internalIncident = new Incident();
                string   response         = checkIfInternalIncidentExistForSR(reportingIncID, srID);
                if (response != null)
                {
                    internalIncident.ID = new ID
                    {
                        id          = Convert.ToInt32(response),
                        idSpecified = true
                    };
                }
                /*Set OOTB fields*/
                IncidentContact primarycontact = new IncidentContact {
                    Contact = new NamedID {
                        ID = new ID {
                            id = contactID, idSpecified = true
                        }
                    }
                };
                internalIncident.PrimaryContact = primarycontact;
                internalIncident.Organization   = new NamedID {
                    ID = new ID {
                        id = orgID, idSpecified = true
                    }
                };

                /*Set Custom fields*/
                List <GenericField> customAttributes = new List <GenericField>();
                customAttributes.Add(createGenericField("FSAR", createNamedIDDataValue(fsarID), DataTypeEnum.NAMED_ID));
                customAttributes.Add(createGenericField("SalesRelease", createNamedIDDataValue(srID), DataTypeEnum.NAMED_ID));
                customAttributes.Add(createGenericField("reporting_incident", createNamedIDDataValue(reportingIncID), DataTypeEnum.NAMED_ID));
                GenericObject customAttributeobj = genericObject(customAttributes.ToArray(), "IncidentCustomFieldsc");
                GenericField  caPackage          = createGenericField("CO", createObjDataValue(customAttributeobj), DataTypeEnum.OBJECT);

                /*Set Custom fields*/
                List <GenericField> customFields = new List <GenericField>();
                customFields.Add(createGenericField("incident_type", createNamedIDDataValueForName("Internal Incident"), DataTypeEnum.NAMED_ID));//55 is id of "Internal incident"
                GenericObject customfieldobj = genericObject(customFields.ToArray(), "IncidentCustomFieldsc");
                GenericField  cfpackage      = createGenericField("c", createObjDataValue(customfieldobj), DataTypeEnum.OBJECT);

                internalIncident.CustomFields = genericObject(new[] { caPackage, cfpackage }, "IncidentCustomFields");

                ClientInfoHeader hdr = new ClientInfoHeader()
                {
                    AppID = "Create Internal Incident"
                };
                //Update if internal incident exist
                if (response != null)
                {
                    _rightNowClient.Update(hdr, new RNObject[] { internalIncident }, new UpdateProcessingOptions {
                        SuppressExternalEvents = false, SuppressRules = false
                    });
                    return(Convert.ToInt32(response));
                }
                else//create a new internal incident
                {
                    RNObject[] resultobj = _rightNowClient.Create(hdr, new RNObject[] { internalIncident }, new CreateProcessingOptions {
                        SuppressExternalEvents = false, SuppressRules = false
                    });
                    if (resultobj != null)
                    {
                        return(Convert.ToInt32(resultobj[0].ID.id));
                    }
                }
            }
            catch (Exception ex)
            {
                WorkspaceAddIn.InfoLog("Excpetion in creating internal incident: " + ex.Message);
            }
            return(0);
        }
        /// <summary>
        /// Create Reporting Incident
        /// </summary>
        /// <param name="contactID"></param>
        /// <param name="fsarID"></param>
        /// <param name="orgID"></param>
        /// <returns></returns>
        public int CreateReportingIncident(int contactID, int fsarID, int orgID)
        {
            try
            {
                string response = checkIfReportingIncidentExistforFSAR(fsarID, contactID);
                if (response != null)
                {
                    return(Convert.ToInt32(response));
                }
                /*Set OOTB fields*/
                Incident        reportedIncident = new Incident();
                IncidentContact primarycontact   = new IncidentContact {
                    Contact = new NamedID {
                        ID = new ID {
                            id = contactID, idSpecified = true
                        }
                    }
                };
                reportedIncident.PrimaryContact = primarycontact;
                reportedIncident.Organization   = new NamedID {
                    ID = new ID {
                        id = orgID, idSpecified = true
                    }
                };
                reportedIncident.StatusWithType = new StatusWithType
                {
                    Status = new NamedID
                    {
                        Name = "Solution Development"
                    }
                };

                /*Set Custom Attributes*/
                List <GenericField> customAttributes = new List <GenericField>();
                customAttributes.Add(createGenericField("FSAR", createNamedIDDataValue(fsarID), DataTypeEnum.NAMED_ID));
                customAttributes.Add(createGenericField("FSAR_required", createBooleanDataValue(true), DataTypeEnum.BOOLEAN));
                GenericObject customAttributeobj = genericObject(customAttributes.ToArray(), "IncidentCustomFieldsc");
                GenericField  caPackage          = createGenericField("CO", createObjDataValue(customAttributeobj), DataTypeEnum.OBJECT);

                /*Set Custom fields*/
                List <GenericField> customFields = new List <GenericField>();
                customFields.Add(createGenericField("incident_type", createNamedIDDataValueForName("Reported Incident"), DataTypeEnum.NAMED_ID));//55 is id of "Internal incident"
                GenericObject customfieldobj = genericObject(customFields.ToArray(), "IncidentCustomFieldsc");
                GenericField  cfpackage      = createGenericField("c", createObjDataValue(customfieldobj), DataTypeEnum.OBJECT);

                reportedIncident.CustomFields = genericObject(new[] { caPackage, cfpackage }, "IncidentCustomFields");

                ClientInfoHeader hdr = new ClientInfoHeader()
                {
                    AppID = "Create Reported Incident"
                };
                RNObject[] resultobj = _rightNowClient.Create(hdr, new RNObject[] { reportedIncident }, new CreateProcessingOptions {
                    SuppressExternalEvents = false, SuppressRules = false
                });
                if (resultobj != null)
                {
                    return(Convert.ToInt32(resultobj[0].ID.id));
                }
            }
            catch (Exception ex)
            {
                WorkspaceAddIn.InfoLog("Excpetion in creating reported incident: " + ex.Message);
            }
            return(0);
        }